在开发机器上安装 VS2012 Premium 后,单元测试失败,因此开发人员修复了该问题。当更改被推送到 TeamCity 时,单元测试失败了。除了解决方案文件正在升级以兼容VS2012之外,该项目没有变化。它仍然针对 .net 框架 4.0
我已将问题隔离为调用Uri.ToString
. 以下代码复制了该行为。
Imports NUnit.Framework
<TestFixture()>
Public Class UriTest
<Test()>
Public Sub UriToStringUrlDecodes()
Dim uri = New Uri("http://www.example.org/test?helloworld=foo%B6bar")
Assert.AreEqual("http://www.example.org/test?helloworld=foo¶bar", uri.ToString())
End Sub
End Class
在没有安装 VS2012 的机器上在 VS2010 中运行它成功,在安装了 VS2012 的机器上在 VS2010 中运行它失败。两者都使用来自 NuGet 的最新版本的 NCrunch 和 NUnit。
来自失败断言的消息是
Expected string length 46 but was 48. Strings differ at index 42.
Expected: "http://www.example.org/test?helloworld=foo¶bar"
But was: "http://www.example.org/test?helloworld=foo%B6bar"
-----------------------------------------------------^
.NET 4 和 .NET 4.5 的MSDN文档显示ToString
不应编码此字符,这意味着旧的行为应该是正确的。
A String instance that contains the unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %.
安装 VS2012 后,该 unicode 字符被转义。
VS2012机器上System.dll文件版本为4.0.30319.17929
构建服务器上 System.dll 的文件版本为 4.0.30319.236
uri.ToString()
忽略我们为什么使用,我们正在测试什么以及任何潜在的解决方法的优点。谁能解释为什么这种行为似乎已经改变,或者这是一个错误?
编辑,这里是 C# 版本
using System;
using NUnit.Framework;
namespace SystemUriCSharp
{
[TestFixture]
public class UriTest
{
[Test]
public void UriToStringDoesNotEscapeUnicodeCharacters()
{
var uri = new Uri(@"http://www.example.org/test?helloworld=foo%B6bar");
Assert.AreEqual(@"http://www.example.org/test?helloworld=foo¶bar", uri.ToString());
}
}
}
进一步调查,如果我以 .NET 4.0 或 .NET 4.5 为目标,则测试失败,如果我将其切换到 .NET 3.5,则测试成功。