标签: fluent-assertions

扩展方法过载选择

我有两种扩展方法:

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类型时才选择第二个重载.有没有办法在两种情况下都选择第二次过载?

.net c# extension-methods overloading fluent-assertions

12
推荐指数
3
解决办法
598
查看次数

如何使用流畅断言比较列表?

我想比较一个对象列表,忽略列表中对象的顺序,只比较对象中的一些属性,目前我正在使用以下代码来执行此比较:

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)

然而,这似乎不太理想,因为当出现故障时,您无法获得预期值的打印.是否有使用流畅断言执行此比较的内置机制?

c# unit-testing xunit fluent-assertions

12
推荐指数
2
解决办法
2万
查看次数

对于异步方法/ Func,无法识别FluentAssertions ShouldNotThrow

我试图检查异步方法抛出具体异常.

为此我使用的是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 []) …

c# async-await fluent-assertions

12
推荐指数
1
解决办法
1万
查看次数

流畅断言:近似比较类属性

我有一个类Vector3D具有的属性X,YZ类型的双(它也有其它性能如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,或者这是否需要构建允许包含/排除属性的通用扩展方法?

c# unit-testing fluent-assertions

12
推荐指数
1
解决办法
2869
查看次数

NUnit或Fluent Assertions测试参考相等性?

我正在使用NUnit 2.6.2 + Fluent Assertions 2.0.1.

我想声明两个引用不指向同一个对象实例.我找不到一个干净的方式来表达这一点.

NUnitAssert.ReferenceEquals(ref1, ref2)- 但我找不到否定的断言.

Fluent Assertions中,我找不到任何可以直接支持此场景的内容.

我能做到的唯一方法就是这样:

NUnit的: Assert.False(object.ReferenceEquals(ref1, ref2));

流利: object.ReferenceEquals(ref1, ref2).Should().BeFalse();

就最小噪音而言,这两者似乎都不太理想.有没有更好的办法?

c# nunit unit-testing reference fluent-assertions

11
推荐指数
2
解决办法
4472
查看次数

Fluent断言:断言一个或另一个值

使用流畅的断言,我想断言给定的字符串包含两个字符串之一:

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版,如果这很重要的话.

c# combinations unit-testing assert fluent-assertions

11
推荐指数
2
解决办法
5863
查看次数

OR条件的流畅断言

我正在尝试为以下条件设置流畅的断言.但是找不到带有表达式的方法或带有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)

c# fluent assertions fluent-assertions

11
推荐指数
2
解决办法
3437
查看次数

当类型为 C# 9 记录时,FluentAssertions Should().BeEquivalentTo() 在简单情况下会失败,看似将对象视为字符串

我最近开始使用 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() …

c# fluent-assertions .net-5

11
推荐指数
2
解决办法
9605
查看次数

FluentAssertions断言单个对象的多个属性

有没有办法使用FluentAssertions做这样的事情

response.Satisfy(r =>
    r.Property1== "something" &&
    r.Property2== "anotherthing"));
Run Code Online (Sandbox Code Playgroud)

我试图避免编写多个Assert语句.使用https://sharptestex.codeplex.com/可以实现这一点,我使用时间最长.但SharpTestEx不支持.Net Core.

c# fluent-assertions .net-core

10
推荐指数
2
解决办法
4009
查看次数

在 As 项上使用 Should().NotBeNull() 时出现不明确的调用

当我进行以下测试时

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 时,这种情况开始发生。任何有关如何解决此问题的想法将不胜感激。

fluent-assertions

10
推荐指数
2
解决办法
3858
查看次数