C# NServiceBus 2.5.0.1442单元测试总线.CreateInstance<;界面>;在某些单元测试中返回null
我在使用Sagas进行NServiceBus单元测试时遇到了奇怪的行为 运行C# NServiceBus 2.5.0.1442单元测试总线.CreateInstance<;界面>;在某些单元测试中返回null,c#,unit-testing,nservicebus,C#,Unit Testing,Nservicebus,我在使用Sagas进行NServiceBus单元测试时遇到了奇怪的行为 运行 NSB 2.5.0.1442(由于IMessage而卡在此处,并且它处于多个服务中) NServiceBus.测试 Resharper 8.2单元测试转轮 VS2013更新最新rtm版本/软件包/内容 我的解决方案中有三个单元测试项目。其中只有一个引用了NServiceBus.Testing,因为它是唯一一个测试传奇的 消息。测试 服务测试(此项参考NSB测试) ViewModelUpdater.Testing 单
- NSB 2.5.0.1442(由于IMessage而卡在此处,并且它处于多个服务中)
- NServiceBus.测试
- Resharper 8.2单元测试转轮
- VS2013更新最新rtm版本/软件包/内容
var command = Bus.CreateInstance<IEmailMessage>(a =>
{
a.ToAddress = toAddresses;
a.FromAddress = _formServiceEmailAddress;
a.Subject = emailSubject;
a.Body = body.ToString();
a.IsHtmlBody = true;
});
if (command == null)
{
throw new Exception("Email Message Failed to Create");
}
Bus.Send(command);
是否可能“CreateInstance”已被代理,不再工作?在我的故事中,我没有在任何地方使用Bus.CreateInstance,但是这个IEMailMessage是唯一的接口(我们所有的服务都使用它来发送到标准的电子邮件发送者服务)
我刚刚修改了另一条消息以使用Bus.CreateInstance,但这次是在一个对象上,并且遇到了相同的问题
我把同样的东西改成了Bus.Send,但没有收到同样的问题
我现在正在将我的原始代码更新为
Bus.Send<IEmailMessage>(a =>
{
a.ToAddress = toAddresses;
a.FromAddress = _formServiceEmailAddress;
a.Subject = emailSubject;
a.Body = body.ToString();
a.IsHtmlBody = true;
});
因此,当所有测试项目都运行时,我们仍然会遇到一个问题。我想,如果我更改预期的顺序,那么在运行所有测试时,测试可能会工作,但单独运行时,它将失败
当更改IEmailMessage预期的顺序时,无论我将其放置在顺序中的什么位置(甚至在发布预期之间),我都会收到此错误消息
System.InvalidCastException : Unable to cast object of type 'CreateFormSummary' to type 'IEmailMessage'.
所以现在我的问题似乎不是它是一个接口。我将用一个具有相同属性的本地类替换IEmailMessage
创造这个
public class EmailMessage : IEmailMessage
{
public string BCCAddress { get; set; }
public string Body { get; set; }
public string CCAddress { get; set; }
public string FromAddress { get; set; }
public bool IsHtmlBody { get; set; }
public string Subject { get; set; }
public string ToAddress { get; set; }
}
//replaced IEmailMessage with concrete
Bus.Send<EmailMessage>(a =>
{
a.ToAddress = toAddresses;
a.FromAddress = _formServiceEmailAddress;
a.Subject = emailSubject;
a.Body = body.ToString();
a.IsHtmlBody = true;
});
//Run All Unit tests
//output of this unit test
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
IEmailMessage
ExpectedReceipientIs__["Manager@domain.com"]
Saga正在调用两个InstanceBus.CreateInstance
解决方案
创建接口的实现,然后不使用它。解决方案
创建一个接口的实现,然后不使用它。我所能建议的就是使用一个更新的版本。自v2.5以来,已经修复了很多错误,我们实在不可能支持它。2年前发布的版本3.2.8是我们目前仍在修补的最早版本。感谢Udi,我们正在计划升级到目前的NSB 4.5。我会更新结果。正在与金伯利讨论许可问题。
InitiatedTestSaga(command)
.ExpectPublish<FinanceUserAccessFormSubmitted>(m => CompareSubmitted(m,command) )
.ExpectPublish<AssignedToManager>(m =>
{
Console.WriteLine("AssignedToManager");
return m.FormId == command.FormId;
})
.ExpectSend<CreateFormSummary>(m =>
{
Console.WriteLine("CreateFormSummary");
return m.FormId == command.FormId;
})
.ExpectSend<CreateFormHistory>(m =>
{
Console.WriteLine("CreateFormHistory");
return m.FormId == command.FormId;
})
.ExpectSend<UpdateFormSummary>(m =>
{
Console.WriteLine("UpdateFormSummary");
return m.FormId == command.FormId;
})
.ExpectSend<IEmailMessage>(m =>
{
Console.WriteLine("IEmailMessage");
return TestCommands.ExpectedReceipientIs(m, command.FormDetails.ManagerForApproval.EmailAddress);
})
.ExpectReply<CompletionMessage>(m => m.ErrorCode == (int) ResponseCode.Success)
.When(s => s.Handle(command));
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
Rhino.Mocks.Exceptions.ExpectationViolationException : IBus.Send(callback method: <>c__DisplayClass2`1.<ExpectSend>b__1); Expected #1, Actual #0.
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
IEmailMessage
ExpectedReceipientIs__["Manager@domain.com"]
System.InvalidCastException : Unable to cast object of type 'CreateFormSummary' to type 'IEmailMessage'.
public class EmailMessage : IEmailMessage
{
public string BCCAddress { get; set; }
public string Body { get; set; }
public string CCAddress { get; set; }
public string FromAddress { get; set; }
public bool IsHtmlBody { get; set; }
public string Subject { get; set; }
public string ToAddress { get; set; }
}
//replaced IEmailMessage with concrete
Bus.Send<EmailMessage>(a =>
{
a.ToAddress = toAddresses;
a.FromAddress = _formServiceEmailAddress;
a.Subject = emailSubject;
a.Body = body.ToString();
a.IsHtmlBody = true;
});
//Run All Unit tests
//output of this unit test
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
IEmailMessage
ExpectedReceipientIs__["Manager@domain.com"]
private void CreateAndSendEmail(string[] emailAddresses, string emailSubject, StringBuilder body)
{
string toAddresses = String.Join(",", emailAddresses);
var msg = Bus.CreateInstance<IEmailMessage>(a=>
{
a.ToAddress = toAddresses;
a.FromAddress = _formServiceEmailAddress;
a.Subject = emailSubject;
a.Body = body.ToString();
a.IsHtmlBody = true;
}) ;
if (msg == null)
{
throw new Exception("msg is null");
}
Bus.Send(msg);
}
//Run All Unit Tests
//output is this
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
IEmailMessage
ExpectedReceipientIs__
["Manager@wesleymission.org.au"]
AssignedToManager
CreateFormSummary
CreateFormHistory
UpdateFormSummary
System.Exception : msg is null