我有一个嵌套的class
,FluentAssertions可以声明它们。
然后,将class
更改为struct
,测试失败。
(如果我将IEnumerable<ItemStruct> MyItems { get; set; }
更改为ItemStruct MyItem { get; set; }
,则在两种情况下比较成功。因此,我猜测与IEnumerable有关。)
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace CowsCannotReadLogs.FileHandling.UnitTest
{
[TestClass]
public class TestFluentAssertionStruct
{
public struct DataStruct
{
public IEnumerable<string> MyItems { get; set; }
}
// Fails.
[TestMethod]
public void TestSimpleStruct()
{
var data1 = new DataStruct { MyItems = new[] { "A" } };
var data2 = new DataStruct { MyItems = new[] { "A" } };
data1.Should().BeEquivalentTo(data2);
}
// Fails.
[TestMethod]
public void TestSimpleStructWithComparingByValue()
{
var data1 = new DataStruct { MyItems = new[] { "A" } };
var data2 = new DataStruct { MyItems = new[] { "A" } };
data1.Should().BeEquivalentTo(data2,
options => options.ComparingByValue<DataStruct>());
}
public class DataClass
{
public IEnumerable<string> MyItems { get; set; }
}
// Succeeds.
[TestMethod]
public void TestSimpleClass()
{
var data1 = new DataClass { MyItems = new[] { "A" } };
var data2 = new DataClass { MyItems = new[] { "A" } };
data1.Should().BeEquivalentTo(data2);
}
}
}
答案 0 :(得分:1)
在<object att5="some" att7="other" id="6" name="value">
...
</object>
上使用BeEquivalentTo
时,默认情况下将通过 value语义比较这两个实例。
要比较其成员的结构,可以在本地或全局覆盖默认行为。
在测试内部:
struct
全球范围内[TestMethod]
public void Test()
{
subject.Should().BeEquivalentTo(expected,
opt => opt.ComparingByMembers<DataStruct>());
}
:
[AssemblyInitialize]