我需要在输出窗口中标记我的替代品,因为现在我有了这个:
Expected: not collection containing <Castle.Proxies.IFormProxy>
But was: < <Castle.Proxies.IFormProxy>, <Castle.Proxies.IFormProxy> >
我想要这个:
Expected: not collection containing <Bad>
But was: < <Good>, <Bad> >
ToString()
是显而易见的方式,但它不起作用(How to substitute Object.ToString using NSubstitute?)
答案 0 :(得分:0)
这是一个老问题,但这是我在遇到问题时所做的事情:
在我的界面中,我添加了ToString()
public interface IRowDetail
{
...
string ToString();
}
创建替代品以返回我的ToString:
var fake = Substitute.For<IRowDetail>();
string message = $"{x} - {y}";
fake.ToString().Returns(message); // this works because it's IRowDetail.ToString()
return fake;
在测试中我创建了一条自定义消息:
Assert.AreEqual(expectedList, orderedList, "Lists differ: " + Diff(expectedList, orderedList.ToList()));
...
private string Diff(List<IRowDetail> expectedList, List<IRowDetail> orderedList)
{
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < expectedList.Count; i++)
{
var o1 = ((IRowDetail)expectedList[i]).ToString(); // this is calling IRowDetail.ToString() which we configured
var o2 = ((IRowDetail)orderedList[i]).ToString();
if(!o1.Equals(o2))
buffer.AppendLine($"Expected {o1} but was {o2} at index {i}.");
}
return buffer.ToString();
}
这是输出:
Lists differ: Expected 1 - 55 but was 1 - 38 at index 1.
Expected 1 - 38 but was 1 - 55 at index 2.
Expected 4 - 44 but was 4 - 39 at index 10.
Expected 4 - 39 but was 4 - 44 at index 11.
也许这也适合你。 欢呼声。