问题描述
我使用起订量来创建HttpResponseBase的模仿对象。我需要能够测试HttpResponseBase.End()被调用在我的图书馆。要做到这一点,我指定的电话后一些文本之前一些文本。然后我检查,只有在调用结束()之前的文本存在于HttpResponseBase.Output。
现在的问题是,我无法弄清楚如何嘲笑HttpResponseBase .END(),使其停止处理,就像在ASP.NET中一样。
公共静态HttpResponseBase CreateHttpResponseBase(){
变种模拟=新模拟< HttpResponseBase>();
StringWriter的输出=新的StringWriter();
mock.SetupProperty(X => x.StatusCode);
mock.SetupGet(X => x.Output).Returns(输出);
mock.Setup(X => x.End())/ *我该怎么放在这里? * /;
mock.Setup(X => x.Write(It.IsAny&所述;串GT;()))
.Callback&所述;串将(S => output.Write(S));
返回mock.Object;
}
这是一个有点不清楚我它是什么,你正在努力实现的,但是从你的描述,它听起来就像你正试图让你的抽象表现得像一个特定的实现。换句话说,因为HttpResponse.End()具有一定的行为,你希望你的模拟有同样的行为?
在一般情况下,这是特别不容易做到与Moq的,因为它没有的有序的期望(不像RhinoMocks的)的概念。然而,有,因为它一个书阅读更多关于伪造品和其他测试双打。
I'm using Moq to create a mock object of HttpResponseBase. I need to be able to test that HttpResponseBase.End() was called in my library. To do this, I specify some text before the call and some text after. Then I check that only the text before the call to End() is present in HttpResponseBase.Output.
The problem is, I can't figure out how to mock HttpResponseBase.End() so that it stops processing, like it does 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;
}
It is a bit unclear to me what it is you are trying to achieve, but from your description, it sounds like you are attempting to get your Abstraction to behave like a particular implementation. In other words, because HttpResponse.End() has a certain behavior, you want your Mock to have the same behavior?
In general, that is not particularly easy to do with Moq, since it has no concept of ordered expectations (unlike RhinoMocks). There is, however, a feature request for it.
You might be able to use a Callback together with setting up the End method to toggle a flag that determines any further behavior of the Mock, but it's not going to be particularly pretty. I'm thinking about 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 have all other Setups have dual implementatations based on whether ended
is true or false.
It would be pretty damn ugly, so I'd seriously reconsider my options at this point. There are at least two directions you can take:
- Make a Fake implementation of HttpResponseBase instead of using Moq. It sounds like you are expecting such specific behavior of the implementation that a Test Double with embedded logic sounds like a better option. Put shortly, a Fake is a Test Double that can contain semi-complex logic that mimics the intended production implementation. You can read more about Fakes and other Test Doubles in the excellent xUnit Test Patterns book.
- Reconsider your initial assumptions. It sounds to me like you are tying your client very closely to a particular behavior of HttpResponseBase, so you may be violating the Liskov Substitution Principle. However, I may be mistaken, as a method called 'End' carries certain connotations beyond the purely semantic, but still, I'd personally consider if a better design was possible.
这篇关于我如何嘲笑HttpResponseBase.End()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!