简体   繁体   English

.NET Webhandler测试字符串相等性比较失败

[英].NET Webhandler Test string equality comparison fails

I am currently writing tests for my .NET application. 我目前正在为.NET应用程序编写测试。 To test a webhandler, I am calling it with a webclient and am planning to compare it with a string coming from a file, which I previously have written. 为了测试Web处理程序,我将使用Web客户端对其进行调用,并计划将其与来自我先前编写的文件中的字符串进行比较。

I am storing the expected string in a file, because the WebHandler is generating a html-document, which I am returning as text/plain. 我将预期的字符串存储在文件中,因为WebHandler正在生成一个html文档,我将其作为文本/纯文本返回。

This is the code I am using currently: 这是我当前正在使用的代码:

private string getWebResponse(string projectName)
{
    string url = "http://webhandler.ashx?parameter={0}";
    WebClient client = new WebClient();
    return client.DownloadString(string.Format(url, projectName));
}

[TestMethod()]
public void ProcessRequestTest1()
{
    string expected = File.ReadAllText(Path.Combine(projectRootFolder, "testresults", "Test (1).txt"), Encoding.UTF8);
    Assert.AreEqual(expected, getWebResponse("Test (1)"));
}

When running this testcase, the test fails even though the strings returned appear to be identical (as I have tested both of them with a diff-tool). 运行此测试用例时,即使返回的字符串似乎相同(如我已经使用diff-tool测试了它们),测试仍然失败。 Is there an issue that I am missing when gathering the strings? 收集字符串时是否缺少我的问题?

The issue were the newline characters. 问题是换行符。 My webhandler defined newlines as \\n . 我的网络处理程序将换行符定义为\\n Copy-Pasting the result from my handler into a file lead me to a different string as windows is using \\r\\n for linebreaks. 将处理程序中的结果复制粘贴到文件中后,由于Windows使用\\r\\n作为换行符,导致我输入了另一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM