c# - Setting up methods by using lambda expressions -
i'm trying fake method on instance using correspondent lambda expression:
private void translatecallbacktosetup<tresult>(mock<tservice> stubservice, imethodcall<tservice,tresult> methodcall) { stubservice.setup(t => methodcall.runmethod(t)).returns(() => { return default(tresult); }); } public interface imethodcall<in tservice, out tresult> : imethodcall tservice : class { func<tservice, tresult> runmethod { get; } }
the syntax seems fine, yet code fails argumentexception:
expression not method invocation: t => t
any thoughts?
this failing because you're trying set method on other mock itself.
you're saying want imethodcall
instance return value when runmethod
method called stubservice
parameter. in case you'd need pass in mock imethodcall
, object behaviour defining.
if @ examples here, you'll see methods being mocked methods on mock. if refactor tservice type take methodcall instead, might work.
on service
public iservice { tresult executemethodcall(imethodcall<iservice, tresult>); }
and in test
stubservice.setup(t => t.executemethodcall(methodcall))
Comments
Post a Comment