gpt4 book ai didi

c# - 如何为 NServiceBus Saga 创建单元测试?

转载 作者:太空狗 更新时间:2023-10-29 22:12:54 26 4
gpt4 key购买 nike

我正在尝试按照这篇文章 (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).

最佳答案

正如 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));

}
}

关于c# - 如何为 NServiceBus Saga 创建单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4953752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com