1

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

akhil
  • 23
  • 4

2 Answers2

3

Some notes:

  • GetMerchant returns a Task<T>, so you need to await this call.
  • ActionResult<T> supports an implicit cast to T
  • Since Merchant is a class, using Equals will use reference equality. In your particular case, you're assuming that the same instance of Merchant that was passed into the controller is returned. Then you also don't need to use BeOfType.
  • You're mixing FluentAssertions and xUnit assertions.

So, you can rewrite the last part as:

// Act
Merchant result = await MerchantController.GetMerchant(merchantId);

// Assert
result.Should().BeSameAs(merchant);

One final design-related comment. A controller is an implementation detail of an HTTP API. So you really should be testing the HTTP by either using OWIN (in .NET 4.x) or the HostBuilder in .NET Core and later such as is done here.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • I tried this method earlier but got some type of compatibility error message and added it in the question session. This is the link to my repository hope this will give more information to help me [link]https://github.com/Akhil1812007/AmazonAPI-Test -Dennis Doomen. – akhil Sep 19 '22 at 05:04
0

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>();



}
akhil
  • 23
  • 4