我如何嘲笑HttpResponseBase.End()? [英] How do I mock HttpResponseBase.End()?

查看:245
本文介绍了我如何嘲笑HttpResponseBase.End()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用起订量来创建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的)的概念。然而,有,因为它一个功能请求。



您可能能够与设置End方法来切换决定了模拟任何进一步的行为放在一起使用回调,但它不会是特别漂亮。我想是这样的:

 布尔结束= FALSE; 
变种模拟=新模拟< HttpResponseBase>();
mock.Setup(X => x.End())回调(()=>截止= TRUE)。
//其他设置涉及'结束'和回调



然后让所有其他设置有双implementatations根据是否结束是真还是假。



这将是非常该死的丑,所以我会认真重新考虑我的选择在这一点上。




  1. 请个假执行HttpResponseBase的,而不是使用起订量:您可以在那里至少需要两个方向。这听起来像你期待一个测试用双嵌入式逻辑听起来像一个更好的选择,实施这样的具体行为。不久的说,假的就是测试替身可以包含一个模仿预期的生产实施半复杂的逻辑。您可以在的xUnit测试模式书阅读更多关于伪造品和其他测试双打。

  2. 重新考虑你最初的设想。这听起来像你对我有非常密切的绑你的客户HttpResponseBase的特定行为,所以你可能违反了里氏替换原则。不过,我可能是弄错了,一个名为终结的方法进行超越了单纯的语义内涵一定的,但尽管如此,我个人认为,如果一个更好的设计是可能的。


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:

  1. 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.
  2. 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆