尝试运行测试时出现以下错误。我试过IgnoreArguments()
了,但这似乎没有奏效。这个异常只有在我尝试时才会抛出,mock.Ordered()
如果我这样做mock.Record()
了,它就会通过。
System.InvalidOperationException:以前的方法“IProductRepository.GetAllProducts();” 需要返回值或抛出异常。
[Test]
public void TestwithOrderedRecordsandPlayBack()
{
var mock = new MockRepository();
var productRepository = mock.StrictMock<IProductRepository>();
var product = new Grains
{
Name = "Wonder Bread",
Category = "Grains"
};
IList list = new ArrayList();
//Class under test
var service = new ProductService(productRepository);
using (mock.Ordered())
{
Expect.Call(productRepository
.GetAllProducts())
.IgnoreArguments()
.Return(new ArrayList());
Expect.Call(()=>productRepository.SaveProduct(product));
}
using (mock.Playback())
{
list = service.GetAllProducts();
service.SaveProduct(product);
}
}
服务电话:
public virtual IList GetAllProducts()
{
IList list = _productRepository.GetAllProducts();
return list;
//throw new System.Exception("Not implemented");
}
public virtual IList GetAllProducts()
{
IList list = _productRepository.GetAllProducts();
return list;
//throw new System.Exception("Not implemented");
}