什么是单元测试的接口库的目的 [英] What is the purpose of unit testing an interface repository

查看:162
本文介绍了什么是单元测试的接口库的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试用于检索键入客户

$ B对象的 ICustomerRepository 接口
$ b

  • 作为一个单元测试什么样的价值我在通过测试 ICustomerRepository 以这种方式获得?

  • 在什么条件下将下面的测试失败?

  • 对于这种性质的测试,它是最好做测试,我知道应该失败?即寻找ID的时候,我知道我只放在 5 存储库中的 4



我可能失去了一些东西明显,但它似乎是类,它实现 ICustomerRepository 将是整合测试。更多价值

  [TestClass中] 
公共类CustomerTests:TestClassBase
{
私人客户SetUpCustomerForRepository()
{
返回新客户()
{
CUSTID = 5,
DifId =55,
CustLookupName =花花公子 ,
LoginList =新[]
{
新登录{LoginCustId = 5,LoginName将=tdude},
新登录{LoginCustId = 5,LoginName将=tdude2}
}
};
}

[TestMethod的]
公共无效CanGetCustomerById()
{
//安排
VAR的客户= SetUpCustomerForRepository();
变种库=存根< ICustomerRepository>();

//行为
repository.Stub(REP => rep.GetById(5))返回(客户)。

//断言
Assert.AreEqual(客户,repository.GetById(5));
}
}



试验基地班

 公共类TestClassBase 
{
保护牛逼存根< T>()其中T:类
{
返回MockRepository.GenerateStub< T>();
}
}



ICustomerRepository和IRepository

  public接口ICustomerRepository:IRepository<客户> 
{
&IList的LT;客户> FindCustomers(串Q);
客户GetCustomerByDifID(字符串difId);
客户GetCustomerByLogin(字符串LOGINNAME);
}

公共接口IRepository< T>
{
无效保存(T实体);
无效保存(名单< T>实体);
BOOL保存(T实体,出字符串消息);
无效删除(T实体); $ B $(B T)GetById(INT ID);
ICollection的< T>的FindAll();
}


解决方案

规则#测试1:



了解您测试的目的是什么,它试图将其写之前证明。如果你不知道它证明了,那么它的无用:)



由于其他海报正确说,你删的界面,然后调用存根 - - ?这并不能证明你的产品代码是否正常工作什么



什么是存根



存根用于提供罐头值来驱动类的某些方面受测试。例如。假设你有一个的CustomerService 有类型的实例 ICustomerRepository 。如果你想看到的CustomerService 可以优雅地处理错误情况下,当库是空的,你会存根 ICustomerRepository GetCustomerById 方法返回什么/空/抛出一个异常,那么确保的CustomerService 的方法做正确的事情(如返回客户没有找到结果)。



即存根仅仅是一个合作者,可以帮助你达到所感兴趣的特定条件/行为。我们正在测试的CustomerService 和废止 ICustomerRepository 仅仅是帮助我们实现我们的目标。



你不是第一个问过这个问题:)。我通常建议开发商手工卷的测试双打第一。它有助于了解所有的相互作用,什么框架实际上是为你做的。


I am unit testing an ICustomerRepository interface used for retrieving objects of type Customer.

  • As a unit test what value am I gaining by testing the ICustomerRepository in this manner?
  • Under what conditions would the below test fail?
  • For tests of this nature is it advisable to do tests that I know should fail? i.e. look for id 4 when I know I've only placed 5 in the repository

I am probably missing something obvious but it seems the integration tests of the class that implements ICustomerRepository will be of more value.

[TestClass]
public class CustomerTests : TestClassBase
{
    private Customer SetUpCustomerForRepository()
    {
        return new Customer()
        {
            CustId = 5,
            DifId = "55",
            CustLookupName = "The Dude",
            LoginList = new[]
            {
                new Login { LoginCustId = 5, LoginName = "tdude" },
                new Login { LoginCustId = 5, LoginName = "tdude2" }
            }
        };
    }

    [TestMethod]
    public void CanGetCustomerById()
    {
        // arrange
        var customer = SetUpCustomerForRepository();
        var repository = Stub<ICustomerRepository>();

        // act
        repository.Stub(rep => rep.GetById(5)).Return(customer);

        // assert
        Assert.AreEqual(customer, repository.GetById(5));
    }
}

Test Base Class

public class TestClassBase
{
    protected T Stub<T>() where T : class
    {
        return MockRepository.GenerateStub<T>();
    }
}

ICustomerRepository and IRepository

public interface ICustomerRepository : IRepository<Customer>
{
    IList<Customer> FindCustomers(string q);
    Customer GetCustomerByDifID(string difId);
    Customer GetCustomerByLogin(string loginName);
}

public interface IRepository<T>
{
    void Save(T entity);
    void Save(List<T> entity);
    bool Save(T entity, out string message);
    void Delete(T entity);
    T GetById(int id);
    ICollection<T> FindAll();
}

解决方案

Rule #1 of testing:

Know what the purpose of your test is and what it is trying to prove before you write it. If you don't know what it proves, then it's useless :)

As the other posters have correctly said, you're stubbing an interface and then calling the stub -- this doesn't prove anything about whether your production code works.

What is a stub?

Stubs are used to provide canned values to drive some aspect of the class under test. E.g. Say you have a CustomerService that has an instance of type ICustomerRepository. If you wanted to see that the CustomerService could gracefully handle an error case when the repository was empty, you would stub the ICustomerRepository's GetCustomerById method to return nothing/null/throw an exception, then ensure that the CustomerService method did the correct thing (e.g. return a customer not found result).

I.e. the stub is just a collaborator that helps you reach the particular condition/behaviour of interest. We're testing CustomerService and the stubbed ICustomerRepository merely helps us achieve our goal.

You're not the first to ask this very question :). I usually advise developers to hand-roll their test doubles at first. It helps to understand all of the interactions and what the framework is actually doing for you.

这篇关于什么是单元测试的接口库的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆