验证NServiceBus事件属性(使用接口)

时间:2016-08-23 19:46:03

标签: nservicebus

我无法验证使用NServiceBus.Testing(NServiceBus,V6 beta)发布的事件

在我的api控制器中,我发布了一个事件

await _messageSession.Publish<IStrategyCreated>(stgy =>
{
     stgy.StrategyId = strategyToAdd.Id;
     stgy.InvestmentObjective = strategyToAdd.InvestmentObjective;
     stgy.PrincipalInvestmentStrategy = strategyToAdd.PrincipalInvestmentStrategy;
     stgy.PortfolioConsultant = strategyToAdd.PortfolioConsultant;
     stgy.StrategyName = strategyToAdd.Name;
     stgy.StrategyCode = strategyToAdd.Code;
});

在我的测试中,我有:

Assert.That(messageSession.PublishedMessages.Length, Is.EqualTo(1), "Messages published");
Assert.IsInstanceOf<IStrategyCreated>(messageSession.PublishedMessages[0],"Message published was of type IStrategyCreated");

第二行失败,因为邮件类型为IStrategyCreated_impl 错误:

  Message published was of type IStrategyCreated
  Expected: instance of <Strategy.Contracts.Events.IStrategyCreated>
  But was:  <NServiceBus.Testing.PublishedMessage`1[System.Object]>

完整的测试方法:

    public async Task TestCreateStrategy ()
    {

        var messageSession = new TestableMessageSession();
        var dbContent = _container.Resolve<IStrategyDbContext>();
        var apiContext = new StrategyController(messageSession, dbContent);
        var result =
            await apiContext.Create(new StrategyModel {Code = "NEW"}) as
                OkNegotiatedContentResult<StrategyModel>;

        Assert.That(messageSession.PublishedMessages.Length, Is.EqualTo(1), "Messages published");
        Assert.IsInstanceOf<IStrategyCreated>(messageSession.PublishedMessages[0],"Message published was of type IStrategyCreated");
        var stgyCreated =(IStrategyCreated) messageSession.PublishedMessages[0];

        Assert.That(result, Is.Not.Null, "Result Is Null");
        Assert.That(stgyCreated.StrategyCode, Is.EqualTo("NEW"));
        Assert.That(result?.Content.Code, Is.EqualTo("NEW"), "Returning Strategy");
        Assert.That(dbContent.Strategies.Count(), Is.EqualTo(1), "Exactly 1 item was added to strategy list");


    }

1 个答案:

答案 0 :(得分:2)

而不是messageSession.PublishedMessages[0]使用messageSession.PublishedMessages[0].Message,应该工作。