Moq + Unit Testing + System.Reflection.TargetParameterCountException: Parameter Count mismatch -
i getting "system.reflection.targetparametercountexception: parameter count mismatch" exception when attempting mock our apiclient.
i using following code setup moq response
private void setupapiclientforgetzones(ienumerable<zone> zone) { this.mockhubapiclient.setup(x => x.getasync<ienumerable<zone>>(it.isany<string>(), it.isany<idictionary<string, string>>())) .returns( (string name) => { return zone == null ? task.fromresult<ienumerable<zone>>(null) : task.run(() => zone); }); this.mockapiclientfactory.setup(x => x.createclient(it.isany<string>())) .returns(this.mockhubapiclient.object); }
the iapiclient interface attempting mock is
public interface iapiclientasync : iapiclient { task<string> getasync(string apicontroller); task<t> getasync<t>(string apicontroller) t : class; task<string> getasync(string apicontroller, idictionary<string, string> param, string querystring); task<t> getasync<t>(string apicontroller, idictionary<string, string> param) t : class; }
my unit test is
[test] public void getzonesnotcached() { var data = new list<zone> { new zone { zoneid = 1, zonename = "test zone 1" }, new zone { zoneid = 2, zonename = "test zone 2" } }; this.setupapiclientforgetzones(data); this.mockcache.setup(x => x.getitemfromcache<ienumerable<zone>>(it.isany<string>())); var organisationservice = new organisationservice(this.mockunitofworkasync.object, this.mockapiclientfactory.object, this.mockcache.object); var results = organisationservice.getzones(1, 1).tolist(); assert.isnotnull(results); assert.areequal(3, results.count, "there should 3 records returned"); this.mockcache.verify(x => x.getitemfromcache<ienumerable<zone>>(it.isany<string>()), times.once()); this.mockhubapiclient.verify(x => x.getasync<ienumerable<zone>>(it.isany<string>(), it.isany<idictionary<string, string>>()), times.once()); }
i have found numerous other posts same exception none of solutions or examples same mine.
i have been able mock response when calling getasync method has single string paramter.
private void setupapiclientforalldealerdetails(ienumerable<dealerdetail> dealerdetails) { this.mockhubapiclient.setup( x => x.getasync<ienumerable<dealerdetail>>(it.isany<string>())) .returns( (string name) => { return dealerdetails == null ? task.fromresult<ienumerable<dealerdetail>>(null) : task.run(() => dealerdetails); }); this.mockapiclientfactory.setup(x => x.createclient(it.isany<string>())) .returns(this.mockhubapiclient.object); }
any ideas anyone?
if use expression in .returns instead of value, expression's parameters must match of method signature mocking.
for example, found in setupapiclientforgetzones:
this.mockhubapiclient.setup(x => x.getasync<ienumerable<zone>>(it.isany<string>(), it.isany<idictionary<string, string>>())) .returns( (string name) => { return zone == null ? task.fromresult<ienumerable<zone>>(null) : task.run(() => zone); });
when should be:
this.mockhubapiclient.setup(x => x.getasync<ienumerable<zone>>(it.isany<string>(), it.isany<idictionary<string, string>>())) .returns<string, idictionary<string, string>>( (name, dict) => { return zone == null ? task.fromresult<ienumerable<zone>>(null) : task.run(() => zone); });
Comments
Post a Comment