mocks do not fail if the code under test calls an expected method with different arguments

1 view
Skip to first unread message

Kedar

unread,
Jun 23, 2009, 12:30:29 PM6/23/09
to Rhino.Mocks
public interface IConsole {
void Print(string s);
}

[TestFixture]
public class SomeTestFixture
{
public void MethodToTest(IConsole console)
{
console.Print("a");
console.Print("b");
}

[Test]
public void Test()
{
var mockConsole = MockRepository.GenerateMock<IConsole>();
mockConsole.Expect(x => x.Print("a"));
MethodToTest(mockConsole);
mockConsole.VerifyAllExpectations();
}
}

In this example, the only expectation I have is to call Print with
argument "a". But the code under test calls it with "a" and then with
"b". I thought that the VerifyAllExpectations assertion would fail.
But it does not. Is this a problem or am I doing something wrong ?

Alex McMahon

unread,
Jun 23, 2009, 12:49:18 PM6/23/09
to Rhino...@googlegroups.com
GenerateMock will return a dynamicMock, i.e. It will allow unspecified
calls to be made on it. So the behaviour you're seeing is correct

If you wanted the test to fail you could either explicitly create a
strict mock or use mockConsole.AssertWasNotCalled(x=>x.Print("b"));

2009/6/23 Kedar <kgod...@gmail.com>:

Tim Barcz

unread,
Jun 23, 2009, 3:13:09 PM6/23/09
to Rhino...@googlegroups.com
You can get the behavior you're looking for by using the Record/Replay model and using "CreateMock()" which will create a strict mock as Alex has pointed out.  I tend to agree with Alex's proposal to use AssertWasNotCalled, because I favor the AAA syntax.  However you should know that what you want is available using Record/Replay.

Note however that this may be an "overspecification" which is one of the reasons I don't particularly care for strict mocks and should be used with caution.  I don't want to speak for Oren but I believe that is why the default is now a dynamic mock....too many people fell into the over specification trap and paid dearly for it later with failing tests.

Tim
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Reply all
Reply to author
Forward
0 new messages