简体   繁体   中英

xUnit test using Moq for Task<IActionResult> type method

I am new to Moq framework. Here i am trying to write a simple xUnit test for following method in mvc project. on last part of example code i added my xUnit test method where i am not getting idea how to setup the commandMock.Setup . Any idea what should i setup on it?

xUnit test to be written on this method:

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; }
    }

Mediator 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 Test using Moq:

  [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

        }

The UpdateTermsAndConditionCommand is a class which you can instantiate ( var command = new UpdateTermsAndConditionCommand { } ) and cannot be mocked.

Only abstract classes and interfaces can be mocked.

The easiest way is to inject dependencies in the class you want to test and mock these. You can start by reading up on Dependency Injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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