c# - How to mock(rhino.mocks) response object from base.SendAsync() in MVC4.5 Web API handler SendAsync() method -
i have write unit test on asp.net mvc web api controller rhino.mock have handler named ahandler.cs inherts system.net.http.httpclienthandler class. singnature sendasync method of ahandler.cs followings :
protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { ..... var response = base.sendasync(request, cancellationtoken).result; if (response.issuccessstatuscode) { ..... } }
the base keyword above means httpclienthandler , sendasync() method "protected"!!! try mock object "base.sendasync(request, cancellationtoken).result" , got hand-made response result wanted. seems rhino mocks can't see "base" keyword when wrote followings code :
var mockbase = mockrepository.generatemock<ahandler>; mockbase.stub(x => x.base <=== can't see base keyword ^^^^^
so change way , try mock httpclienthandler class
var mockbase = mockrepository.generatemockhttpclienthandler>; mockbase.stub(x => x. <== can't see sendasync() method, becase protected !!
now suffer in !! can give me advice how made custom response in mvc handler ?! !!
why want mock handler in first place ? can inject specific dummy implementation tests. handler return new httpresponse message expected tests.
public class mydummyhttphandler : delegatinghandler { httpresponsemessage response; public mydummyhttphandler(httpresponsemessage response) { this.response = response; } protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { ..... taskcompletionsource<httpresponsemessage> tsc = new taskcompletionsource<httpresponsemessage>(); tsc.setresult(this.response); return tsc.task; } }
Comments
Post a Comment