我想验证(断言)我的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,但是对一些更优雅的方式感兴趣.
目前,我们正在将使用一些代码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) 我正在我的 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 断言有BeInAscendingOrder和BeInDescendingOrder方法,但是即使在查看文档并遵循可能相关的问题之后,我也看不到链接排序方法的示例。
我目前的测试验证是这样的:
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 排序应该相互结合应用,而不仅仅是按顺序应用?
我正在为一个实用程序库编写一些单元测试,当我遇到一个我希望实际上通过的测试失败时.该问题与比较两个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) 如何检查集合的每个对象是否符合给定的谓词?例如:检查每个项目(来自给定集合)它是否与给定谓词(MyPredicate)匹配.代码应该看起来像这样:
collection.Should().AllMatch(item => MyPredicate(item));
Run Code Online (Sandbox Code Playgroud)
是这样的东西还是我必须自己写?
我有一个.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) 使用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) 我有两个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) 我有许多嵌套的复杂对象,我尝试使用以下代码将它们与流畅断言进行比较:
\nrestResponse.Should().BeEquivalentTo(mappedSoapResponse, options =>\n {\n options.AllowingInfiniteRecursion();\n options.IgnoringCyclicReferences();\n return options;\n });\nRun Code Online (Sandbox Code Playgroud)\n尽管如此,尽管特别启用了无限递归,但我仍然遇到“达到最大递归深度\xe2\x80\xa6”的问题。
\n与以下 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# ×8
unit-testing ×3
.net ×2
assertion ×1
assertions ×1
comparison ×1
fluent ×1
json.net ×1
nullable ×1
nunit ×1
sorting ×1
tdd ×1
xunit.net ×1