How can I return especific value of a static method in my the method that I am testing?

Viewed 47

Here is the method that I am going to test and I want IsPhoneNomValid() would return false so then I would be able to assert my expectations:

public async Task<UserResponseDto> RegisterUser(RegistrationRequestDto register, CancellationToken cancelationToken)
    {
        // I want the IsPhoneNomValid() method, Would return "FALSE"
        var isPhoneNumberValid = register.PhoneNumber.IsPhoneNomValid();

        if (!isPhoneNumberValid)
            return new UserResponseDto
            {
                Status = new StatusMaker().ErrorStatus("Some Error Message")
            };

        var isActiveAccountPhoneNumberExists = await IsActiveAccountPhoneNumberExist(register.PhoneNumber, cancelationToken);


        if (isActiveAccountPhoneNumberExists.Status == "error")
            return new UserResponseDto
            {
                Status = isActiveAccountPhoneNumberExists
            };

       

    }

RegisterUser_MustReturnPhoneNumberError is my Test method:

public class AccountUserTests
{

    private Mock<IUserService> _userService { get; set; }

    public AccountUserTests()
    {
        _userService = new Mock<IUserService>();
    }

    public async Task RegisterUser_MustReturnPhoneNumberError()
    {
        //Arrang

        // in here I want to setup IsPhoneNomValid() would return false.

        //Act

        //Assert
    }
}

Is there any way that I can test the static methods which are used in my main function which I am testing ?

Here is the IsPhoneNomValid() codes:

public static class Validation
{
    public static bool IsPhoneNomValid(this string phoneNumber)
    {
        //TODO Does it need to be foreign phone numbers ?
        var isMatch = Regex.Match(phoneNumber, @"^09[0-9]{9}$");

        if (isMatch.Success)
            return true;

        return false;
    }
}
1 Answers

You want to test your static method IsPhoneNomValid.

    [Theory]
    [InlineData("123456789")]
    public void TestMethod(string phoneNumber)
    {
        bool isPhoneNumber =Validation.IsPhoneNomValid(phoneNumber);
        Assert.True(isPhoneNumber);
    }

With InlineData, you can test with multiple phone numbers.

Related