moq - How to unit test a helper funtion call in a collection in C# using Mock -
i 'm new unit testing , want create unit test following code:
public ienumerable<product> createproductlist( ienumerable<int> productsids, ienumerable<product> products) { var productslist = productsids.groupjoin( inner: products, outerkeyselector: x => x, innerkeyselector: y => y.id, resultselector: (x, y) => _productservicehelper. factoryproduct(x, y.firstordefault(k => k.id == x))).tolist(); return productslist; }
the unit test have written far trying use mocksequence unsuccessfully:
[test, testcasesource(nameof(testcases_createproductlist_somemissingids))] public void createproductlist_somemissingids_returnlistcontainingmissingones (ienumerable<int> listwithrequiredproductsids, ienumerable<product> listmissingids, ienumerable<product> expectedlistcontainingallrequired) { //_mockproductservicehelper.insequence(sequence).setup(x => // x.factoryproduct(it.isany<int>(), it.isany<product>())).returns(()=> expectedlistcontainingallrequire(x => x)); //expectedlistcontainingallrequired.tolist() // .select(x => _mockproductservicehelper.insequence(sequence) // .setup(y => y.factoryproduct(it.isany<int>(), it.isany<product>())) //.returns(x)); //expectedlistcontainingallrequired. var result = _productservice.createproductlist(listwithrequiredproductsids, listmissingids); var jsonresult = jsonconvert.serializeobject(result); var jsonexpectedlistcontainingallrequired = jsonconvert.serializeobject (expectedlistcontainingallrequired); //_mockproductservicehelper.verify(x => x.factoryproduct(),times.once); //assert.that(result.count(), is.equalto(expectedlistcontainingallrequired.count())); assert.that(result.count(), is.equalto(expectedlistcontainingallrequired.count())); assert.that(jsonresult, is.equalto(jsonexpectedlistcontainingallrequired)); } public static ienumerable testcases_createproductlist_somemissingids { { yield return datacases_createproductlist_somemissingids( new list<int> { 1, 2 }, new list<int> { 1 }); yield return datacases_createproductlist_somemissingids( new list<int> { 1, 2, 3, 4 }, new list<int> { 1, 2 }); } } private static testcasedata datacases_createproductlist_somemissingids (ienumerable<int> productids, ienumerable<int> productobjects) { var productlistobjects = productobjects.select(createmockobject); var expectedproductlist = productids.select(createmockobject); return new testcasedata(productids, productlistobjects, expectedproductlist); } private static product createmockobject(int x) { return new product { category = new category { id = 1, name = "padrao" }, name = "product " + x, description = "any description " + x, creation = datetime.now.date, launch = datetime.now.date, price = x * 2 }; }
i have tried many things apparently nothing works. left paths tried follow commented.
does have suggestion or example helps accomplish testing method?
Comments
Post a Comment