C# 如何模拟NServiceBus的IEndpointInstance
我有下面的控制器,它通过NServiceBus IEndpointInstance(全双工响应/请求解决方案)进行通信。我想测试这个控制器中的验证,所以我需要传递一个IEndpointInstance对象。不幸的是,我能找到的文档中没有提到这一点 在nuget包中,我找到了TestableEndpointInstance类,但我不知道如何使用它 我有下面的测试代码,它可以编译,但当我运行它时它只是挂起。我认为TestableEndpointInstance参数化有问题 有人能帮我举个例子吗 控制器:C# 如何模拟NServiceBus的IEndpointInstance,c#,unit-testing,integration-testing,nservicebus,C#,Unit Testing,Integration Testing,Nservicebus,我有下面的控制器,它通过NServiceBus IEndpointInstance(全双工响应/请求解决方案)进行通信。我想测试这个控制器中的验证,所以我需要传递一个IEndpointInstance对象。不幸的是,我能找到的文档中没有提到这一点 在nuget包中,我找到了TestableEndpointInstance类,但我不知道如何使用它 我有下面的测试代码,它可以编译,但当我运行它时它只是挂起。我认为TestableEndpointInstance参数化有问题 有人能帮我举个例子吗 控制
public CountryController(
IEndpointInstance endpointInstance,
IMasterDataContractsValidator masterDataContractsValidator)
{
this.endpointInstance = endpointInstance;
this._masterDataContractsValidator = masterDataContractsValidator;
}
[HttpPost]
[Route("Add")]
public async Task<HttpResponseMessage> Add([FromBody] CountryContract countryContract)
{
try
{
CountryRequest countryRequest = new CountryRequest();
this._masterDataContractsValidator.CountryContractValidator.ValidateWithoutIdAndThrow(countryContract);
countryRequest.Operation = CountryOperations.Add;
countryRequest.CountryContracts.Add(countryContract);
// nservicebus communication towards endpoint
return message;
}
catch (Exception e)
{
var message = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
return message;
}
}
public CountryControllerTests()
{
TestableEndpointInstance endpointInstance = new TestableEndpointInstance();
// Validator instantiation
this.countryController = new CountryController(endpointInstance, masterDataContractsValidator);
}
[Theory]
[MemberData("CountryControllerTestsAddValidation")]
public async void CountryControllerTests_Add_Validation(
int testId,
CountryContract countryContract)
{
// Given
// When
Func<Task> action = async () => await this.countryController.Add(countryContract);
// Then
action.ShouldThrow<Exception>();
}
公共控制器(
IEndpointInstance endpointInstance,
IMasterDataContractsValidator主数据契约验证器)
{
this.endpointInstance=endpointInstance;
这。_masterDataContractsValidator=masterDataContractsValidator;
}
[HttpPost]
[路线(“添加”)]
公共异步任务添加([FromBody]CountryContract CountryContract)
{
尝试
{
CountryRequest CountryRequest=新CountryRequest();
此.\u masterDataContractsValidator.CountryContractValidator.ValidateWithoutandThrow(countryContract);
countryRequest.Operation=CountryOperations.Add;
countryRequest.CountryContracts.Add(CountryContracts);
//面向端点的nservicebus通信
返回消息;
}
捕获(例外e)
{
var message=this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,e.message);
返回消息;
}
}
测试:
public CountryController(
IEndpointInstance endpointInstance,
IMasterDataContractsValidator masterDataContractsValidator)
{
this.endpointInstance = endpointInstance;
this._masterDataContractsValidator = masterDataContractsValidator;
}
[HttpPost]
[Route("Add")]
public async Task<HttpResponseMessage> Add([FromBody] CountryContract countryContract)
{
try
{
CountryRequest countryRequest = new CountryRequest();
this._masterDataContractsValidator.CountryContractValidator.ValidateWithoutIdAndThrow(countryContract);
countryRequest.Operation = CountryOperations.Add;
countryRequest.CountryContracts.Add(countryContract);
// nservicebus communication towards endpoint
return message;
}
catch (Exception e)
{
var message = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
return message;
}
}
public CountryControllerTests()
{
TestableEndpointInstance endpointInstance = new TestableEndpointInstance();
// Validator instantiation
this.countryController = new CountryController(endpointInstance, masterDataContractsValidator);
}
[Theory]
[MemberData("CountryControllerTestsAddValidation")]
public async void CountryControllerTests_Add_Validation(
int testId,
CountryContract countryContract)
{
// Given
// When
Func<Task> action = async () => await this.countryController.Add(countryContract);
// Then
action.ShouldThrow<Exception>();
}
public CountryControllerTests()
{
TestableEndpointInstance endpointInstance=新的TestableEndpointInstance();
//验证器实例化
this.countryController=新的countryController(endpointInstance、masterDataContractsValidator);
}
[理论]
[成员数据(“CountryControllerTestsAddValidation”)]
公共异步无效CountryControllerTests\u添加\u验证(
int testId,
国家合同(国家合同)
{
//给定
//什么时候
Func action=async()=>等待这个.countryController.Add(countryContract);
//然后
动作。ShouldThrow();
}
我为IEndpointInstance添加了doco
给定控制器
public class MyController
{
IEndpointInstance endpointInstance;
public MyController(IEndpointInstance endpointInstance)
{
this.endpointInstance = endpointInstance;
}
public Task HandleRequest()
{
return endpointInstance.Send(new MyMessage());
}
}
可以用
[Test]
public async Task ShouldSendMessage()
{
var endpointInstance = new TestableEndpointInstance();
var handler = new MyController(endpointInstance);
await handler.HandleRequest()
.ConfigureAwait(false);
var sentMessages = endpointInstance.SentMessages;
Assert.AreEqual(1, sentMessages.Length);
Assert.IsInstanceOf<MyMessage>(sentMessages[0].Message);
}
[测试]
公共异步任务ShouldSendMessage()
{
var endpointInstance=新的TestableEndpointInstance();
var handler=newmycontroller(endpointInstance);
wait handler.HandleRequest()
.配置等待(错误);
var sentMessages=endpointInstance.sentMessages;
AreEqual(1,sentMessages.Length);
Assert.IsInstanceOf(sentMessages[0].Message);
}
我为IEndpointInstance添加了doco
给定控制器
public class MyController
{
IEndpointInstance endpointInstance;
public MyController(IEndpointInstance endpointInstance)
{
this.endpointInstance = endpointInstance;
}
public Task HandleRequest()
{
return endpointInstance.Send(new MyMessage());
}
}
可以用
[Test]
public async Task ShouldSendMessage()
{
var endpointInstance = new TestableEndpointInstance();
var handler = new MyController(endpointInstance);
await handler.HandleRequest()
.ConfigureAwait(false);
var sentMessages = endpointInstance.SentMessages;
Assert.AreEqual(1, sentMessages.Length);
Assert.IsInstanceOf<MyMessage>(sentMessages[0].Message);
}
[测试]
公共异步任务ShouldSendMessage()
{
var endpointInstance=新的TestableEndpointInstance();
var handler=newmycontroller(endpointInstance);
wait handler.HandleRequest()
.配置等待(错误);
var sentMessages=endpointInstance.sentMessages;
AreEqual(1,sentMessages.Length);
Assert.IsInstanceOf(sentMessages[0].Message);
}
为什么不使用Moq或FakeItEasy等模拟框架来创建模拟IENDPointInstance?提供的示例不完整。提供可用于表示问题的。你还需要清楚你想要实现什么。这是单元测试还是集成测试?@Alex:问得好。我没有想到。让我试一试。为什么不使用Moq或FakeItEasy之类的模拟框架来创建一个模拟IENDPointInstance?提供的示例不完整。提供可用于表示问题的。你还需要清楚你想要实现什么。这是单元测试还是集成测试?@Alex:问得好。我没有想到。让我试试。谢谢,我现在的生活好多了!:)不过,通过Moq对象实现IEndpointInstance也很好。谢谢,我现在的生活好多了!:)然而,仅仅通过Moq对象实现IEndpointInstance也可以很好地工作。