【发布时间】:2016-12-17 19:49:18
【问题描述】:
我将NSubstitute 用于我的NUnit 测试,并尝试检查是否使用正确的值调用了方法。直到知道一切正常。我必须本地化文本,因此为此使用资源字符串。现在每个单元测试都失败了,它测试要接收的方法,其中字符串参数包含一个新行。
这是一个简化的例子:
// Old Version, where the unit test succeed
public void CallWithText(ICallable callable)
{
callable.ShowText("Some text with a new\nline.");
}
// New Version, where the unit test fails
// Text of `Properties.Resources.TextWithNewLine` in the
// Resources.resx is "Some text with a new
// line."
public void CallWithText(ICallable callable)
{
callable.ShowText(Properties.Resources.TextWithNewLine);
}
[Test]
public void CallWithText_WhenCalled_CallsCallable()
{
var caller = new Caller();
var callable = Substitute.For<ICallable>();
caller.CallWithText(callable);
callable.Received(1).ShowText("Some text with a new\nline.");
}
对我来说,新行似乎有问题。有没有人有解决方案,因为调整所有单元测试是一团糟。
【问题讨论】:
-
请发帖minimal reproducible example。还要发布失败的测试的输出。您确定资源文件只包含一个换行符而不包含换行符吗?
-
你是对的(就像@Alexandr Niktin),资源文件包含\r\n,这就是测试失败的原因。
标签: c# string unit-testing nunit nsubstitute