我有两种扩展方法:
public static IPropertyAssertions<T> ShouldHave<T>(this T subject)
{
return new PropertyAssertions<T>(subject);
}
public static IPropertyAssertions<T> ShouldHave<T>(this IEnumerable<T> subject)
{
return new CollectionPropertyAssertions<T>(subject);
}
Run Code Online (Sandbox Code Playgroud)
现在我写一些使用它的代码:
List<Customer> collection2 = new List<Customer>();
collection2.ShouldHave(); //first overload is chosen
IEnumerable<Customer> collection3 = new List<Customer>();
collection3.ShouldHave(); //second overload is chosen
Run Code Online (Sandbox Code Playgroud)
仅当我明确指定IEnumerable类型时才选择第二个重载.有没有办法在两种情况下都选择第二次过载?
我想比较一个对象列表,忽略列表中对象的顺序,只比较对象中的一些属性,目前我正在使用以下代码来执行此比较:
actual.Should().NotBeNull();
actual.Count.Should().Be(expected.Count);
//compare ignoring order
foreach (var exp in expected)
actual.Should().Contain(act =>
act.IndividualId.Equals(exp.IndividualId)
&& act.Email.Equals(exp.Email)
&& act.FirstName.Equals(exp.FirstName)
&& act.LastName.Equals(exp.LastName)
);
Run Code Online (Sandbox Code Playgroud)
然而,这似乎不太理想,因为当出现故障时,您无法获得预期值的打印.是否有使用流畅断言执行此比较的内置机制?
我试图检查异步方法抛出具体异常.
为此我使用的是MSTEST和FluentAssertions 2.0.1.
我已经检查了Codeplex上的这个讨论,并看看它如何与异步异常方法一起工作,这是另一个关于FluentAssertions异步测试的链接:
尝试使用我的'生产'代码一段时间之后,我已经关闭了Fluentassertions假的aync类,我的结果代码是这样的(将此代码放在[TestClass]:
[TestMethod]
public void TestThrowFromAsyncMethod()
{
var asyncObject = new AsyncClass();
Action action = () =>
{
Func<Task> asyncFunction = async () =>
{
await asyncObject.ThrowAsync<ArgumentException>();
};
asyncFunction.ShouldNotThrow();
};
}
internal class AsyncClass
{
public async Task ThrowAsync<TException>()
where TException : Exception, new()
{
await Task.Factory.StartNew(() =>
{
throw new TException();
});
}
public async Task SucceedAsync()
{
await Task.FromResult(0);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是ShouldNotThrow无效:
代码无法识别ShouldNotThrow方法.如果我尝试编译,它会给我这个错误:'System.Func'不包含'ShouldNotThrow'的定义和最好的扩展方法重载'FluentAssertions.AssertionExtensions.ShouldNotThrow(System.Action,string,params object []) …
我有一个类Vector3D具有的属性X,Y和Z类型的双(它也有其它性能如Magnitude).
使用Fluent Assertions以给定精度近似比较所有属性或属性选择的最佳方法是什么?
目前我一直在这样做:
calculated.X.Should().BeApproximately(expected.X, precision);
calculated.Y.Should().BeApproximately(expected.Y, precision);
calculated.Z.Should().BeApproximately(expected.Z, precision);
Run Code Online (Sandbox Code Playgroud)
是否有单线方法可以实现相同的目标?比如使用ShouldBeEquivalentTo,或者这是否需要构建允许包含/排除属性的通用扩展方法?
我正在使用NUnit 2.6.2 + Fluent Assertions 2.0.1.
我想声明两个引用不指向同一个对象实例.我找不到一个干净的方式来表达这一点.
NUnit有Assert.ReferenceEquals(ref1, ref2)- 但我找不到否定的断言.
在Fluent Assertions中,我找不到任何可以直接支持此场景的内容.
我能做到的唯一方法就是这样:
NUnit的: Assert.False(object.ReferenceEquals(ref1, ref2));
流利: object.ReferenceEquals(ref1, ref2).Should().BeFalse();
就最小噪音而言,这两者似乎都不太理想.有没有更好的办法?
使用流畅的断言,我想断言给定的字符串包含两个字符串之一:
actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay");
// eiter value should pass the assertion.
// for example: "you may do it oneWay." should pass, but
// "you may do it thisWay." should not pass
Run Code Online (Sandbox Code Playgroud)
仅当包含这两个值时,断言才会失败.由于没有Or()运算符,这不起作用(甚至不编译).
这是我现在这样做的方式:
bool isVariant1 = actual.Contains(@"oneWay");
bool isVariant2 = actual.Contains(@"anotherWay");
bool anyVariant = (isVariant1 || isVariant2);
anyVariant.Should().BeTrue("because blahblah. Actual content was: " + actual);
Run Code Online (Sandbox Code Playgroud)
这很冗长,必须手动创建"因为"参数以获得有意义的输出.
有没有办法以更易读的方式做到这一点?一个解决方案也应适用于其他流利的断言类型,如Be(),HaveCount()等...
我在.NET 3.5上使用FluentAssertions 2.2.0.0版,如果这很重要的话.
我正在尝试为以下条件设置流畅的断言.但是找不到带有表达式的方法或带有Or()的ObjectAssertion.
我得检查我的服务状态是枚举值Pending还是Active
services.Should().HaveCount(totalServices).And.BeOfType<Service>().Which.ServiceStatusKey.Should().Be(Status.Pending);
Run Code Online (Sandbox Code Playgroud)
我想要的东西,
.Be(Status.Pending).Or().Be(Status.Active)
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我实现这一目标.
FluentAsserstions版本:4.1.1(Nuget的最新内容)附加4.1 FluentAssertions.Primitive命名空间.
// Decompiled with JetBrains decompiler
// Type: FluentAssertions.Primitives.ObjectAssertions
// Assembly: FluentAssertions.Core, Version=4.1.1.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a
// MVID: 090116C5-E9A5-4878-B62E-DE0EBFEBBE14
// Assembly location: C:\RA\P4V\BOSS\trunk\M5Portal\packages\FluentAssertions.4.1.1\lib\net45\FluentAssertions.Core.dll
using FluentAssertions;
using FluentAssertions.Common;
using FluentAssertions.Execution;
using System;
using System.Diagnostics;
namespace FluentAssertions.Primitives
{
/// <summary>
/// Contains a number of methods to assert that an <see cref="T:System.Object"/> is in the expected state.
///
/// </summary>
[DebuggerNonUserCode]
public class ObjectAssertions : ReferenceTypeAssertions<object, ObjectAssertions>
{
/// <summary>
/// Returns the …Run Code Online (Sandbox Code Playgroud) 我最近开始使用 FluentAssertions,它应该具有强大的对象图比较功能。
我正在尝试做最简单的事情:将Address对象的属性与对象的属性进行比较AddressDto。它们都包含 4 个简单的字符串属性:国家/地区、城市、街道和邮政编码(它不是生产系统)。
有人可以向我解释一下,就像我两岁一样,出了什么问题吗?
partnerDto.Address.Should().BeEquivalentTo(partner.Address)
Run Code Online (Sandbox Code Playgroud)
它失败并显示以下消息:
信息:
预期结果。地址为 4 Some street, 12345 Toronto, Canada,但找到了 AddressDto { Country = Canada, ZipCode = 12345, City = Toronto, Street = 4 Some street }。
有配置:
- 使用声明的类型和成员
- 按值比较枚举
- 按名称匹配成员(或抛出)
- 没有自动转换。
- 严格遵守字节数组中项目的顺序
它似乎试图将Address对象视为字符串(因为它覆盖了ToString()?)。我尝试使用该options.ComparingByMembers<AddressDto>()选项,但似乎没有什么区别。
(AddressDto是一个record顺便说一句,不是一个class,因为我正在这个项目中测试新的.Net 5功能;但这可能没有什么区别。)
使用FluentAssertionsrecord代替Trips,因为记录会在后台class自动覆盖,并且 FluentAssertions 假设它应该使用而不是属性比较,因为覆盖可能是为了提供所需的比较。Equals()Equals()Equals()
Equals()但是,在这种情况下, in a 的默认重写实现record实际上仅在两种类型相同时才有效,因此它会失败,因此 FluentAssertions 报告 上的失败BeEquivalentTo() …
有没有办法使用FluentAssertions做这样的事情
response.Satisfy(r =>
r.Property1== "something" &&
r.Property2== "anotherthing"));
Run Code Online (Sandbox Code Playgroud)
我试图避免编写多个Assert语句.使用https://sharptestex.codeplex.com/可以实现这一点,我使用时间最长.但SharpTestEx不支持.Net Core.
当我进行以下测试时
var contentRes = res as OkNegotiatedContentResult<List<MachineHealthTableDTO>>;
contentRes.Should().NotBeNull();
Run Code Online (Sandbox Code Playgroud)
我收到错误
The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should<TDataRow>(TDataRow)' and 'DataSetAssertionExtensions.Should<TDataSet>(TDataSet)'
Run Code Online (Sandbox Code Playgroud)
当我从流畅的断言 5 升级到 6 时,这种情况开始发生。任何有关如何解决此问题的想法将不胜感激。
c# ×9
unit-testing ×4
.net ×1
.net-5 ×1
.net-core ×1
assert ×1
assertions ×1
async-await ×1
combinations ×1
fluent ×1
nunit ×1
overloading ×1
reference ×1
xunit ×1