在流利的断言中使用beeGiverentto的意外行为
我对流利的断言有一个问题,这似乎与文档所说的内容有关。 我有一个简单的情况来显示问题。
public class UnitTest1
{
[Fact]
public void Test1()
{
var test = new Test { Name = "Test", Value = "123" };
var testDto = new TestDto { Name = "Test" };
test.Should().BeEquivalentTo(testDto);
}
}
public class Test
{
public string Name { get; set; }
public string Value { get; set; }
}
public class TestDto
{
public string Name { get; set; }
}
我希望该测试将基于“ value”属性在testdto类中不存在的事实失败。
文档指出,根据这句话,我的理解是正确的。
订单对象的所有公共成员都必须在订单上可用。如果缺少任何成员,将会抛出一个例外。
我是错误地理解这一点还是在流利的断言中是一个问题?
I'm having an issue with fluent assertions that seems to go against what the documentation is stating.
I have this simple case to show the problem.
public class UnitTest1
{
[Fact]
public void Test1()
{
var test = new Test { Name = "Test", Value = "123" };
var testDto = new TestDto { Name = "Test" };
test.Should().BeEquivalentTo(testDto);
}
}
public class Test
{
public string Name { get; set; }
public string Value { get; set; }
}
public class TestDto
{
public string Name { get; set; }
}
I would expect that this test would fail based on the fact that the "Value" property doesn't exist on the TestDto class.
The documentation states that my understanding would be correct based on this sentence.
All public members of the Order object must be available on the OrderDto having the same name. If any members are missing, an exception will be thrown.
Am I understanding this incorrectly or is this an issue within Fluent Assertions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是预期的行为。流利的断言通过匹配
testdto
ontest> test
的裸露属性来评估对象图。尝试将顺序倒转,主张按预期失败。
This is expected behavior. Fluent Assertion evaluate the object graph by matching exposed properties of
TestDto
onTest
.Try to invert the order and the assertion fail as expected.
流利的断言的规范说,预期对象的所有公共成员都必须在测试对象上可用,否则将抛出例外:
testObject.should()。beeGiverentTo(enducentObject);
如果您替换了
testdto.should()。peeGiverentto(test);
然后它会按照您的预期工作,它将引发异常。
但是,如果您像您一样替换两个对象,则不会引发异常,因为在这种情况下,所有预期的道具( testdto )都存在于testObject( test em>)。
The Fluent Assertion's specification says that all public members of the expected object must be available on the test object otherwise an exception will be thrown:
testObject.Should().BeEquivalentTo(expectedObject);
If you substitute
testDto.Should().BeEquivalentTo(test);
then it will work as you expected, it will throw the exception.
But if you replace the two object as you did, it will not throw the exception, since in that case all the props of the expected (testDto) are existing in the testObject (test).