在外部上下文中重用NSpec规范

时间:2013-01-16 21:32:57

标签: c# bdd nspec

我正在研究NSpec框架。

这是我的例子。我为一个简单的HttpRequester类编写了规范:

using Moq;
using NSpec;

namespace FooBrowser.UnitTests.BDD
{
    class HttpRequester_specification : nspec
    {
        private HttpRequester requester;

        private string sentData;
        private int sendTimes;

        private readonly Mock<IConnection> connectionMock;
        private string resource;

        public HttpRequester_specification()
        {
            connectionMock = new Mock<IConnection>();

            connectionMock
                .Setup(x => x.Send(It.IsAny<string>()))
                .Callback<string>(data =>
                {
                    sendTimes++;
                    sentData = data;
                });
        }

        void given_opened_connection_with_no_recent_sends()
        {
            before = () =>
            {
                sendTimes = 0;
            };

            context["when HttpRequester is constructed"] = () =>
            {
                before = () => requester = new HttpRequester(connectionMock.Object);

                it["should not do any request"] = () => sendTimes.should_be(0);

                context["when performing request"] = () =>
                {
                    act = () => requester.Request(resource);

                    context["when resource is not specified"] = () =>
                    {
                        it["should do 1 request"] = () => sendTimes.should_be(1);
                        it["should send HTTP GET / HTTP/1.0"] = () => sentData.should_be("GET / HTTP/1.0");
                    };

                    context["when resource is index.html"] = () =>
                    {
                        before = () => resource = "index.html";

                        it["should do 1 request"] = () => sendTimes.should_be(1);
                        it["should send HTTP GET /index.html HTTP/1.0"] = () => sentData.should_be("GET /index.html HTTP/1.0");
                    };
                };
            };
        }
    }
}

正如你所看到的那样[“应该做1个请求”] =()=&gt; sendTimes.should_be(1);写了两次。

我尝试将它移动到外部上下文中:

context["when performing request"] = () =>
{
    act = () => requester.Request(resource);

    context["when resource is not specified"] = () =>
    {
        it["should send HTTP GET / HTTP/1.0"] = () => sentData.should_be("GET / HTTP/1.0");
    };

    context["when resource is index.html"] = () =>
    {
        before = () => resource = "index.html";

        it["should send HTTP GET /index.html HTTP/1.0"] = () => sentData.should_be("GET /index.html HTTP/1.0");
    };

    it["should do 1 request"] = () => sendTimes.should_be(1);
};

但这会导致它[[应该做1个请求“] =()=&gt; sendTimes.should_be(1);对于外部上下文检查一次,而不是根据我的需要检查内部上下文。

那么,我能以某种方式将它移到外部环境吗?

或者更容易为NSpec提供一些代码来实现这种行为吗?

我在Reusing NSpec specifications找到了类似的问题但是我想保留lambda表达式语法(没有继承)以查看1个地方的所有规范。

1 个答案:

答案 0 :(得分:2)

很抱歉看到这两周没有回复,但我只是通过提取像

这样的方法解决这个问题。
void ItShouldRequestExactly(int n)
{
    it["should do " + n + " request"] = () => sendTimes.should_be(n);
}

在大多数情况下,这对我来说太干了。 当您传入实际在规范执行时初始化的对象时,会遇到微妙的问题,但是对于这个简单的示例,它非常适合。 遗憾的是,我没有看到将这种mixin断言注入上下文的另一种方法。