BDD:何时/何地设置存根?
我使用 TDD/SSR 一段时间了。我正在尝试过渡到 BDD:上下文、becauseOf 和断言。
我正在使用 Rhino Mocks 进行隔离,现在我正在为语法而苦苦挣扎。这是我到目前为止所得到的(注意:ContextSpecification 类源代码):
public static class DocumentIdAdapterTests {
public class DocumentIdAdapterContext : ContextSpecification {
protected IDocumentIdAdapter _documentIdAdapter;
protected ISettings _settingsMock;
protected override void Context() {
_settingsMock = MockRepository.GenerateMock<ISettings>();
_documentIdAdapter = new DocumentIdAdapter(_settingsMock);
}
}
[TestClass]
public class when_single_document_url_is_created : DocumentIdAdapterContext {
protected override void BecauseOf() {
_settingsMock.Stub(x => x.DocumentServiceBaseUrl).Return("fooOutput");
_documentIdAdapter.GetDocumentServiceSingleDocumentUrl("fooInput");
}
[TestMethod]
public void the_settings_should_provide_the_document_service_base_url() {
_settingsMock.AssertWasCalled(x => { var ignored = x.DocumentServiceBaseUrl; });
}
}
}
我应该在哪里设置我的存根?例如,我应该在哪里存根 DocumentServiceBaseUrl 属性将返回的值?我现在正在 CauseOf 方法中执行此操作,但我应该在 Context 方法中执行此操作吗?
I've been using TDD/SSR for a while. I'm trying to transition to BDD: context, becauseOf, and Asserts.
I'm using Rhino Mocks to isolate, and I'm struggling with syntax now. Here's what I've got so far (note: ContextSpecification class source):
public static class DocumentIdAdapterTests {
public class DocumentIdAdapterContext : ContextSpecification {
protected IDocumentIdAdapter _documentIdAdapter;
protected ISettings _settingsMock;
protected override void Context() {
_settingsMock = MockRepository.GenerateMock<ISettings>();
_documentIdAdapter = new DocumentIdAdapter(_settingsMock);
}
}
[TestClass]
public class when_single_document_url_is_created : DocumentIdAdapterContext {
protected override void BecauseOf() {
_settingsMock.Stub(x => x.DocumentServiceBaseUrl).Return("fooOutput");
_documentIdAdapter.GetDocumentServiceSingleDocumentUrl("fooInput");
}
[TestMethod]
public void the_settings_should_provide_the_document_service_base_url() {
_settingsMock.AssertWasCalled(x => { var ignored = x.DocumentServiceBaseUrl; });
}
}
}
Where am I supposed to setup my stubs? For example, where am I supposed to stub the value that the DocumentServiceBaseUrl property will return? I'm doing it in my BecauseOf method now, but should I be doing it in my Context method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于哪些上下文会影响类的行为,以及哪些上下文是类运行所必需的。
如果您始终从特定上下文开始(例如,您的文档服务始终位于特定 URL),那么您可以在构造函数或设置方法中进行设置。
如果你有影响你行为的上下文(你称之为becauseOfs),那么每个上下文都需要一个新的场景。这通常是驱动场景的原因 - 产生不同结果(断言)的上下文组合。
一个好的 BDD 技巧是寻找不同的上下文。想一想,“我的代码应该始终以这种方式运行吗?是否存在产生不同结果的上下文?”这为您提供了良好的对话起点,以发现您对代码不了解的任何内容,并允许您为行为的每个新方面提供示例(单元测试)。
It depends which contexts affect your class's behaviour, and which are simply necessary for your class to operate.
If you always start with particular contexts (for instance, your document service is always located at a particular URL) then you can set this up in the constructor or setup method.
If you have contexts which affect your behaviour (what you call becauseOfs) then each context will need a new scenario. This is usually what drives scenarios - combinations of contexts producing different outcomes (Asserts).
A good BDD trick is to look for different contexts. Think, "Should my code always behave that way? Is there a context which produces a different outcome?" This gives you good conversation starters for discovering anything you don't know about your code, and allows you to provide examples (Unit tests) for each new aspect of behaviour.