【问题标题】:How to create unit tests for NServiceBus Saga?如何为 NServiceBus Saga 创建单元测试?
【发布时间】:2011-02-10 05:37:24
【问题描述】:

我正在尝试按照这篇文章 (http://blog.zoolutions.se/post/2010/04/01/Conquering-NServiceBus-part-4-e28093-Testing.aspx) 为我的 nservicebus 创建单元测试传奇项目

看下面的代码,不知道为什么总是报错 有人知道我该如何解决吗?

(我使用的是 nservice bus 2.0)

public class ReportSaga : Saga<ReportSagaData>,
                          IAmStartedByMessages<RequestReportMessage>,
                          IHandleMessages<PollingReportStatusMessage>
{
// implementation
}



[TestFixture]
    public class ReportSaga_HandleRequestReportMessageTests
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            var assemblies = new[]
                         {
                             typeof (ReportSaga).Assembly,
                             typeof (RequestReportMessage).Assembly,
                             typeof (PollingReportStatusMessage).Assembly,
                             Assembly.Load("NServiceBus"),
                             Assembly.Load("NServiceBus.Core")
                         };

            Test.Initialize(assemblies);
        }

        [Test]
        public void HandleRequestReportMessageTests()
        {

            Test.Handler<ReportSaga>()
                .OnMessage<RequestReportMessage>(x =>
                {
                    x.Id = 1234;
                    x.ReportDate = DateTime.Now;
                });


        }
    }


Test 'UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests' failed: System.ArgumentException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type 'T'.
  ----> System.TypeLoadException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type parameter 'T'.
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)
    at NServiceBus.Testing.Test.Handler[T](T handler)
    at NServiceBus.Testing.Test.Handler[T]()
    ReportSaga_HandleRequestReportMessageTests.cs(34,0): at UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests()
    --TypeLoadException
    at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
    at System.RuntimeTypeHandle.Instantiate(Type[] inst)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)

0 passed, 1 failed, 0 skipped, took 1.11 seconds (NUnit 2.5.5).

【问题讨论】:

    标签: c# unit-testing nservicebus


    【解决方案1】:

    正如 Udi 所说,但是语法应该是这样的:

    [TestFixture]
    public class ReportSaga_HandleRequestReportMessageTests
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            var assemblies = new[]
                         {
                             typeof (ReportSaga).Assembly,
                             typeof (RequestReportMessage).Assembly,
                             typeof (PollingReportStatusMessage).Assembly,
                             Assembly.Load("NServiceBus"),
                             Assembly.Load("NServiceBus.Core")
                         };
    
            Test.Initialize(assemblies);
        }
    
        [Test]
        public void HandleRequestReportMessageTests()
        {
    
            var message = new RequestReportMessage { Id = 1234, ReportDate = DateTime.Now };
    
            Test.Saga<ReportSaga>()
                .ExpectPublish<PublishMessage>(e => e.SomePropertyOfPublishMethod == "value")
                .When(x => x.Handle(message));
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      为了测试一个 saga,你需要调用 Test.Saga 而不是 Test.Handler。

      【讨论】:

        【解决方案3】:

        这里有一个包含一些示例的页面 => https://docs.particular.net/samples/unit-testing/

        沿用上面页面中的 1 个示例,稍作修改:

        [Test]
        public async Task ShouldProcessDiscountOrder()
        {
            // Arrange
            var saga = new YourSaga
            {
                Data = new YourSagaData()
            };
        
            var context = new TestableMessageHandlerContext();
        
            var yourCommand = new MyCommandOrEvent
            {
                propA = 1
            };
        
            // Act
            await saga.Handle(yourCommand, context)
                .ConfigureAwait(false);
        
            // Assert
            var processMessage = (OutputTypeReturnedByHandle)context.SentMessages[0].Message;
            Assert.AreEqual(123, processMessage.Something);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-21
          相关资源
          最近更新 更多