当前位置:  开发笔记 > 编程语言 > 正文

OR条件的流畅断言

如何解决《OR条件的流畅断言》经验,为你挑选了2个好方法。

我正在尝试为以下条件设置流畅的断言.但是找不到带有表达式的方法或带有Or()的ObjectAssertion.

我得检查我的服务状态是枚举值Pending还是Active

services.Should().HaveCount(totalServices).And.BeOfType().Which.ServiceStatusKey.Should().Be(Status.Pending);

我想要的东西,

.Be(Status.Pending).Or().Be(Status.Active)

有人可以帮助我实现这一目标.

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
{
  /// 
  /// Contains a number of methods to assert that an  is in the expected state.
  /// 
  /// 
  [DebuggerNonUserCode]
  public class ObjectAssertions : ReferenceTypeAssertions
  {
    /// 
    /// Returns the type of the subject the assertion applies on.
    /// 
    /// 
    protected override string Context
    {
      get
      {
        return "object";
      }
    }

    public ObjectAssertions(object value)
    {
      this.Subject = value;
    }

    /// 
    /// Asserts that an object equals another object using its  implementation.
    /// 
    /// 
    /// The expected valueA formatted phrase as is supported by  explaining why the assertion
    ///             is needed. If the phrase does not start with the word because, it is prepended automatically.
    ///             Zero or more objects to format using the placeholders in .
    ///             
    public AndConstraint Be(object expected, string because = "", params object[] reasonArgs)
    {
      Execute.Assertion.BecauseOf(because, reasonArgs).ForCondition(ObjectExtensions.IsSameOrEqualTo(this.Subject, expected)).FailWith("Expected {context:object} to be {0}{reason}, but found {1}.", expected, this.Subject);
      return new AndConstraint(this);
    }

    /// 
    /// Asserts that an object does not equal another object using its  method.
    /// 
    /// 
    /// The unexpected valueA formatted phrase explaining why the assertion should be satisfied. If the phrase does not
    ///             start with the word because, it is prepended to the message.
    ///             Zero or more values to use for filling in any  compatible placeholders.
    ///             
    public AndConstraint NotBe(object unexpected, string because = "", params object[] reasonArgs)
    {
      Execute.Assertion.ForCondition(!ObjectExtensions.IsSameOrEqualTo(this.Subject, unexpected)).BecauseOf(because, reasonArgs).FailWith("Did not expect {context:object} to be equal to {0}{reason}.", unexpected);
      return new AndConstraint(this);
    }

    /// 
    /// Asserts that an object is an enum and has a specified flag
    /// 
    /// 
    /// The expected flag.A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
    ///             start with the word because, it is prepended to the message.
    ///             Zero or more values to use for filling in any  compatible placeholders.
    ///             
    public AndConstraint HaveFlag(Enum expectedFlag, string because = "", params object[] reasonArgs)
    {
      Execute.Assertion.BecauseOf(because, reasonArgs).ForCondition(this.Subject != null).FailWith("Expected type to be {0}{reason}, but found .", (object) expectedFlag.GetType()).Then.ForCondition(this.Subject.GetType() == expectedFlag.GetType()).FailWith("Expected the enum to be of type {0} type but found {1}{reason}.", (object) expectedFlag.GetType(), (object) this.Subject.GetType()).Then.Given((Func) (() => this.Subject as Enum)).ForCondition((Func) (@enum => @enum.HasFlag(expectedFlag))).FailWith("The enum was expected to have flag {0} but found {1}{reason}.", (Func) (_ => (object) expectedFlag), (Func) (@enum => (object) @enum));
      return new AndConstraint(this);
    }

    /// 
    /// Asserts that an object is an enum and does not have a specified flag
    /// 
    /// 
    /// The unexpected flag.A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
    ///             start with the word because, it is prepended to the message.
    ///             Zero or more values to use for filling in any  compatible placeholders.
    ///             
    public AndConstraint NotHaveFlag(Enum unexpectedFlag, string because = "", params object[] reasonArgs)
    {
      Execute.Assertion.BecauseOf(because, reasonArgs).ForCondition(this.Subject != null).FailWith("Expected type to be {0}{reason}, but found .", (object) unexpectedFlag.GetType()).Then.ForCondition(this.Subject.GetType() == unexpectedFlag.GetType()).FailWith("Expected the enum to be of type {0} type but found {1}{reason}.", (object) unexpectedFlag.GetType(), (object) this.Subject.GetType()).Then.Given((Func) (() => this.Subject as Enum)).ForCondition((Func) (@enum => !@enum.HasFlag(unexpectedFlag))).FailWith("Did not expect the enum to have flag {0}{reason}.", new object[1]
      {
        (object) unexpectedFlag
      });
      return new AndConstraint(this);
    }
  }
}

Jehof.. 9

您可以在这种情况下使用Match.

ServiceStatusKey.Should().Match(p=>p==Status.Pending || p == Status.Active);

FluentAssertions不支持Or() - 语句.将Match()用于通用断言.



1> Jehof..:

您可以在这种情况下使用Match.

ServiceStatusKey.Should().Match(p=>p==Status.Pending || p == Status.Active);

FluentAssertions不支持Or() - 语句.将Match()用于通用断言.



2> 小智..:

尝试这种方式:

(services.Status == Status.Pending || services.Status == Status.Active).Should().BeTrue("Message when it is false.");

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有