在下面的简单示例中,我尝试找到一个Should()
使测试通过并失败的断言,就像我在评论中描述的那样。我目前使用result.ShouldBeEquivalentTo(expectedResult, o => o.RespectingRuntimeTypes())
的那个在最后两个测试中没有失败。目标是result
并且excpectedResult
应该始终具有完全相同的类型和值。
using System;
using System.Linq;
using FluentAssertions;
using Xunit;
public class ParserTests
{
// The test that should be failing will be removed once the correct Should() is found
[Theory,
InlineData(DataType.String, "foo", "foo"), // should pass
InlineData(DataType.Integer, "42", 42), // should pass
InlineData(DataType.ByteArray, "1,2,3", new byte[] { 1, 2, 3 }), // should pass
InlineData(DataType.ByteArray, "1,2,3", new byte[] { 3, 2, 1 }), // should fail
InlineData(DataType.ByteArray, "1,2,3", new double[] { 1.0, 2.0, 3.0 }), // should fail, but doesn't
InlineData(DataType.Integer, "42", 42.0d)] // should fail, but doesn't
public void ShouldAllEqual(DataType dataType, string dataToParse, object expectedResult)
{
var result = Parser.Parse(dataType, dataToParse);
result.ShouldBeEquivalentTo(expectedResult, o => o.RespectingRuntimeTypes());
// XUnit's Assert seems to have the desired behavior
// Assert.Equal(result, expectedResult);
}
}
// Simplified SUT:
public static class Parser
{
public static object Parse(DataType datatype, string dataToParse)
{
switch (datatype)
{
case DataType.Integer:
return int.Parse(dataToParse);
case DataType.ByteArray:
return
dataToParse.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(byte.Parse)
.ToArray();
default:
return dataToParse;
}
}
}
public enum DataType
{
String,
Integer,
ByteArray
}