- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为以下条件设置流畅的断言。但是找不到带有表达式的方法或带有 Or() 的 ObjectAssertion。
我必须检查我的服务状态是枚举值 Pending
还是 Active
services.Should().HaveCount(totalServices).And.BeOfType<Service>().Which.ServiceStatusKey.Should().Be(Status.Pending);
我想要这样的东西,
.Be(Status.Pending).Or().Be(Status.Active)
有人可以帮我实现这个目标吗。
FluentAssertions 版本: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 type of the subject the assertion applies on.
///
/// </summary>
protected override string Context
{
get
{
return "object";
}
}
public ObjectAssertions(object value)
{
this.Subject = value;
}
/// <summary>
/// Asserts that an object equals another object using its <see cref="M:System.Object.Equals(System.Object)"/> implementation.
///
/// </summary>
/// <param name="expected">The expected value</param><param name="because">A formatted phrase as is supported by <see cref="M:System.String.Format(System.String,System.Object[])"/> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param><param name="reasonArgs">Zero or more objects to format using the placeholders in <see cref="!:because"/>.
/// </param>
public AndConstraint<ObjectAssertions> 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<ObjectAssertions>(this);
}
/// <summary>
/// Asserts that an object does not equal another object using its <see cref="M:System.Object.Equals(System.Object)"/> method.
///
/// </summary>
/// <param name="unexpected">The unexpected value</param><param name="because">A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param><param name="reasonArgs">Zero or more values to use for filling in any <see cref="M:System.String.Format(System.String,System.Object[])"/> compatible placeholders.
/// </param>
public AndConstraint<ObjectAssertions> 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<ObjectAssertions>(this);
}
/// <summary>
/// Asserts that an object is an enum and has a specified flag
///
/// </summary>
/// <param name="expectedFlag">The expected flag.</param><param name="because">A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param><param name="reasonArgs">Zero or more values to use for filling in any <see cref="M:System.String.Format(System.String,System.Object[])"/> compatible placeholders.
/// </param>
public AndConstraint<ObjectAssertions> 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 <null>.", (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<Enum>((Func<Enum>) (() => this.Subject as Enum)).ForCondition((Func<Enum, bool>) (@enum => @enum.HasFlag(expectedFlag))).FailWith("The enum was expected to have flag {0} but found {1}{reason}.", (Func<Enum, object>) (_ => (object) expectedFlag), (Func<Enum, object>) (@enum => (object) @enum));
return new AndConstraint<ObjectAssertions>(this);
}
/// <summary>
/// Asserts that an object is an enum and does not have a specified flag
///
/// </summary>
/// <param name="unexpectedFlag">The unexpected flag.</param><param name="because">A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param><param name="reasonArgs">Zero or more values to use for filling in any <see cref="M:System.String.Format(System.String,System.Object[])"/> compatible placeholders.
/// </param>
public AndConstraint<ObjectAssertions> 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 <null>.", (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<Enum>((Func<Enum>) (() => this.Subject as Enum)).ForCondition((Func<Enum, bool>) (@enum => !@enum.HasFlag(unexpectedFlag))).FailWith("Did not expect the enum to have flag {0}{reason}.", new object[1]
{
(object) unexpectedFlag
});
return new AndConstraint<ObjectAssertions>(this);
}
}
}
最佳答案
您可以在这种情况下使用 Match。
ServiceStatusKey.Should().Match<Status>(p=>p==Status.Pending || p == Status.Active);
FluentAssertions 不支持 Or() 语句。将 Match() 用于通用断言。
关于c# - OR 条件的流利断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34654341/
我有以下实体字段: /** * @Assert\Regex( * pattern = "/^d+\.(jpg|png|gif)$/", * htmlPattern = "/^d+\.(j
我有一个显示查询结果的表。 我使用这个检查表包含结果: cy.get("table", {timeout: 60000}).find("tr[data-index]").should("have.le
void (assert)(int e) { assert(e); } 这里是如何工作的? 最佳答案 void (assert)(int e) 相当于 void assert(int) 为什么
大家好,我的第一次测试遇到了一些问题 我正在写这个 fragment ,但我一直遇到这个问题: java.lang.AssertionError at org.junit.Assert.fail(As
从表中,我获取字符串列表,并想检查当 val!= "A"时,表列表中是否存在字符串“x”: for (int i = 0; i list = new ArrayList(); for(int i=0
我想在现有代码中为 VHDL 断言操作添加一个错误计数器。基本上,我的代码如下所示: assert data = good_data report "Bad data" severity er
我有一个无法处理的问题,所以我想也许你可以帮助我。基本上我有一个函数,它接收 char* 作为参数并对其执行一些操作(我已经检查了这些步骤/函数,它们工作得很好)。如果函数中给出的 char* 是“”
我有一些代码用于检查输入日期是否正确。只需检查是否不超过 12 个月,给定月份中的日期是否正确,并且还应该考虑闰年(因此,基本上是正常的日期检查)。唯一不合适的是年份不应低于 2000。如果日期无效,
我有一个关于 Cypress 断言的问题,最近才开始使用这个测试平台,但是当 URL 返回一个随机数时卡住了,如下所示。 /Geocortex/Essentials/REST/sites/SITE?f
我有一个 Python mock对象,我想断言是否设置了该对象的任何属性。 我不相信PropertyMock将适用于我的目的,因为我必须知道是否设置了任何属性,而不是特定属性。 它也不像我可以模拟模拟
考虑这个简单的基类 Foo 有一个函数 foo 调用一个纯虚函数 foo_,用 Doxygen 记录: class Foo { public: /** \brief This functi
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我在这段代码中执行 malloc 时遇到问题, /*function starts*/ if(NULL==(partial_results=(bignum_t**)malloc(sizeof
大家好,我想知道如何将整个代码块放入一个断言中? 例如,我有一个数组,我想对数组的每个值进行断言。这是我的代码的样子: for (int value : values) { assert Wi
这不是我的作业——我只是在练习。我似乎无法完全理解这个断言概念。 1) Determine the pre-condition for x that guarantees the post-condi
是否可以在带有时间戳的断言失败时记录信息 例如 int a = 10 assert( a > 100 ); 那么它将失败并像时间戳一样输出 2013-12-02 , 17:00:05 assert f
由于我们公司不使用单元测试,所以我正在自学对自己的代码进行单元测试。我正在使用标准的 .net 测试框架进行一些非常基本的单元测试。 我的一个方法返回一个 IEnumerable我想测试它的输出。所以
我有一个如下所示的对象数组,并且只想针对数组中的某些字段进行断言。 对象数组 - var a = [{"key":"key2","value":"value2","updatedBy":"name1"
我正在尝试编写一个 PSL 断言来检查输入断言的数量是否与输出断言的数量相匹配。 例如: . 在输入上任何时候都可能发生任何事情,输出也可以在任何时候断言。确切时间未知,也不重要。我要验证的是没有信息
我想在Cypress中做一个断言如下: cy.get(a).should('be.visible').or(()=>{ cy.get(b).should('be.visible'); });
我是一名优秀的程序员,十分优秀!