【发布时间】:2012-08-17 10:38:16
【问题描述】:
在开发机器上安装 VS2012 Premium 后,单元测试失败,因此开发人员修复了该问题。当更改被推送到 TeamCity 时,单元测试失败了。除了解决方案文件正在升级以兼容VS2012之外,该项目没有变化。它仍然针对 .net framework 4.0
我已将此问题隔离为调用 Uri.ToString 时 unicode 字符被转义的问题。以下代码复制了该行为。
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,则测试成功。
【问题讨论】:
-
我知道逻辑表明我做错了什么并且不会在 .NET 中出现错误,但我无法弄清楚为什么会发生这种情况。
-
大概
bool check = @"http://www.example.org/test?helloworld=foo¶bar" == uri.ToString();是假的吧? -
由于还没有人指出问题所在,我已将其作为连接上的错误提交。 connect.microsoft.com/VisualStudio/feedback/details/758479/…
-
我不会说逻辑表明 .NET 中不会有错误,它们也有问题,例如在装有 VS2010 SP1 的机器上安装 VS2012 时会杀死 SQL CE 接口Visual Studio 中的驱动程序。我会通过安装将其称为错误,但是仍然是错误。这是一个有趣的问题,我目前同意你关于它改变的原因。
-
@ChrisDiver 根据您对连接报告的回复,
foo%B6bar无效。你同意?如果是这样,我认为您可以在此处发布它作为您自己问题的答案。