【发布时间】:2011-03-05 05:18:44
【问题描述】:
我正在对用于检索Customer 类型对象的ICustomerRepository 接口进行单元测试。
- 作为一个单元测试,以这种方式测试
ICustomerRepository可以获得什么价值? - 以下测试在什么情况下会失败?
- 对于这种性质的测试,是否建议进行我知道会失败的测试?即当我知道我只在存储库中放置了
5时,查找 id4
我可能遗漏了一些明显的东西,但实现ICustomerRepository 的类的集成测试似乎更有价值。
[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));
}
}
测试基类
public class TestClassBase
{
protected T Stub<T>() where T : class
{
return MockRepository.GenerateStub<T>();
}
}
ICustomerRepository 和 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();
}
【问题讨论】:
标签: c# unit-testing tdd rhino-mocks