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);
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.
source share