Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks: stubbing value used in assertion?

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?

like image 996
lance Avatar asked Nov 14 '22 23:11

lance


1 Answers

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.

like image 142
KeithS Avatar answered Dec 18 '22 05:12

KeithS