Pascal Naber
Pascal Naber

Reputation: 1092

NServicebus Test.Handler ExpectSend does not give expected result

I asked this some time ago on the yahoogroup but unfortunately no answer.

When ExpectSend is added to the unittest, the test fails with this message:

Rhino.Mocks.Exceptions.ExpectationViolationException: IBus.Send(callback method:
<>c__DisplayClass2`1.<ExpectSend>b__1); Expected #1, Actual #0

.

This looks like the Bus.Send method is never called. But it is. When the line with .ExpectSend is not used, the test succeeded.

    [TestMethod()]
    public void Should_Handle_Goedkeuring_Which_Results_In_VolledigGoedgekeurdeFactuur()
    {
        int inkoopFactuurId = 5;

        Test.Initialize();

        var mock = new Mock<IProcesGoedkeuringDataService>();
        mock.Setup(x => x.IsFactuurVolledigGoedgekeurd(inkoopFactuurId)).Returns(true);

        Test.Handler<GoedkeuringDoorGebruikerCommandHandler>()
        .WithExternalDependencies(h => h.DataService = mock.Object)
        .ExpectSend<FactuurVolledigGoedgekeurdCommand>(c =>
        c.InkoopFactuurId == inkoopFactuurId)
        .OnMessage<GoedkeuringDoorGebruikerCommand>(m =>
        SetupMessage(m)); ;
    }

The handler: The IsFactuurVolledigGoedgekeurd method returns true in this case.

public class GoedkeuringDoorGebruikerCommandHandler : IHandleMessages<GoedkeuringDoorGebruikerCommand>
{
    public IBus Bus { get; set; }
    public IProcesGoedkeuringDataService DataService { get; set; }

    public void Handle(GoedkeuringDoorGebruikerCommand message)
    {
        RegistreerGoedkeuring(message.InkoopFactuurId, message.GebruikerId);

        bool volledigGoedgekeurd = IsFactuurVolledigGoedgekeurd(message.InkoopFactuurId);
        if (volledigGoedgekeurd)
        {
            Bus.Send(new FactuurVolledigGoedgekeurdCommand(message.InkoopFactuurId));
        }
    }
}

Upvotes: 1

Views: 1106

Answers (1)

jacek.spolnik
jacek.spolnik

Reputation: 21

Check this code (based on RequestResponse NServiceBus sample):

[TestFixture]
public class Tests
{
    [Test]
    public void TestHandler()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(m => m.DataId == dataId && m.String == str && m.SecretQuestion == secret)
            .OnMessage<RequestDataMessage>(m => { m.DataId = dataId; m.String = str; m.SecretQuestion = secret; });
    }

    [Test]
    public void TestHandler2()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(Check)
            .OnMessage<RequestDataMessage>(m => { m.DataId = dataId; m.String = str; m.SecretQuestion = secret; });
    }

    private static bool Check(RequestDataMessage m)
    {
        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        return m.DataId == dataId && m.String == str && m.SecretQuestion == secret;
    }

    [Test]
    public void TestHandler3()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(m => m.DataId == dataId && m.String == str && m.SecretQuestion == secret)
            .OnMessage<RequestDataMessage>(m => SetUp(m));
    }

    private static void SetUp(RequestDataMessage m)
    {
        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        m.DataId = dataId;
        m.String = str;
        m.SecretQuestion = secret;
    }

Output: 1. Pass 2. Fail 3. Fail

(Rhino.Mocks.Exceptions.ExpectationViolationException : IBus.Send(callback method: <>c_DisplayClass2`1.b_1); Expected #1, Actual #0.)

Probably the using of your method applied as an action to ExpectSend/OnMessage method it breaks the RhinoMocks expectation. I do not know why, but it is the reason why exception appeared.

Upvotes: 1

Related Questions