How do I make fun of HttpResponseBase.End ()?

I am using Moq to create the layout of an HttpResponseBase object. I need to check that HttpResponseBase.End () has been called in my library. To do this, I specify the text before the call and some text after. Then I check that only the text before calling End () is present in the HttpResponseBase.Output.

The problem is that I cannot figure out how to make fun of HttpResponseBase.End () so that it stops processing like in ASP.NET.

public static HttpResponseBase CreateHttpResponseBase() { var mock = new Mock<HttpResponseBase>(); StringWriter output = new StringWriter(); mock.SetupProperty(x => x.StatusCode); mock.SetupGet(x => x.Output).Returns(output); mock.Setup(x => x.End()) /* what do I put here? */; mock.Setup(x => x.Write(It.IsAny<string>())) .Callback<string>(s => output.Write(s)); return mock.Object; } 
+4
source share
1 answer

It’s a little incomprehensible to me what exactly are you trying to achieve, but from your description it sounds like you are trying to make your abstraction behave like a concrete implementation. In other words, since HttpResponse.End () has a specific behavior, do you want your Mock to have the same behavior?

In general, this is not particularly easy to do with Moq, because it has no concept of ordered expectations (unlike RhinoMocks). However, there is a function request for it .

You may be able to use the callback along with setting the End method to toggle the flag that defines any further Mock behavior, but that won't be particularly pretty. I am thinking of something like this:

 bool ended = false; var mock = new Mock<HttpResponseBase>(); mock.Setup(x => x.End()).Callback(() => ended = true); // Other setups involving 'ended' and Callbacks 

Then all other installations have a double implementation based on whether ended true or false.

That would be damn ugly, so I would seriously review my options at this point. There are at least two areas that you can take:

  • Make a fake implementation of HttpResponseBase instead of using Moq. It looks like you expect such specific implementation behavior that Test Double with integrated logic sounds like the best option. In short, Fake is a Test Double that can contain semi-commercial logic that mimics an intended production implementation. You can learn more about fakes and other tests in the excellent xUnit Test Patterns book .
  • Revise your initial assumptions. It seems to me that you very closely bind your client to the specific behavior of the HttpResponseBase, so you can violate the Liskov Substitution Principle. Nevertheless, I can be mistaken, since the method called β€œThe End” carries certain connotations beyond the purely semantic, but still I personally think that a better design is possible.
+2
source

All Articles