question

AmosMatthew-0630 avatar image
0 Votes"
AmosMatthew-0630 asked ·

Why this test will not pass?

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-webapps-vnet
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

markxa avatar image
0 Votes"
markxa answered ·

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.

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for answering my dilemma, Mark.

Even if the test is supposed to throw an Exception, it should indicate a pass or a fail test. Instead the test icon spins blue for a few seconds then fades into a continuous hangup.

0 Votes 0 ·

I suspect the hang is due to the Console.ReadLine() - when you're running a test there's no console for it to read from so it'll just sit there waiting for input that never comes.

0 Votes 0 ·
ryanchill avatar image
0 Votes"
ryanchill answered ·

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




· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am using using Microsoft.VisualStudio.TestTools.UnitTesting;

0 Votes 0 ·