使用 Moq for Task<IActionResult> 类型方法的 xUnit 测试
xUnit test using Moq for Task<IActionResult> type method
我是 Moq 框架的新手。在这里,我正在尝试为 mvc 项目中的以下方法编写一个简单的 xUnit 测试。在示例代码的最后一部分,我添加了我的 xUnit 测试方法,但我不知道如何设置 commandMock.Setup
。知道我应该在上面设置什么吗?
x要写在这个方法上的单元测试:
public async Task<IActionResult> Save(UpdateTermsAndConditionCommand command)
{
var response = await Mediator.Send(command);
return response.Success ? this.SuccessSaveResult(response.Message) : this.FailSaveResult(CommonResources.msgErrorSomthingWentWrong);
}
UpdateTermsAndConditionCommand class:
public class UpdateTermsAndConditionCommand : IRequest<ResponseDetail>
{
public long Id { get; set; }
[Display(Name = "labelEnglishTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgEnglishTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string EngTermsAndCondition { get; set; }
[Display(Name = "labelDutchTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgDutchTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string DutchTermsAndCondition { get; set; }
[Display(Name = "labelGermanTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgGermanTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string GermanTermsAndCondition { get; set; }
}
调解员class:
[XSSFilter]
public abstract class BaseController<T> : Controller
{
private ISender _mediator;
private ILogger<T> _logger;
protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetService<ISender>();
protected ILogger<T> Logger => _logger ??= HttpContext.RequestServices.GetService<ILogger<T>>();
[HttpPost]
public ActionResult Pdf_Export_Save(string contentType, string base64, string fileName)
{
var fileContents = Convert.FromBase64String(base64);
return File(fileContents, contentType, fileName);
}
public async Task<List<LanguageListItemDto>> GetLanguages()
{
var response = await Mediator.Send(new GetLanguageListQuery());
return response.Languages;
}
}
xUnit 测试使用最小起订量:
[Fact]
public void SaveXUnit()
{
//arrange
var commandMock = new Mock<UpdateTermsAndConditionCommand>();
var controller = new TermsandConditionController();
commandMock.Setup(x => x.Execute());//here i am not getting idea how to setup the command
//act
//assert
}
UpdateTermsAndConditionCommand
是一个 class
,您可以实例化它 (var command = new UpdateTermsAndConditionCommand { }
),不能 被模拟。
只能模拟 abstract classes
和 interfaces
。
最简单的方法是在您想要测试和模拟的class
中注入 依赖项。您可以先阅读 Dependency Injection
我是 Moq 框架的新手。在这里,我正在尝试为 mvc 项目中的以下方法编写一个简单的 xUnit 测试。在示例代码的最后一部分,我添加了我的 xUnit 测试方法,但我不知道如何设置 commandMock.Setup
。知道我应该在上面设置什么吗?
x要写在这个方法上的单元测试:
public async Task<IActionResult> Save(UpdateTermsAndConditionCommand command)
{
var response = await Mediator.Send(command);
return response.Success ? this.SuccessSaveResult(response.Message) : this.FailSaveResult(CommonResources.msgErrorSomthingWentWrong);
}
UpdateTermsAndConditionCommand class:
public class UpdateTermsAndConditionCommand : IRequest<ResponseDetail>
{
public long Id { get; set; }
[Display(Name = "labelEnglishTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgEnglishTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string EngTermsAndCondition { get; set; }
[Display(Name = "labelDutchTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgDutchTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string DutchTermsAndCondition { get; set; }
[Display(Name = "labelGermanTermsAndCondition", ResourceType = typeof(TermsAndConditionResources))]
[XSSIgnore]
[Required(ErrorMessageResourceName = "msgGermanTermsConditionRequire", ErrorMessageResourceType = typeof(TermsAndConditionResources))]
public string GermanTermsAndCondition { get; set; }
}
调解员class:
[XSSFilter]
public abstract class BaseController<T> : Controller
{
private ISender _mediator;
private ILogger<T> _logger;
protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetService<ISender>();
protected ILogger<T> Logger => _logger ??= HttpContext.RequestServices.GetService<ILogger<T>>();
[HttpPost]
public ActionResult Pdf_Export_Save(string contentType, string base64, string fileName)
{
var fileContents = Convert.FromBase64String(base64);
return File(fileContents, contentType, fileName);
}
public async Task<List<LanguageListItemDto>> GetLanguages()
{
var response = await Mediator.Send(new GetLanguageListQuery());
return response.Languages;
}
}
xUnit 测试使用最小起订量:
[Fact]
public void SaveXUnit()
{
//arrange
var commandMock = new Mock<UpdateTermsAndConditionCommand>();
var controller = new TermsandConditionController();
commandMock.Setup(x => x.Execute());//here i am not getting idea how to setup the command
//act
//assert
}
UpdateTermsAndConditionCommand
是一个 class
,您可以实例化它 (var command = new UpdateTermsAndConditionCommand { }
),不能 被模拟。
只能模拟 abstract classes
和 interfaces
。
最简单的方法是在您想要测试和模拟的class
中注入 依赖项。您可以先阅读 Dependency Injection