我正在使用流畅的断言,并且进行了以下测试:
result.Should().NotBeNull();
result.Link.Should().Equals("https://someinvoiceurl.com");
Run Code Online (Sandbox Code Playgroud)
效果很好,但是当我尝试这个时
result.Should().NotBeNull()
.Which.Link.Equals("https://someinvoiceurl.com");
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
“AndConstraint”不包含“Which”的定义,并且找不到接受“AndConstraint”类型的第一个参数的可访问扩展方法“Which”(您是否缺少 using 指令或程序集引用?)
我做错了什么?
以下Func委托抛出一个ArgumentNullException:
Func<Task> act = async () => await _someService
.someMethod(1, 2, 3, 4);
Run Code Online (Sandbox Code Playgroud)
使用Fluent 断言,断言:
act.Should().ThrowExactlyAsync<ArgumentException>();
Run Code Online (Sandbox Code Playgroud)
应该失败:
断言当前
Func抛出确切类型的异常TException(而不是派生异常类型)。
ArgumentNullException派生自ArgumentException,给定描述,断言应该失败,但它通过了。
这是一个错误还是我滥用了这个?
这段代码工作正常
[Test]
public void boo()
{
var collection = new[] { 1, 2, 3 };
collection.Should().Equal(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
但是,这失败了
[Test]
public void foo()
{
var collection = new[] { "1", "2", "3" };
collection.Should().Equal("1", "2", "3");
}
Run Code Online (Sandbox Code Playgroud)
失败消息是:
'预期集合等于{1}因为2,但{"1","2","3"}包含2个项目太多.
这有什么不对?为什么无法比较可枚举的字符串?
原因,我的问题是 - 如何在foo()中处理案例?
场景:我有一个带有可为空属性的对象,当我运行被测方法时,该对象将被更新。在期望的对象中,我没有指定它,因为我想单独验证该值。这是一个简单的测试演示
using System;
using FluentAssertions;
using NUnit.Framework;
namespace FluentAssertionsNullableFailure
{
public class SimpleWithNullable
{
public Int64? nullableIntegerProperty
{ get; set; }
public string strProperty
{ get; set; }
}
[TestFixture]
public class Demo
{
public SimpleWithNullable actual = new SimpleWithNullable { nullableIntegerProperty = 1, strProperty = "I haz a string!" };
public SimpleWithNullable expected = new SimpleWithNullable { strProperty = "I haz a string!" };
[Test]
public void NullableTest ()
{
actual.ShouldBeEquivalentTo (
expected,
opt => opt.Using<Int64?> ( c => …Run Code Online (Sandbox Code Playgroud) 如果我下载FluentAssertions 1.7.1(我目前所有的单元测试都是针对的),那么我可以对dll进行引用,下面的测试也可以正常工作.
但我试图通过NuGet升级并使用FluentAssertions版本3.0.90并尝试3.0.107.在遇到问题之后我试图制作一个全新的解决方案/类库项目,但它无法从库中检测出任何东西......
下面的测试将无法编译,因为.Should无法找到bool...或其他任何相关的扩展方法.但我可以清楚地看到它存在于对象浏览器中我可以在我的程序集引用中看到FluentAssertions和FluentAssertions.Core,但只能在对象浏览器中查看FluentAssertions.Core.Intellisence似乎唯一能找到的扩展方法是.ShouldRaisePropertyChangeFor<...>和.ShouldNotRaisePropertyChangeFor<...>.
我的项目目标是.Net4.0,此时我认为这是一个配置问题,但我不知道从哪里开始查看是否存在.谷歌上没有其他人似乎有这个问题.
using System;
using FluentAssertions;
using NUnit.Framework;
namespace IntegrationTests.CommonTests
{
[TestFixture]
public class _BasicTemplate_Tests
{
[Test]
public void Run_Basic_Test()
{
true.Should().Be(true);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写单元测试,我有一些看起来像这样的东西:
[Fact]
public void GetFoos_only_gets_foo1()
{
_foo1.included = true; //Foo object
_foo2.included = false; //Foo object
_sut.GetFoos().Should().OnlyContain(_foo1); // this doesn't work, is there a specific syntax to use?
}
Run Code Online (Sandbox Code Playgroud)
并且GetFoos()返回和IEnumberable<Foo>
我们使用Specflow,目前,我们的大多数断言都是使用Fluent Assertions完成的,例如:
myval.Should().NotBe(null)
一切都运行良好,但在某些情况下,我们希望相同的代码有时断言不确定,有时不会.
例如,假设我们有一个调用方法来安装AUT的步骤.
断言AUT可以正确安装的测试将使用此方法作为WHEN/THEN步骤的一部分.
在这种情况下,如果安装失败,我们希望正常进行并且无法通过测试.
GIVEN The AUT is NOT installed
WHEN I install the AUT
但是,对于所有其他测试,可以将相同的方法作为GIVEN步骤的一部分或作为BeforeScenario/BeforeFeature挂钩的一部分调用,并且在该实例中,如果安装失败,则测试应该失败,因为它是不是测试本身失败,而是安装阶段.
GIVEN the AUT is installed
WHEN I perform function X of the AUT
所以在这个例子中,让我们假设WHEN I install the AUT第一个测试中的步骤定义,以及GIVEN the AUT is installed第二个测试中的步骤定义只是用类似的东西调用辅助方法AppFacade.Install()
当然,我们可以AppFacade.Install()使用条件等垃圾和所有其他辅助方法,但我想知道是否有人有更优雅的解决方案 - 即扩展断言引擎的方式,以便它自动应用这个逻辑 - 我看到没有障碍实现此目的的Specflow方面,您可以检查Specflow上下文对象以检测您所处的步骤类型.
理想情况下,我们希望坚持使用流利断言,但如果这过于复杂化,我们会愿意重新考虑.
我坚持认为简单的例子。我想断言一个对象集合包含一个与给定对象等效的对象。喜欢:col.ShouldContainEquivalentTo(obj)
var objectList1 = new List<SomeClass> { new SomeClass("A"), new SomeClass("B"), new SomeClass("C") };
var objectList2 = new List<SomeClass> { new SomeClass("C"), new SomeClass("B"), new SomeClass("A") };
objectList1.ShouldAllBeEquivalentTo(objectList2); //this works
objectList2.ShouldContainEquivalentTo(new SomeClass("B")); //method does not exist. How can I achieve sthg like that
Run Code Online (Sandbox Code Playgroud)
我想基于对象的值进行比较-随便怎么样ShouldBeEquivalentTo和ShouldAllBeEquivalentTo工作。不必编写我自己的相等比较器。
马蒂亚斯(BR Matthias)
我正在尝试使用Nspec。我已按照以下说明进行操作:http : //nspec.org/
using NSpec;
using FluentAssertions;
class my_first_spec : nspec
{
string name;
void before_each()
{
name = "NSpec";
}
void it_asserts_at_the_method_level()
{
name.ShouldBeEquivalentTo("NSpec");
}
void describe_nesting()
{
before = () => name += " Add Some Other Stuff";
it["asserts in a method"] = () =>
{
name.ShouldBeEquivalentTo("NSpec Add Some Other Stuff");
};
context["more nesting"] = () =>
{
before = () => name += ", And Even More";
it["also asserts in a lambda"] …Run Code Online (Sandbox Code Playgroud) 可能是一个简单的,但不能让它工作;
我已经将方法上的签名改为Task
在我的单元测试中,我使用流畅的断言.
但不能让这个工作:
_catalogVehicleMapper
.Invoking(m => m.MapToCatalogVehiclesAsync(searchResult, filtersInfo, request, default(CancellationToken)))
.Should().Throw<ArgumentException>()
.WithMessage("One of passed arguments has null value in mapping path and can not be mapped");
Run Code Online (Sandbox Code Playgroud)
这MapToCatalogVehiclesAsync是异步方法,但我需要等待它,但等待和异步调用,似乎没有做..有人..?