Rhino Mocks:断言中使用的存根值?
首先是我的问题,然后是一些细节:
问:在确保在后续分配中使用属性的值时,是否需要对属性的值进行存根?
详细信息:
我在 MSpec 类中使用 Rhino Mocks 3.5 的 AAA 语法。我已经修剪了下面的代码,以使其(希望如此)易于理解。
*不存根 _fooResultMock 的属性值:*
[Subject("Foo")]
public class when_foo {
Establish context = () => {
_fooDependencyMock.Stub(x => x.GetResult()).Return(_fooResultMock);
_foo = new Foo(_fooDependencyMock);
};
Because action = () => {
_foo.Whatever();
};
It should_set_the_name_field = () => {
_fooTargetMock.AssertWasCalled(x => x.Name = _fooResultMock.Name);
};
}
*存根 _fooResultMock 的属性值:*
[Subject("Foo")]
public class when_foo {
Establish context = () => {
_fooDependencyMock.Stub(x => x.GetResult()).Return(_fooResultMock);
_fooResultMock.Stub(x => x.Name).Return(_theName); // _theName!
_foo = new Foo(_fooDependencyMock);
};
Because action = () => {
_foo.Whatever();
};
It should_set_the_name_field = () => {
_fooTargetMock.AssertWasCalled(x => x.Name = _theName); // _theName!
};
}
对我的测试来说重要的是,在 _fooResultMock 的 Name
属性中找到的值被分配给 _fooTargetMock 的属性。
那么,第一个代码块是否充分测试了这一点,或者第二个代码块(它存根 _fooResultMock 的 Name
属性的值)是否必要?
第二个区块是否出于某种原因不受欢迎?
First my question, and then some details:
Q: Do I need to stub the value of a property when making sure its value is used in a subsequent assignment?
Details:
I'm using Rhino Mocks 3.5's AAA syntax in MSpec classes. I've trimmed the code below to keep it (hopefully) easy to grok.
*Not Stubbing _fooResultMock's Property Value:*
[Subject("Foo")]
public class when_foo {
Establish context = () => {
_fooDependencyMock.Stub(x => x.GetResult()).Return(_fooResultMock);
_foo = new Foo(_fooDependencyMock);
};
Because action = () => {
_foo.Whatever();
};
It should_set_the_name_field = () => {
_fooTargetMock.AssertWasCalled(x => x.Name = _fooResultMock.Name);
};
}
*Stubbing _fooResultMock's Property Value:*
[Subject("Foo")]
public class when_foo {
Establish context = () => {
_fooDependencyMock.Stub(x => x.GetResult()).Return(_fooResultMock);
_fooResultMock.Stub(x => x.Name).Return(_theName); // _theName!
_foo = new Foo(_fooDependencyMock);
};
Because action = () => {
_foo.Whatever();
};
It should_set_the_name_field = () => {
_fooTargetMock.AssertWasCalled(x => x.Name = _theName); // _theName!
};
}
The important thing to my test is that the value found in _fooResultMock's Name
property gets assigned to _fooTargetMock's property.
So, does the first code block adequately test that, or is second code block (which stubs the value of _fooResultMock's Name
property) necessary?
Is the second block undesirable for any reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些问题,这将指示正确的答案:
_fooResultMock 是具体类的 PartialMock 吗?如果是这样,那么如果您不存根 Name,您将获得真实类的 Name 属性的值。如果 _fooResultMock 不是 PartialMock 并且您不存根它,您将获得 Name 类型的默认值(可能为 null)。
什么是 _fooTargetMock?此测试中的任何地方都没有指定它。应该是 _foo 吗?
我假设结果模拟不是部分模拟;部分模拟的主要情况是将单个类的某些方法与同一类中的其他方法隔离(例如,模拟文件写入方法,以便您可以测试调用文件写入方法的计算方法)。在这种情况下,第一个代码块基本上是将 null 与 null 进行比较,无论目标模拟是否从结果模拟中获取其 Name 字段。因此,第二个代码块可以更好地测试是否发生了赋值。
Some questions, which will indicate the correct answer:
Is _fooResultMock a PartialMock of a concrete class? If so, then if you don't stub Name, you'll get the value of the real class's Name property. If _fooResultMock is NOT a PartialMock and you don't stub it, you'll get the default for Name's type (probably null).
What's _fooTargetMock? It's not specified anywhere in this test. Is that supposed to be _foo instead?
I'm assuming that the results mock is not a partial mock; the main case for partial mocks is to isolate some methods of a single class from others in the same class (mocking a file-writing method, for instance, so you can test the calculation method that calls the file-writing method). In this case, the first code block is basically comparing null to null, whether the target mock got its Name field from the results mock or not. So, the second code block is a better test of whether an assignment occurred.