首先我的问题,然后有些细节:

q:我是否需要存根属性的值,确保其值在后续作业中使用?

详细信息:
我正在使用rhino模型3.5在mspec类中的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);
    };
}
.

* stubbing _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属性的值)是必要的?

是由于任何原因的第二块不期望

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top