user1765862
user1765862

Reputation: 14165

Asserting very basic

I'm reading content from two files, now I want to test that content with my expected string.

string read1 = File.ReadAllText("@C:\somefile.txt");
string read2 = File.ReadAllText("@C:\somefilee.txt");

string expectedString = "blah";

Assert.AreEqual(read1 and read2 equals expected );

I know this is basic but I'm kinda stuck here.

Upvotes: 1

Views: 306

Answers (4)

Eamon Nerbonne
Eamon Nerbonne

Reputation: 48096

I prefer to use plain C# to write such assertions, which you can with ExpressionToCode (nuget package). With that, your assertion would look as follows:

PAssert.That(
    () => read1 == expectedString && read2 == expectedString
    , "optional failure message");

On a failure, the library will include that expression in it's output, and include the actual values of the various variables (read1, read2, and expectedString) you've used.

For example, you might get a failure that looks as follows:

optional failure message
read1 == expectedString && read2 == expectedString
  |    |        |        |   |    |        |
  |    |        |        |   |    |        "blah"
  |    |        |        |   |    false
  |    |        |        |   "Blah"
  |    |        |        false
  |    |        "blah"
  |    true
  "blah"

Disclaimer: I wrote ExpressionToCode.

Upvotes: 2

Josh Greifer
Josh Greifer

Reputation: 3231

Assert(read1 == read2 && read1 == expectedString, "Not all equal")

Upvotes: 1

bash.d
bash.d

Reputation: 13207

If I get you right, you want this:

try{
if(Assert.AreEqual(read1,read2,false)){
//do things
}
catch(AssertFailedException ex){
//assert failed
}

Look here for MSDN.

Upvotes: -1

Marek Musielak
Marek Musielak

Reputation: 27132

You need to use 2 asserts, first to compare expected string with first file content, and then compare second file content with the first one (or with expected string once again), e.g.:

Assert.AreEqual(expectedString, read1, "File content should be equal to expected string");
Assert.AreEqual(read1, read2, "Files content should be identical");

Or you can use the condition

Assert.IsTrue(read1 == read2 == expectedString, "Files content should be equal to expected string");

But in this case you won't know what was the problem if the test fails.

Upvotes: 4

Related Questions