Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在外部环境中重用NSpec规范_C#_Bdd_Nspec - Fatal编程技术网

C# 在外部环境中重用NSpec规范

C# 在外部环境中重用NSpec规范,c#,bdd,nspec,C#,Bdd,Nspec,我正在学习NSpec框架 这是我的例子。我已经为一个简单的HttpRequester类编写了规范: using Moq; using NSpec; namespace FooBrowser.UnitTests.BDD { class HttpRequester_specification : nspec { private HttpRequester requester; private string sentData; pri

我正在学习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个请求”]=()=>sendTimes;检查一次外部上下文,而不是我想要的内部上下文

那么,我可以把它转移到外部环境吗

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


我在这里发现了类似的问题,但我想保留lambda表达式语法(没有继承),以便在一个位置查看所有规范。

很抱歉,这两个星期都没有得到回答,但我只需提取一个类似

void ItShouldRequestExactly(int n)
{
    it["should do " + n + " request"] = () => sendTimes.should_be(n);
}
在大多数情况下,这对我来说已经足够干了。 当您传入在spec执行时实际初始化的对象时,您会遇到一些微妙的问题,但是对于这个简单的示例,它非常适合。 遗憾的是,我没有看到将这种混合断言注入上下文的其他方法

void ItShouldRequestExactly(int n)
{
    it["should do " + n + " request"] = () => sendTimes.should_be(n);
}