FluentAssertions:检查包含对象的列表,不包括属性

时间:2017-03-28 12:38:03

标签: fluent-assertions

我有几个实现IEvent的Event类。 要根据预期事件检查实际事件,请使用

actualEvent.ShouldBeEquivalentTo(expectedEvent,opt => opt.RespectingRuntimeTypes()
                                                         .Excluding(e => e.DateCreated));

事件具有DateCreated属性,我忽略该属性,因为在不同时间创建了实际和预期。

如何在actualEvents列表中检查expectedEvent是否至少存在一次?

我想做以下事情;

actualEvents.Should().Contain(expectedEvent,opt => opt.RespectingRuntimeTypes()
                                                      .Excluding(e => e.DateCreated));

但这是不可能的。

这可以在fluentassertions中完成吗?

2 个答案:

答案 0 :(得分:3)

我有一个类似的场景,我有一个项目列表(一个具有属性的对象),我想检查列表是否包含我正在测试的本地项目。

我在这个问题上找到了解决方案:FluentAssertions, making sure IEnumerable contains only single element

我通过编写以下语句简化了我的解决方案:

FooList.Should().Contain(fooItem, x => true)

上述约束规定FooList对象应包含fooItem,它用lambda x =>表示。真。

相当直接并且为我的用例工作。试一试,查看我链接的问题主题可能有所帮助。

祝你好运。

答案 1 :(得分:0)

使用FluentAssertions 4.19.4,我通过实现自己的扩展方法解决了该问题。

使用可以接受config参数的 ShouldBeEquivalentTo 方法,针对期望的元素分别声明集合中的每个元素。

expss::sum_if()

然后您可以编写以下内容:

using FluentAssertions.Collections;
using FluentAssertions.Equivalency;
using FluentAssertions.Execution;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace FluentAssertions.Extensions
{
    public static class FluentAssertionsHelper
    {
        public static AndWhichConstraint<TAssertions, T> Contain<TAssertions, T>(this SelfReferencingCollectionAssertions<T, TAssertions> colAssert,
            T expected,
            Func<EquivalencyAssertionOptions<T>, EquivalencyAssertionOptions<T>> config,
            string because = "",
            params object[] becauseArgs) where TAssertions : SelfReferencingCollectionAssertions<T, TAssertions>
        {
            var items = colAssert.Subject;

            if (items == null)
            {
                Execute.Assertion.BecauseOf(because, becauseArgs).FailWith("Expected {context:collection} to contain {0}{reason}, but found {1}.", expected, items);
            }

            var containsItem = false;

            using (var scope = new AssertionScope())
            {
                foreach (var item in items)
                {
                    try
                    {
                        item.ShouldBeEquivalentTo(expected, config, because, becauseArgs);
                    }
                    catch (NullReferenceException) { }

                    var failures = scope.Discard();
                    if (!failures.Any())
                    {
                        containsItem = true;
                        break;
                    }
                }
            }

            if (!containsItem)
            {
                Execute.Assertion.BecauseOf(because, becauseArgs).FailWith("Expected {context:collection} {0} to contain {1}{reason}.", items, expected);
            }

            return new AndWhichConstraint<TAssertions, T>((TAssertions)colAssert, items.Where(item => EqualityComparer<T>.Default.Equals(item, expected)));
        }
    }
}
相关问题