asp.net-web-apixunit.netfluent-assertionsfakeiteasy

Xunit.net: Validating model returned by an async ASP.NET method


enter image description hereI have been writing unit testing for the API. I need to check whether values inside the model returned by the action method are expected or not.

[Fact]
public async Task MerchantController_GetMGetProductByMerchantId_Merchant()
{
    //Arrange
    var merchantId = 1000;
    var merchant = new Merchant
    {
        MerchantId = merchantId,
        MerchantEmail = "akhil@gmail.com",
        MerchantName="akhil",
        MerchantPassword="12345",
        ConfirmPassword="12345",
    };
    A.CallTo(() => _merchantRepository.GetMerchantByID(1000)).Returns(merchant);
    var MerchantController = new MerchantController(_merchantRepository);

    //Act
    var result = MerchantController.GetMerchant(merchantId);

    //Assert
    result.Should()
          .BeOfType<Task<ActionResult<Merchant>>>();
   
    Assert.True(merchant.Equals(result));
}

How can I check result.MerchantEmail and akhil@gmail.com are equal in the assert.

controller with the action method GetMerchant

[HttpGet("{id}")]
public async Task<ActionResult<Merchant>> GetMerchant(int id)
{
    try
    {
        return await _repository.GetMerchantByID(id);
    }
    catch
    {
        return NotFound();
    }
}

Repository for the 'GetMerchantByID'

public async Task<Merchant> GetMerchantByID(int MerchantId)
{
    return await _context.Merchants.FindAsync(MerchantId);
}

I need to check the value contained in the merchant model and how I can do it. Merchant model

public class Merchant
{
    [Key]
    public int MerchantId { get; set; }

    [Required(ErrorMessage = "Field can't be empty")]
    [DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]
    public string? MerchantEmail { get; set; }

    public string? MerchantName { get; set; }

    [DataType(DataType.PhoneNumber)]
    [Display(Name = "Phone Number")]

    public string? MerchantPhoneNumber { get; set; }
    [Display(Name = "Please enter password"), MaxLength(20)]
    public string? MerchantPassword { get; set; }
    [NotMapped]

    [Display(Name = "ConfirmPassword")]
    [Compare("MerchantPassword", ErrorMessage = "Passwords don not match")]
    public string? ConfirmPassword { get; set; }
}

can someone help me to find a way if it is possible or suggest some resources


Solution

  • The changes I made are given, to get the value from the return type from the instance of the ActionResult use (ActionResultinstance).Value it will separate the value from return type.

    [Fact]
    public async Task MerchantController_GetMGetProductByMerchantId_Merchant()
    {
        //Arrange
        var merchantId = 1000;
        Merchant merchant = new Merchant
        {
            MerchantId = merchantId,
            MerchantEmail = "akhil@gmail.com",
            MerchantName = "akhil",
            MerchantPassword = "12345",
            ConfirmPassword = "12345",
        };
        A.CallTo(() => _merchantRepository.GetMerchantByID(1000)).Returns(merchant);
        var MerchantController = new MerchantController(_merchantRepository);
    
        //Act
        ActionResult<Merchant> TempResult =await  MerchantController.GetMerchant(merchantId);
        var result = TempResult.Value;
        //Assert
    
        result.Should().BeOfType<Merchant>();
    
    
    
    }