0

This is the following code. Whenever I run this test, it gets stuck on starting... It works fine with [FACT] but not with [Theory]. What could be the reason? What is the solution of it?

    [Theory, AutoMoqData]
    public async Task Login_When_Valid_EmailAndPassword_Returns_SuccessResponse(
    UserVM MoqResponse, 
    string tokenResponse,
    [Frozen] Mock<IAuthenticationManager> _authenticationManager,
    AuthController sut,
    UserVM request)
{ 
    _authenticationManager.Setup(x => x.Authenticate(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(MoqResponse);
    _authenticationManager.Setup(x => x.BuildToken(It.IsAny<UserVM>())).Returns(tokenResponse);

    // Act
    var result = await sut.Login(request);
    var resultObj = result as OkObjectResult;

    // Assert
    Assert.NotNull(result);
    Assert.Equal(200, resultObj.StatusCode);
}
  • 1
    When using the [Theory], do you meet any error? As we all known, the difference between [FACT] and [Theory] attribute is that: The [Fact] attribute is used by the xUnit.net test runner to identify a 'normal' unit test: a test method that takes no method arguments. The [Theory] represents a suite of tests that execute the same code but have different input arguments. When using the [Theory] attribute, we could create a custom AutoMoqData method to provide the parameter list. So, I suppose perhaps the issue is related to the AutoMoqData method. Please check it, or you can post the related code. – Zhi Lv Sep 21 '20 at 09:04
  • You could refer to the following link to use the `[Theory]` attribute and AutoFixture: [Link 1](https://www.c-sharpcorner.com/article/unit-test-in-net-core-application-using-xunit/), [.NET core write better unit tests with Xunit + Autofixture + Moq](https://stackoverflow.com/questions/58197457/) and [An Introduction to XUnit, Moq and AutoFixture](https://engineering.thetrainline.com/an-introduction-to-xunit-moq-and-autofixture-995315f656f). Besides, according to your code, since you are using the Freeze method to generate an item, you put the [Frozen]attribute in front of the parameter. – Zhi Lv Sep 21 '20 at 09:11
  • Thank you @ZhiLv for that explanation. My issue got resolved when I removed the DataAnnotations from my view model. – Bhavya Shah Sep 25 '20 at 06:13
  • public class UserVM { [Key] public Guid UserId { get; set; } //[EmailAddress(ErrorMessage ="Invalid Email!")] public string Email { get; set; } } If I uncomment EmailAddress, the code get stuck, Do you know the reason? – Bhavya Shah Sep 25 '20 at 06:16

0 Answers0