當我嘗試運行我的測試時,出現以下錯誤。我試過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");
}
那麼你的服務電話是什麼樣的?它真的是一個如此薄的層,它只是將請求逐字傳遞給你的存儲庫? –
這不是關於你*能夠擁有的東西 - 而是你的代碼具有什麼功能? –
@JonSkeet,我把它放在上面的編輯中。 – Fabii