Reputation: 9242
i have an interface IKeyValueStore and i wanted to Mock this class and set its one method to return some dummy data. I have tried this way
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;
}
i am passing this storemock object to one of my testing class constructor. say TestClass
var objtestclass = new TestClass(storemock.Object);
and the i calling a method the call restore method.
objtestclass.checkstore();
during the constructor i am setting the IkeyValueStore type _store private field. So my testclass constructor is :-
public TestClass(IkeyValueStore store)
{
_store = store;
}
My testing Method (restore) is :-
Public async Task<IEnumerable<IProductDescription>> checkscore()
{
// here products is coming null.
Products = _store.Restore<List<IProductDescription>>(CachedProductsKey, null, new StringDataContractSerializer());
}
Problem :- In checkstore method product should have a list of product descriptions but is coming null. Am i making a wrong mock of IkeyValueStore ? any help is appreciated.
Upvotes: 1
Views: 166
Reputation: 5685
It looks like your problem is that the third argument is not getting matched correctly so your mock you set up is not being used.
In your setup instead of using new StringDataContractSerializer()
try It.IsAny<StringDataContractSerializer>()
Upvotes: 2