标签: fluent-assertions

如何断言所有选定的属性都已设置(非空或空)

我想验证(断言)我的DTO对象上的某些属性是否已设置.我试图用Fluent Assertions来做,但以下代码似乎不起作用:

mapped.ShouldHave().Properties(
    x => x.Description,
    ...more
    x => x.Id)
    .Should().NotBeNull(); 
Run Code Online (Sandbox Code Playgroud)

是否可以通过Fluent Assertions或其他工具实现这一目标?Fluent断言具有ShouldBeEquivalentTo,但实际上我只关心那些是不是空/空,所以我无法使用.

当然我可以在每个属性级别上做一个Assert,但是对一些更优雅的方式感兴趣.

.net c# fluent-assertions

7
推荐指数
1
解决办法
8602
查看次数

如何用FluentAssertions替换Assert.Fail()

目前,我们正在将使用一些代码Assert.IsTrue()Assert.AreEqual()Assert.IsNotNull(),等的基本单元测试断言库C#

我们要使用FluentAssertions,例如 value.Should().BeNull().

Assert.Fail()在某些地方使用了一些测试。我想用什么来有效地替换那些,因为我们要删除每个“ Assert。*”,而我在FluentAssertions中找不到等效的东西?

这是一个例子

[TestMethod, TestCategory("ImportantTest")]
public void MethodToTest_Circumstances_ExpectedResult()
{
    // Arrange
    var variable1 = new Type1() { Value = "hello" };
    var variable2 = new Type2() { Name = "Bob" };

    // Act
    try
    {
        MethodToTest(variable1, variable2);
        // This method should have thrown an exception
        Assert.Fail();
    }
    catch (Exception ex)
    {
        ex.Should().BeOfType<DataException>();
        ex.Message.Should().Be(Constants.DataMessageForMethod);
    }

    // Assert
    // test that variable1 was changed by the method
    variable1.Should().NotBeNull();
    variable1.Value.Should().Be("Hello!");
    // test …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing assertions fluent-assertions

7
推荐指数
1
解决办法
2626
查看次数

如何验证多个排序已应用于集合?

我正在我的 Kendo 网格上实现可排序的列,用户预期的行为是允许同时对多个列进行排序。

自然地,我首先编写一个单元测试,以便能够验证排序是(默认情况下)首先是Ref升序,然后是Name升序。

有问题的测试供应商在这里:

_context.Suppliers.Add(new Supplier { Ref = "abc", Name = "steve"});
_context.Suppliers.Add(new Supplier { Ref = "abc", Name = "bob"});    
_context.Suppliers.Add(new Supplier { Ref = "cde", Name = "ian"});    
_context.Suppliers.Add(new Supplier { Ref = "fgh", Name = "dan"}); 
Run Code Online (Sandbox Code Playgroud)

然后我去要求对我分类的供应商进行测试。

我知道 Fluent 断言有BeInAscendingOrderBeInDescendingOrder方法,但是即使在查看文档并遵循可能相关的问题之后,我也看不到链接排序方法的示例。

我目前的测试验证是这样的:

results.Should().HaveCount(4).And.BeInAscendingOrder(x => x.Ref)
           .And.BeInAscendingOrder(x => x.Name);
Run Code Online (Sandbox Code Playgroud)

我期待验证的工作有点像LINQ它有的地方OrderBy(x => x.Ref).ThenBy(x => x.Name)

但是,在运行测试时,它失败了,因为它期望集合按升序Name排序(最终排序断言)。

有没有办法告诉 Fluent Assertions 排序应该相互结合应用,而不仅仅是按顺序应用?

c# sorting tdd unit-testing fluent-assertions

7
推荐指数
1
解决办法
889
查看次数

使用FluentAssertions比较可空类型与其基础类型时,这是一个错误吗?

我正在为一个实用程序库编写一些单元测试,当我遇到一个我希望实际上通过的测试失败时.该问题与比较两个float变量,而不是比较float?一个float变量和一个变量有关.

我正在使用NUnit(2.6.0.12051)和FluentAssertions(1.7.1)的最新版本,下面是一个小代码剪辑,说明了这个问题:

using FluentAssertions;
using FluentAssertions.Assertions;
using NUnit.Framework;

namespace CommonUtilities.UnitTests
{
    [TestFixture]
    public class FluentAssertionsFloatAssertionTest
    {
        [Test]
        public void TestFloatEquality()
        {
            float expected = 3.14f;
            float notExpected = 1.0f;
            float actual = 3.14f;

            actual.Should().BeApproximately(expected, 0.1f);
            actual.Should().BeApproximately(notExpected, 0.1f); // A: Correctly fails (Expected value 3,14 to approximate 1 +/- 0,1, but it differed by 2,14.)
            actual.Should().BeInRange(expected, expected);
            actual.Should().BeInRange(notExpected, notExpected); // B: Correctly fails (Expected value 3,14 to be between 1 and 1, but it was …
Run Code Online (Sandbox Code Playgroud)

c# comparison nunit nullable fluent-assertions

6
推荐指数
1
解决办法
546
查看次数

FluentAssertions:匹配集合的每个对象

如何检查集合的每个对象是否符合给定的谓词?例如:检查每个项目(来自给定集合)它是否与给定谓词(MyPredicate)匹配.代码应该看起来像这样:

collection.Should().AllMatch(item => MyPredicate(item));
Run Code Online (Sandbox Code Playgroud)

是这样的东西还是我必须自己写?

.net c# fluent-assertions

6
推荐指数
1
解决办法
1515
查看次数

我有一个引用FluentAssertions的单元测试项目,当我更新到版本3时出现错误

我有一个.net 4.0测试项目,它为Should()扩展方法抛出了一个找不到异常的方法.

然后我注意到它也为int类型抛出异常.

有人知道FluentAssertions V3为什么会这样吗?

现在我要回到我的2.2版本.

作为参考,这是FluentAssersions项目网站https://github.com/dennisdoomen/fluentassertions/releases

这是代码示例: 在此输入图像描述

var actualItems = new List<string> { "" };
actualItems.All(i => i == "X").Should().BeTrue("All items should be X") ;
Run Code Online (Sandbox Code Playgroud)

这是一个例外:

Error   237 'bool' does not contain a definition for 'Should' 
and no extension method 'Should' accepting a first argument of type 'bool' 
could be found (are you missing a using directive or an assembly reference?)
C:\pathtoproject\Tests.cs
Run Code Online (Sandbox Code Playgroud)

fluent-assertions

6
推荐指数
1
解决办法
1626
查看次数

如何使用 FluentAssertions 比较两个 MemoryStream

使用FluentAssertion 3.1.229,如何比较两个不同的内容MemoryStream

写入actualStream.Should().Be(expectedStream);会出现以下错误:

System.IO.MemoryStream
{
   CanRead = True
   CanSeek = True
   CanTimeout = False
   CanWrite = True
   Capacity = 8
   Length = 8
   Position = 0
   ReadTimeout = "[Property 'ReadTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
   WriteTimeout = "[Property 'WriteTimeout' threw an exception: 'Exception has been thrown by the target of an invocation.']"
}, but found 

System.IO.MemoryStream
{
   CanRead = True
   CanSeek = True
   CanTimeout = …
Run Code Online (Sandbox Code Playgroud)

fluent-assertions

6
推荐指数
1
解决办法
4841
查看次数

如何使用C#比较两个Json对象

我有两个Json对象需要进行比较.我正在使用Newtonsoft库进行Json解析.

string InstanceExpected = jsonExpected;
string InstanceActual = jsonActual;
var InstanceObjExpected = JObject.Parse(InstanceExpected);
var InstanceObjActual = JObject.Parse(InstanceActual);
Run Code Online (Sandbox Code Playgroud)

我正在使用Fluent Assertions对它进行比较.但问题是Fluent断言仅在属性count/names不匹配时才会失败.如果json值不同则会通过.当值不同时,我要求失败.

InstanceObjActual.Should().BeEquivalentTo(InstanceObjExpected);
Run Code Online (Sandbox Code Playgroud)

例如,我有实际和预期的json比较如下.并且使用上面的比较方式使得它们传递错误.

{
  "Name": "20181004164456",
  "objectId": "4ea9b00b-d601-44af-a990-3034af18fdb1%>"  
}

{
  "Name": "AAAAAAAAAAAA",
  "objectId": "4ea9b00b-d601-44af-a990-3034af18fdb1%>"  
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing json.net assertion fluent-assertions

6
推荐指数
2
解决办法
5937
查看次数

流畅的断言“已达到最大递归深度......”

我有许多嵌套的复杂对象,我尝试使用以下代码将它们与流畅断言进行比较:

\n
restResponse.Should().BeEquivalentTo(mappedSoapResponse, options =>\n            {\n                options.AllowingInfiniteRecursion();\n                options.IgnoringCyclicReferences();\n                return options;\n            });\n
Run Code Online (Sandbox Code Playgroud)\n

尽管如此,尽管特别启用了无限递归,但我仍然遇到“达到最大递归深度\xe2\x80\xa6”的问题。

\n

c# fluent fluent-assertions

6
推荐指数
1
解决办法
3889
查看次数

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
查看次数