|
From: Feininger, C. <chr...@si...> - 2007-08-15 14:52:38
|
Hello everybody,
=20
I worked a little bit with NMock 2 trying to do the following case. I'd
like to write a test with tells me whether some methods are called in
the right order and also the correct amount of the calls. After a while
I figured out that I have to use the Ordered Property of the Mockery
class. I observed the following behavior:
=20
Let's say I have the following interface:
=20
public interface ICurrencyService
{ =20
void Method1();
void Method2();
void Method3();
}=20
=20
And now I have an implementation of a class where I want to use the
interface like this
=20
public class AccountService : IAccountService
{
private readonly ICurrencyService currencyService;
=20
public AccountService(ICurrencyService currencyService)
{
this.currencyService =3D currencyService;
}
=20
public void Whatever()
{
currencyService.Method1(); =20
currencyService.Method1(); =20
currencyService.Method2(); =20
}
}
=20
Now I wrote my TestClass like this:=20
=20
[TestFixture]
public class CurrencyServiceTest
{
private Mockery mocks;
private ICurrencyService mockCurrencyService;
private IAccountService accountService;
=20
[SetUp]
public void SetUp()
{
mocks =3D new Mockery();
mockCurrencyService =3D mocks.NewMock<ICurrencyService>();
accountService =3D new AccountService(mockCurrencyService);
}
=20
[Test]
public void MethodConstrains()
{=20
using (mocks.Ordered)
{
=20
Expect.Exactly(2).On(mockCurrencyService).Method("Method1").WithNoArgume
nts();
=20
Expect.Exactly(1).On(mockCurrencyService).Method("Method2").WithNoArgume
nts();
=20
Expect.Exactly(1).On(mockCurrencyService).Method("Method2").WithNoArgume
nts();
}
currencyService.Whatever();
}
}
=20
I expected that my test fails because I just call Method2 once and not
twice. But it doesn't. The test passes. If I change the the using-clause
to:
=20
using (mocks.Unordered)
{//like above}
=20
Than my test fails, but then I can't guarantee the right order. I played
a bit around with it and figured out that if you use the ordered
property everything which is under an expected amount will pass but if
it is over the expected amount it won't pass. Only if you use the
unordered property I could get notice of the right amount of method
calls but not in the right order.=20
=20
Is that a bug or did I miss something. How do I get my test failed if I
call methods which are not in the right order and if there arent't
enough calls?
=20
Hopefully you can help me.=20
=20
Thanks
=20
Chris
=20
|