0

I have file content like this

XXX,AAAAAA,B,CC;Cont 123456;2.50;1;1;1;2;0;1;l;
XXX,AAAAAA,B,CC;Avboo;1.20;1;1;1;2;0;1;l;
XXX,AAAAAA,B,CC;Dartw esata garle;3.00;1;1;1;2;0;1;ccc; 

file is saved in c:\myFile.txt

in my nunit test I'm trying to create exact strings as is inside file content and after that I want to assert that are equal

[Test]
public void test()
{      
   string line1 = "XXX,AAAAAA,B,CC;Cont 123456;2.50;1;1;1;2;0;1;l;\r\n";
   string line2 = "XXX,AAAAAA,B,CC;Avboo;1.20;1;1;1;2;0;1;l;\r\n";
   string line3 = "XXX,AAAAAA,B,CC;Dartw esata garle;3.00;1;1;1;2;0;1;ccc;\r\n";    
   string expected = string.Concat(line1,line2,line3);
   var fileContent = File.ReadAllText(@"C:\myFile.txt");
   Assert.AreEqual(fileContent, expected);
}

Althout it's looks like that it should be exact strings I'm getting error

Excpected string length 149 but was 154. Strings differ at index 86

9
  • 11
    Did you look at them in the debugger to see what the difference at index 86 is? Commented Mar 13, 2013 at 12:27
  • Have you checked what is coming out of File.ReadAllText()? Commented Mar 13, 2013 at 12:27
  • 1
    You probably need to add a @ char in front of your expected strings, i.e. string line1 = @"XXX..."; Commented Mar 13, 2013 at 12:28
  • Have you tried to remove \r ? What is the encoding of myFile.txt ? Commented Mar 13, 2013 at 12:28
  • you could write multiline text without anything, just use + at the end, example: string expected = "..." + (next line) "...". Commented Mar 13, 2013 at 12:38

6 Answers 6

4

What if you remove the last line break:

 string line3 = "XXX,AAAAAA,B,CC;Dartw esata garle;3.00;1;1;1;2;0;1;ccc;";

I think the end of your text file is not a line break.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the most possible reason for difference in length. And regarding mismatch, it is that strange symbol (letter?) l which may looks like digit 1 and you may have been mistyped it.
This is a good and correct answer, specific to an error in the test. But doesn't really address the core issue - stackoverflow.com/questions/15385301/…
2

Use Environment.NewLine instead of \r\n.

Comments

1

The message you got is explicit:

Expected string length 149 but was 154. Strings differ at index 86

It points at the \r\n location ; So I suspect your file contains \n and not \r and \n together.

                                  position 86: \r found |
                                                        v
...;1;1;1;2;0;1;l;XXX,AAAAAA,B,CC;Avboo;1.20;1;1;1;2;0;1;l;XXX,AAAAAA,B,CC;
  • For this reason, the file comparaison differs because there is a unexpected \r at the position 86.

So you have now 3 extra chars.

Because your text file does probably not end with \r\n, now you have one more extra chars:

  • That's the reason why you have a total of 5 extra chars (4 + an extra char I did not spot yet) : 154 instead of 149.

Comments

0

Another approach.

var actual = File.ReadAllLines(@"c:\abc.txt");
var expected = new List<string>();
expected.Add("line1");
expected.Add("line2");
expected.Add("line3");
Assert.AreEqual(expected, actual);

Or you can do a for loop on the list and check each line one by one.

Comments

0

You have an extra NewLineChar in line3, the following code works correctly:

[Test]
public void test()
{
    string line1 = "XXX,AAAAAA,B,CC;Cont 123456;2.50;1;1;1;2;0;1;l;\r\n";
    string line2 = "XXX,AAAAAA,B,CC;Avboo;1.20;1;1;1;2;0;1;l;\r\n";
    string line3 = "XXX,AAAAAA,B,CC;Dartw esata garle;3.00;1;1;1;2;0;1;ccc;";
    string expected = string.Concat(line1, line2, line3);
    var fileContent = File.ReadAllText(@"C:\myFile.txt");
}

Also you can add an extra NewLineChar in your myfile.txt

Comments

0

There are lots of good answers on here as to how better do the test. But in fact there are two fundemental issues with the question.

  1. it's not a unit test, it's an integration test
  2. If you don't know what the .NET String output of the file is in the first place, the test is pointless. You need to find this out first.

To do this I would first debug the test and copy the output of File.ReadAllText("[filename]"); Then paste this output into your Assert. method/s.

One of the main keys to unit / integration testing is to know exactly what both sides of your test are. Without this, you might as well not bother.

Also, what are you testing?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.