积极答复者
UnitTest 时候 string.substring()的Bug

问题
答案
-
你好,
你可以做个最简单的测试:
直接在控制台程序中测试SubString,隔离其它影响。
你会发现会报如下错误:
Index and length must refer to a location within the string.
Parameter name: length所以不会出现你说的那种情况。可能有其它因素的影响。如你的TestMethod
Microsoft Online Community Support- 已标记为答案 KeFang Chen 2010年4月29日 7:04
-
很明显是你的代码的问题,而非 MS 的问题。例如一个非常非常非常简单的测试例子
/// <summary> ///A test for Run ///</summary> [TestMethod()] public void RunTest() { string str = "abc"; try { str.Substring(2, 5); } catch (Exception ex) { string error = ex.Message; } //Program.Run(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
得到的是一个 ArgumentOutOfRangeException, Message 是 Index and length must refer to a location within the string.\r\nParameter name: length- 已标记为答案 KeFang Chen 2010年4月29日 7:04
-
我昨天特地回家试了,问题不能重现。所以这个应该不是一个 Issue。
Mark Zhou- 已标记为答案 KeFang Chen 2010年4月29日 7:04
全部回复
-
你好,
你可以做个最简单的测试:
直接在控制台程序中测试SubString,隔离其它影响。
你会发现会报如下错误:
Index and length must refer to a location within the string.
Parameter name: length所以不会出现你说的那种情况。可能有其它因素的影响。如你的TestMethod
Microsoft Online Community Support- 已标记为答案 KeFang Chen 2010年4月29日 7:04
-
很明显是你的代码的问题,而非 MS 的问题。例如一个非常非常非常简单的测试例子
/// <summary> ///A test for Run ///</summary> [TestMethod()] public void RunTest() { string str = "abc"; try { str.Substring(2, 5); } catch (Exception ex) { string error = ex.Message; } //Program.Run(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
得到的是一个 ArgumentOutOfRangeException, Message 是 Index and length must refer to a location within the string.\r\nParameter name: length- 已标记为答案 KeFang Chen 2010年4月29日 7:04
-
我昨天特地回家试了,问题不能重现。所以这个应该不是一个 Issue。
Mark Zhou- 已标记为答案 KeFang Chen 2010年4月29日 7:04