【发布时间】:2014-06-20 09:46:10
【问题描述】:
我有一个接口IKeyValueStore,我想模拟这个类并设置它的一个方法来返回一些虚拟数据。这种方式我试过了
var storemock = new Mock<IKeyValueStore>();
storemock.Setup(m => m.Restore<List<IProductDescription>>(It.IsAny<string>(),null,new StringDataContractSerializer())).Returns(ListOfProductDescriptions);
private List<IProductDescription> ListOfProductDescriptions()
{
var obj1 = new ProductDescription("id1");
var lst = new List<IProductDescription>();
lst.Add(obj1);
return lst;
}
我正在将此 storemock 对象传递给我的测试类构造函数之一。说 TestClass
var objtestclass = new TestClass(storemock.Object);
和我调用一个方法调用restore方法。
objtestclass.checkstore();
在构造函数期间,我正在设置 IkeyValueStore 类型 _store 私有字段。所以我的 testclass 构造函数是:-
public TestClass(IkeyValueStore store)
{
_store = store;
}
我的测试方法(恢复)是:-
Public async Task<IEnumerable<IProductDescription>> checkscore()
{
// here products is coming null.
Products = _store.Restore<List<IProductDescription>>(CachedProductsKey, null, new StringDataContractSerializer());
}
问题 :- 在 checkstore 方法中,产品应该有一个产品描述列表,但现在为空。我对 IkeyValueStore 做了一个错误的模拟吗?任何帮助表示赞赏。
【问题讨论】:
标签: c# unit-testing mocking moq