Receiving a casting error message during execution

54 views Asked by At

This is my first foray into using NSubstitute.

I am trying to emulate a routine returning a 409 - Conflict error on a web call.

WebResponse response = Substitute.For<WebResponse>();
((HttpWebResponse)response).StatusCode.Returns(HttpStatusCode.Conflict);
return (response);

The code compiles cleanly, but upon execution the setting of the status code throws this:

Unable to cast object of type Castle.Proxies.WebResponse Proxy to type 'System.Net.HttpWebResponse'

Do I have some sort of syntax issue with my casting?

Thanks.

Bryan Hunt

1

There are 1 answers

2
Renat On BEST ANSWER

Right, HttpWebResponse is a derived class from WebResponse, so exception is correct. You might do a mock of HttpWebResponse instead:

var response = Substitute.For<HttpWebResponse>();
response.StatusCode.Returns(HttpStatusCode.Conflict);
...