1

2つのファイルからコンテンツを読み取っていますが、期待する文字列を使用してそのコンテンツをテストしたいと思います。

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

string expectedString = "blah";

Assert.AreEqual(read1 and read2 equals expected );

私はこれが基本的であることを知っていますが、私はちょっとここで立ち往生しています。

4

4 に答える 4

4

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.

于 2013-03-12T09:36:32.183 に答える
2

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.

于 2013-03-12T09:36:24.753 に答える
1

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

于 2013-03-12T09:37:47.640 に答える
-1

私があなたを正しければ、あなたはこれを望んでいます:

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

MSDN については、こちらを参照してください。

于 2013-03-12T09:36:41.087 に答える