C# Visual Studio 2013:测试/断言字符串

C# Visual Studio 2013:测试/断言字符串,c#,string,testing,visual-studio-2013,C#,String,Testing,Visual Studio 2013,我有个问题。我正在测试一个lib,它以xml样式生成一些文本。到目前为止,我正在测试这个函数 Assert.AreEqual(string1, string2); 但是xml样式的字符串长度超过300个字符。当我在一个字符中犯了一个小错误时,测试失败了,结果是字符串不相等。但测试并没有说明,在哪个位置它们是不相等的 所以我的问题是:是否已经有一个实现的函数,它比较两个字符串并告诉我它们在哪个位置不同+字符串的输出 这样试试 var indexBroke = 0; var maxLength

我有个问题。我正在测试一个lib,它以xml样式生成一些文本。到目前为止,我正在测试这个函数

 Assert.AreEqual(string1, string2);
但是xml样式的字符串长度超过300个字符。当我在一个字符中犯了一个小错误时,测试失败了,结果是字符串不相等。但测试并没有说明,在哪个位置它们是不相等的

所以我的问题是:是否已经有一个实现的函数,它比较两个字符串并告诉我它们在哪个位置不同+字符串的输出

这样试试

var indexBroke = 0;
var maxLength = Math.Min(string1.Length, string2.Length);
while (indexBroke < maxLength && string1[indexBroke] == string2[indexBroke]) {
   indexBroke++;
}
return ++indexBroke;
var indexBroke=0;
var maxLength=Math.Min(string1.Length,string2.Length);
而(indexBroke
逻辑是,您一步一步地比较每个字符,当您得到第一个差异时,函数exit返回最后一个索引,其中包含相等的字符

var indexBroke = 0;
var maxLength = Math.Min(string1.Length, string2.Length);
while (indexBroke < maxLength && string1[indexBroke] == string2[indexBroke]) {
   indexBroke++;
}
return ++indexBroke;
var indexBroke=0;
var maxLength=Math.Min(string1.Length,string2.Length);
而(indexBroke
逻辑是,您一步一步地比较每个字符,当您得到第一个差异时,函数exit返回最后一个索引,其中包含相等的字符

var indexBroke = 0;
var maxLength = Math.Min(string1.Length, string2.Length);
while (indexBroke < maxLength && string1[indexBroke] == string2[indexBroke]) {
   indexBroke++;
}
return ++indexBroke;
var indexBroke=0;
var maxLength=Math.Min(string1.Length,string2.Length);
而(indexBroke
逻辑是,您一步一步地比较每个字符,当您得到第一个差异时,函数exit返回最后一个索引,其中包含相等的字符

var indexBroke = 0;
var maxLength = Math.Min(string1.Length, string2.Length);
while (indexBroke < maxLength && string1[indexBroke] == string2[indexBroke]) {
   indexBroke++;
}
return ++indexBroke;
var indexBroke=0;
var maxLength=Math.Min(string1.Length,string2.Length);
而(indexBroke
逻辑是,您一步一步地比较每个字符,当您得到第一个差异时,函数exit返回最后一个具有相同字符的索引。出于这个原因(以及许多其他原因),我建议使用

使用FluentAssertions,您可以这样表述您的断言:

string1.Should().Be(string2);
如果字符串不匹配,您会收到一条信息丰富的消息,帮助您解决问题:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28, but 
"<p>Line one<br>Line two</p>" has a length of 27.
这将给你以下信息:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28 because a multiline-input should have been successfully parsed, but 
"<p>Line one<br>Line two</p>" has a length of 27.
所需的字符串为
“第1行
第2行

”的长度为28,因为多行输入本应已成功解析,但 “第一行
第二行

”的长度为27。
当比较本身没有意义的值(例如布尔值和数字)时,这些原因参数尤其有价值

顺便说一句,FluentAssertions在比较对象图方面也有很大帮助。

出于这个原因(以及其他许多原因),我建议使用

使用FluentAssertions,您可以这样表述您的断言:

string1.Should().Be(string2);
如果字符串不匹配,您会收到一条信息丰富的消息,帮助您解决问题:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28, but 
"<p>Line one<br>Line two</p>" has a length of 27.
这将给你以下信息:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28 because a multiline-input should have been successfully parsed, but 
"<p>Line one<br>Line two</p>" has a length of 27.
所需的字符串为
“第1行
第2行

”的长度为28,因为多行输入本应已成功解析,但 “第一行
第二行

”的长度为27。
当比较本身没有意义的值(例如布尔值和数字)时,这些原因参数尤其有价值

顺便说一句,FluentAssertions在比较对象图方面也有很大帮助。

出于这个原因(以及其他许多原因),我建议使用

使用FluentAssertions,您可以这样表述您的断言:

string1.Should().Be(string2);
如果字符串不匹配,您会收到一条信息丰富的消息,帮助您解决问题:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28, but 
"<p>Line one<br>Line two</p>" has a length of 27.
这将给你以下信息:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28 because a multiline-input should have been successfully parsed, but 
"<p>Line one<br>Line two</p>" has a length of 27.
所需的字符串为
“第1行
第2行

”的长度为28,因为多行输入本应已成功解析,但 “第一行
第二行

”的长度为27。
当比较本身没有意义的值(例如布尔值和数字)时,这些原因参数尤其有价值

顺便说一句,FluentAssertions在比较对象图方面也有很大帮助。

出于这个原因(以及其他许多原因),我建议使用

使用FluentAssertions,您可以这样表述您的断言:

string1.Should().Be(string2);
如果字符串不匹配,您会收到一条信息丰富的消息,帮助您解决问题:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28, but 
"<p>Line one<br>Line two</p>" has a length of 27.
这将给你以下信息:

Expected string to be 
"<p>Line one<br/>Line two</p>" with a length of 28 because a multiline-input should have been successfully parsed, but 
"<p>Line one<br>Line two</p>" has a length of 27.
所需的字符串为
“第1行
第2行

”的长度为28,因为多行输入本应已成功解析,但 “第一行
第二行

”的长度为27。
当比较本身没有意义的值(例如布尔值和数字)时,这些原因参数尤其有价值


顺便说一句,FluentAssertions在比较对象图方面也有很大帮助。

看看这个SO问题:看看这个SO问题:看看这个SO问题:看看这个SO问题: