我正在学习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");
};
};
};
}
}
}
可以看到["should do 1 request"] = () => 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);
};
但这导致它["should do 1 request"] = () => sendTimes.should_be(1);只检查一次外部上下文,而不是我想要的内部上下文。
我能把它移到外部环境吗?
或者是更容易贡献一些代码到NSpec来实现这样的行为?我在这里发现了类似的问题,重用NSpec规范,但我想保持lambda表达式语法(没有继承),以便在一个地方看到所有规范
很抱歉看到这个问题两周没有得到回答,但我只是通过提取像
这样的方法来解决这个问题void ItShouldRequestExactly(int n)
{
it["should do " + n + " request"] = () => sendTimes.should_be(n);
}
在大多数情况下,这对我来说已经足够DRY了。当你传入的对象实际上是在规范执行时初始化的时候,你会遇到一些微妙的问题,但是对于这个简单的例子来说,它非常适合。遗憾的是,我没有看到将这种mixin断言注入到上下文中的其他方法。