1

I am currently using FakeItEasy for unit testing. I'm faking NServiceBus .Send method call was made in a method.

The problem I'm running across is I'm sending out two message on the bus in the method:

bus.Send(new CommandOne(Id = [something]));
bus.Send(new CommandTwo(Id = [something]));

both commands are sent on the faked bus in the same method call (I removed the real command names and params for the sake of simplicity).

Here is what my NUnit code/FakeItEasy code looks like:

bus = A.Fake<IBus>;
var sut = new Sut(bus);
sut.MethodCall();

A.CallTo(() => bus.Send(A<CommandOne>.That.Matches(co => co.Id == [someId]).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => bus.Send(A<CommandTwo>.That.Matches(co => co.Id == [someId]).MustHaveHappened(Repeated.Exactly.Once);

But, when I run this test, I get a test failed result, stating:

System.InvalidCastException : Unable to cast object of type 'CommandTwo' to type 'CommandOne'.

When I send two different commands suing the same fake, I'm specifying the specific object type and a match on the properties of the command, but FakeItEasy is trying to cast the first command being sent (CommandOne), two the second command sent (CommandTwo)...

is there a way to use FakeItEasy to test the same method being called twice of a faked object with different values for each call?

Thanks, Mike

Michael McCarthy
  • 1,502
  • 3
  • 18
  • 45

1 Answers1

2

Your problem sounds very much like Issue 326 - Argument Constraints should support derived types as Type Arguments, which was recently fixed in FakeItEasy release 1.22.0. If you're not using that release, please upgrade and see if the behaviour persists. If you are using 1.22.0, please come back and comment so we can keep trying!

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • Blair, the upgrade worked. Thanks so much! Also nice that the Castle.Core dependency is now gone. – Michael McCarthy Jul 15 '14 at 12:47
  • I'm glad it worked. You must've been on an _old_ version. The Castle.Core dependency left in… before I started using FakeItEasy. Let me check… [1.9.0](https://www.nuget.org/packages/FakeItEasy/1.9.0)! – Blair Conrad Jul 15 '14 at 14:35