Why this test will not pass?

Amos Matthew 26 Reputation points
2020-06-12T01:48:19.733+00:00

Working on this test for days now. What am I doing wrong?

public long Add_PhoneNumber(string v)
{
    Console.WriteLine(v);
    string line = Console.ReadLine();

    if (line.Length > 10 )

        throw new ArgumentOutOfRangeException("Too many numbers");

    if (line.Length < 10)
        throw new ArgumentOutOfRangeException("Too few numbers");

    return (long.Parse(line));

}

public void Passing_AddPhoneNumber()
{

    // Arrange
    long expected_Number = 1231231234;
    Customer cust1 = new Customer("Amos", "Matthew", expected_Number);

    // Act
    long actualNumber = cust1.Add_PhoneNumber("12312312345");
    Assert.AreNotSame(expected_Number, cust1.Add_PhoneNumber("12312312345"));

}
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,830 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mark Allan 416 Reputation points MVP
    2020-06-12T06:55:14.94+00:00

    cust1.Add_PhoneNumber("12312312345") is passing in a string that's 11 characters long, so it'll be throwing at throw new ArgumentOutOfRangeException("Too many numbers");, not returning a value. More generally speaking, don't forget that you can debug and step through unit tests which should help to find problems like this.


1 additional answer

Sort by: Most helpful
  1. Ryan Hill 25,476 Reputation points Microsoft Employee
    2020-06-12T04:48:58.987+00:00

    First thing sticking out to me is that even though you're passing "12312312345" into cust1.Add_PhoneNumber, string line isn't equal to that. I'm not certain a testing framework like mstest; as I'm not sure which one you're using, will allow you text input during the run a test harness. Remove the Console.ReadLine(), it's not needed since you're passing in the entry as an argument.

    Hope this helps.

    Regards,
    Ryan