标签: fluent-assertions

如何使用 FluentAssertions 测试嵌套集合

我有以下规格

BidirectionalGraph Fixture = new BidirectionalGraph();

public void VerticesShouldBeAbleToAssociateMultipleEdges()
{
    int a = 0;
    int b = 1;
    int c = 2;

    Fixture.AddEdge(a, b);
    Fixture.AddEdge(b, c);
    Fixture.AddEdge(c, a);

    Fixture.EdgesFrom(a).Should().BeEquivalentTo
        ( new []{a, b} 
        , new []{a, c});
}
Run Code Online (Sandbox Code Playgroud)

其中 EdgesFrom 定义如下

public IEnumerable<int[]> EdgesFrom(int vertex)
Run Code Online (Sandbox Code Playgroud)

但是我的测试失败了

Result Message: Expected collection 

    {{0, 1}, {0, 2}} to be equivalent to 
    {{0, 1}, {0, 2}}.
Run Code Online (Sandbox Code Playgroud)

这对我来说不太有意义,因为它们显然是等效的。FluentAssertions在比较集合的集合时不起作用吗?

c# collections tdd fluent-assertions

5
推荐指数
1
解决办法
2226
查看次数

Fluent Assertions 将字符串与 Guid 进行比较

我试图找到最流畅的方式来断言某个字符串是有效的 Guid。

iterTags.GUID是一个string.

我的第一次尝试以错误结束,因为string没有实现Guid. 好吧,我看到了它的到来,因为它是在黑暗中拍摄的

iterTags.GUID.Should().BeAssignableTo<Guid>();
Run Code Online (Sandbox Code Playgroud)

所以我想出了这个工作解决方案,但它并不流畅

Guid parsedGuid;
if (!Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid))
    Assert.Fail("iterTags.GUID: '{0}' is not a valid guid");
Run Code Online (Sandbox Code Playgroud)

阅读文档后,我发现没有更好的方法来进行断言。

我的问题:是否有一种流畅的方法来断言字符串有效的Guid

或许,类似...

iterTags.GUID.Should().BeParsedAs<Guid>()
Run Code Online (Sandbox Code Playgroud)

c# fluent-assertions

5
推荐指数
1
解决办法
3814
查看次数

比较时如何让 FluentAssertions ShouldBeEquivalentTo 检查类型?

我有 2 个字典,我希望内容不相同,因为字典包含不同类型的值。但是下面的测试通过

[Scenario]
public void DictionariesWithDifferentTypesShouldBeEquivalent(
    Dictionary<string, object> firstDictionary, 
    Dictionary<string, object> secondDictionary)
{
    "Given a dictionary"
        .f(() => firstDictionary = new Dictionary<string, object> 
                    {
                        { "latency", 0 },
                        { "errorMessages", new string[0] },
                        { "lastChanged", new DateTime(635272310930829706) },
                        { "query", new string[0] },
                        { "items", new string[] { "foo", "bar" } },
                        { "name", "Bob" },
                        { "number", 3 },
                        { "updateInterval", 10 },
                    });

    "And a second dictionary with same values but of differing types"
        .f(() => secondDictionary …
Run Code Online (Sandbox Code Playgroud)

c# fluent-assertions

5
推荐指数
1
解决办法
3639
查看次数

在FluentAssertions中,为什么应该使用方法而不是属性?

在FluentAssertions中,您可以以各种格式进行各种声明.

x.Should().BeEquivalentTo(y);
x.ShouldBeEquivalentTo(y);
Run Code Online (Sandbox Code Playgroud)

都是有效的断言.

为什么是Should方法而不是属性?我没有看到任何Should带参数的例子,所以在我看来它可能很容易成为一个属性.

你也可以断言

x.Should().NotBeNull().And.BeEquivalentTo(y);
Run Code Online (Sandbox Code Playgroud)

这里And是属性而不是方法.不应该AndShould每个都是相同类型的元素(方法/属性)?

TL; DRShould在FluentAssertions而不是属性中 设计方法的设计选择背后是否有正当理由?

.net c# fluent-assertions

5
推荐指数
1
解决办法
720
查看次数

为什么使用“Inclusion”时 FluentAssertions 中的嵌套类会被忽略?

为了说明该问题,请考虑以下三个类:

class Orange
{
    public String color { get; set; }
}
class Foo
{
    public Int32 size { get; set; }
    public Orange orange { get; set; }
}
class Bar
{
    public Int32 size { get; set; }
    public Orange orange { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我创建 Foo 和 Bar 的实例,并将它们的 Orange 实例设置为不同,然后执行断言(不使用“包含”),则断言将按预期工作;它找到“red”和“bff”之间的区别:

Foo foo = new Foo() { size = 3 };
foo.orange = new Orange() {color = "red" }; 

Bar bar = new Bar() { size …
Run Code Online (Sandbox Code Playgroud)

.net c# fluent-assertions

5
推荐指数
1
解决办法
1016
查看次数

如何使用流畅的断言比较对象图中的嵌套列表

如果我有一个包含列表的预期对象图,如下所示:

        var expectedExperiment = new Experiment
        {
            Number= "12345",
            AllocatedInstrument = "Instrument 1",
            Experimenters = new List<Experimenter>
            {
                new Experimenter
                {
                    Name = "Sue"
                    Role = "Scientist",
                    Id = 1,
                    Initials = "S"
                },
                new Experimenter()
                {
                    Name = "Mark",
                    Role = "Technician",
                    Id = 2,
                    Initials = "M"
                },
            }
        };
Run Code Online (Sandbox Code Playgroud)

当我只想在子对象列表中包含某些属性时,如何将其与实际对象进行比较。

例如,我想编写这样的代码来比较所有父对象属性和一些子对象属性:

        actualExperiment.ShouldBeEquivalentTo(expectedExperiment, options => options
            .Including(o => o.Number)
            .Including(o => o.AllocatedInstrument)
            .Including(o => o.Experimenters.Select(e => e.Role))
            .Including(o => o.Experimenters.Select(e => e.Name)));
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外:

System.ArgumentException : Expression <o.Experimenters.Select(e => …
Run Code Online (Sandbox Code Playgroud)

c# testing nunit fluent-assertions

5
推荐指数
1
解决办法
1482
查看次数

FluentAssertions 相当于 xUnit Assert.Collection

与以下 xUnit/FluentAssertions 组合最接近的 FluentAssertions 等效项是什么?

Assert.Collection(things,
    thing =>
    {
        thing.Id.Should().Be(guid1);
        thing.Name.Should().Be("Thing1");
        thing.Attributes.Should().NotBeNull();
        thing.FullName.Should().MatchRegex("^Thing1 [^ ]+$");
    },
    thing =>
    {
        thing.Id.Should().Be(guid2);
        thing.Name.Should().Be("Thing2");
        thing.Attributes.Should().NotBeNull();
        thing.FullName.Should().MatchRegex("^Thing2 [^ ]+$");
    });
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我发现这种风格比断言集合相等或等价的 FluentAssertions 方法更具表现力和/或更简洁。

Assert.Collection 签名:

/// <summary>
/// Verifies that a collection contains exactly a given number of elements, which meet
/// the criteria provided by the element inspectors.
/// </summary>
/// <typeparam name="T">The type of the object to be verified</typeparam>
/// <param name="collection">The collection to be inspected</param>
/// <param name="elementInspectors">The element inspectors, which …
Run Code Online (Sandbox Code Playgroud)

c# xunit.net fluent-assertions

5
推荐指数
0
解决办法
1483
查看次数

(何时)使用 FluentAssertions 是个好主意吗?

我正在重写一个C# .NET 项目,目前正在计划如何进行测试。

在我阅读完所有内容之后,我将安装XUnit 框架(这是第一次——我对 MSTest 更有经验)。现在我想知道我是否应该将它与FluentAssertions(我以前也从未使用过)结合起来,或者更确切地说是编写纯 XUnit 测试。

乍一看,FluentAssertions 听起来很书呆子和时尚,但我不确定它是否真的会让我编写可读性最好的代码,以及它在复杂测试中的扩展性如何。

因此,我正在寻找您的经验和论点。[何时](会 | 会)您使用 FluentAssertions?我很好奇。

.net c# unit-testing xunit fluent-assertions

5
推荐指数
2
解决办法
1528
查看次数

FluentAssertions 因带有枚举的结构而失败,但不带有类

我有一个嵌套的classFluentAssertions可以断言它们。
然后我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 …
Run Code Online (Sandbox Code Playgroud)

c# fluent-assertions

5
推荐指数
1
解决办法
1237
查看次数

使用 BeEquivalentTo 时,我可以告诉 FluentAssertions 忽略 Equals 方法吗

我有一个简单的类,它有两个属性并覆盖了 Equals 方法:

public class Person : IEquatable<Person>
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Person);
    }

    public bool Equals(Person other)
    {
        return other != null &&
            this.Id.Equals(other.Id);
    }

    public override int GetHashCode()
    {
        return 2108858624 + EqualityComparer<Guid>.Default.GetHashCode(this.Id);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我创建了一个简单的测试,其中的Id值相同,但Name值不同。

[Fact]
public void PersonShouldNotBeEqual()
{
    var guid = Guid.NewGuid();

    var p1 = new Person { Id = guid, …
Run Code Online (Sandbox Code Playgroud)

c# fluent-assertions

5
推荐指数
1
解决办法
799
查看次数

标签 统计

c# ×10

fluent-assertions ×10

.net ×3

collections ×1

nunit ×1

tdd ×1

testing ×1

unit-testing ×1

xunit ×1

xunit.net ×1