我正在尝试比较具有多个属性的两个对象,但需要使用谓词来比较特定属性(object1在 处没有这些属性的确切值object2,因此我需要在那里比较部分字符串)。
所以,我正在尝试:
object1.Should().BeEquivalentTo(object2, options => options
.Including(o => o.Property1.StartsWith("something"))
.Including(o => o.Property2.StartsWith("something else")
);
Run Code Online (Sandbox Code Playgroud)
我希望所有其他属性都能像往常一样进行比较。
然而,运行上面的代码会抛出异常:
消息:System.ArgumentException:表达式
<Convert(o.Property1.StartsWith("something"), Object)>不能用于选择成员。参数名称:表达式
我检查了文档,它的示例与我的示例相同(https://fluidassertions.com/objectgraphs/上的“选择成员”一章)。
为什么会出现此异常以及如何修复它?
我目前正在使用FluentAssertion比较 2 个对象。
我很想知道它是用什么方式来比较的?
\n\n使用Reflection像这样
public static void PropertyValuesAreEquals(object actual, object expected) \n{\n PropertyInfo[] properties = expected.GetType().GetProperties();\n foreach (PropertyInfo property in properties)\n {\n object expectedValue = property.GetValue(expected, null);\n object actualValue = property.GetValue(actual, null);\n if (!Equals(expectedValue, actualValue))\n Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue);\n //\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6.\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果用另一种方式来比较,那是什么呢?
\n在BDD测试中,我正在比较日期.比较日期时以字符串形式.即使两个日期都相同,我也会收到此消息并且测试失败
预期对象为"01/20/2012 12:00:00 AM",但发现"1/20/2012 12:00:00 AM".
还有一件事就是在我的系统上发生这种情况.如果我要求其他开发人员进行测试,测试通过正常.我缺少某种类型的设置吗?
它的代码部分是
customer["DateOfBirth"].Should().Be(Helper.DateOfBirth.ToString());
Run Code Online (Sandbox Code Playgroud)
客户是哈希表的地方.这个特别声明在其他机器上通过OK但不在我的机器上.
我知道我可以通过更改日期比较而不是字符串比较来修复它.但我很好奇,因为这在其他机器上很好.
我有以下Fluent Assertion,我想在if语句中添加.我收到一个错误,说我不能隐式地将类型转换为bool.
我试图明确地转换它但我仍然得到一个错误,说不能将类型转换为bool.
actors.Cast.Should().Contain(actor => actor.Name == "Emilia Clark");
检查上述陈述是否属实的最佳方法是什么?
我想通过FluentAssertion语法检查方法的返回值。请考虑以下片段:
public interface IFoo
{
Task<int> DoSomething();
}
public class Bar
{
private readonly IFoo _foo;
private static int _someMagicNumber = 17;
public Bar(IFoo foo)
{
_foo = foo;
}
public async Task<int> DoSomethingSmart()
{
try
{
return await _foo.DoSomething();
}
catch
{
return _someMagicNumber;
}
}
}
[TestFixture]
public class BarTests
{
[Test]
public async Task ShouldCatchException()
{
// Arrange
var foo = Substitute.For<IFoo>();
foo.DoSomething().Throws(new Exception());
var bar = new Bar(foo);
Func<Task> result = () => bar.DoSomethingSmart(); …Run Code Online (Sandbox Code Playgroud) 我正在 Visual Studio 2019 中编写 C# 单元测试。我使用 Xunit 和 FluentAssertion。
其中一项测试中的两行主要代码(出于说明目的)是:
Action a = () => new SomeClass(null);
a.Should().Throw<ArgumentNullException>();
Run Code Online (Sandbox Code Playgroud)
但是,这会导致编译器 CA1806 警告:“Ctor_WhenInvalidArgs_ThenThrowException 创建了从未使用过的 SomeClass 的新实例。将该实例作为参数传递给另一个方法,将该实例分配给变量,或者在不需要时删除对象创建。”
如何解决警告,同时仍然使用 FluentAssertion 以确保构造函数抛出正确的异常?
使用 FluentAssertions 6,您似乎可以更长地验证对象图中的 Enum 是否相当于字符串。来源:https :// Fluentassertions.com/upgradingtov6
enum MyEnum {
A,
B
}
class Source {
MyEnum Enum { get;set;}
}
class Expectation {
string Enum { get;set;}
}
var source = new Source() { Enum = MyEnum.A };
var expectation = new Expectation() {Enum = "A"};
//With V6 this assertion will fail but in V5 it will pass
expectation.Should().BeEquivalentTo(source, options => options.ComparingEnumsByName());
Run Code Online (Sandbox Code Playgroud)
如何使用 FluentAssertions 断言上述对象?我想要的行为是对枚举的 ToString 表示进行断言。
正如我附注的那样,当我与 交换时,我会得到不同的expectation行为source。它们不应该是等价的吗?
我使用 XUnit 和 c Sharp 中的 fluxassertions 进行单元测试。下面是我获取动态类型、将动态对象转换为该动态类型并尝试进行断言的地方:
var dynamicType = Type.GetType(...);
dynamic? myObject = JsonSerializer.Deserialize(myJSONData, dynamicType);
myObject!.Products!.Should().NotBeNull();
Run Code Online (Sandbox Code Playgroud)
如果我调试它, myObject 确实具有所需的属性和值,但是 c Sharp 和 fluenceassertion 会抛出此错误:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'xxxxx.Products' does not contain a definition for 'Should'
Run Code Online (Sandbox Code Playgroud)
是否可以进行比较或者我错过了什么?
是否可以测试一个返回 void 的函数,并且只向控制台写入一行,如下所示使用FluentAssertion?
static void WriteLine()
{
Console.WriteLine("It works!");
}
Run Code Online (Sandbox Code Playgroud) c# ×9
nunit ×3
unit-testing ×3
.net ×2
xunit ×2
asp.net-core ×1
asp.net-mvc ×1
assertion ×1
dynamic ×1
enums ×1
nsubstitute ×1
tdd ×1