繁体   English   中英

使用 Moq for Task 的 xUnit 测试<iactionresult>类型方法</iactionresult>

[英]xUnit test using Moq for Task<IActionResult> type method

我是 Moq 框架的新手。 在这里,我正在尝试为 mvc 项目中的以下方法编写一个简单的 xUnit 测试。 在示例代码的最后一部分,我添加了我的 xUnit 测试方法,但我不知道如何设置commandMock.Setup 知道我应该在上面设置什么吗?

在此方法上编写的 xUnit 测试:

public async Task<IActionResult> Save(UpdateTermsAndConditionCommand command)
        {
            var response = await Mediator.Send(command);
            return response.Success ? this.SuccessSaveResult(response.Message) : this.FailSaveResult(CommonResources.msgErrorSomthingWentWrong);
        }

更新条款和条件命令 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 classesinterfaces

最简单的方法是在你想要测试和模拟这些的class注入依赖。 您可以先阅读依赖注入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM