我刚开始使用 Rhino mock 为我的项目设置测试用例。.Return(objToReturn:list)究竟是做什么的?
如果我初始化并填充列表然后将其传递给模拟存根方法,它似乎才起作用。我假设我可以使用 Mock Stub 方法填充一个列表,然后返回该填充的列表。
....
private ProductRepository _productRepository;
private IProductRepository _productRepositoryStub;
[SetUp]
public void SetUp()
{
_productRepository = new ProductRepository();
//Testing using Rhino Mocks
//Generate stub
_productRepositoryStub = MockRepository.GenerateMock<IProductRepository>();
}
[Test]
public void Canquerydb()
{
IList list = _productRepository.GetAllProducts();
_productRepository.Stub(x=> x.GetAllProducts()).Return(list);
_productRepositoryStub.AssertWasCalled(x => x.GetAllProducts());
}
/// <summary>
/// Refaactor and use MockRhino here
/// </summary>
[Test]
public void can_insert_product()
{
IProduct product = new Grains("Cheese Bread", "Dairy grain", 0);
_productRepository.SaveProduct(product);
_productRepositoryStub.Stub(x=>x.SaveProduct(product));
_productRepositoryStub.AssertWasCalled(x => x.SaveProduct(product));
}