我有以下规格
BidirectionalGraph Fixture = new BidirectionalGraph();
public void VerticesShouldBeAbleToAssociateMultipleEdges()
{
int a = 0;
int b = 1;
int c = 2;
Fixture.AddEdge(a, b);
Fixture.AddEdge(b, c);
Fixture.AddEdge(c, a);
Fixture.EdgesFrom(a).Should().BeEquivalentTo
( new []{a, b}
, new []{a, c});
}
Run Code Online (Sandbox Code Playgroud)
其中 EdgesFrom 定义如下
public IEnumerable<int[]> EdgesFrom(int vertex)
Run Code Online (Sandbox Code Playgroud)
但是我的测试失败了
Result Message: Expected collection
{{0, 1}, {0, 2}} to be equivalent to
{{0, 1}, {0, 2}}.
Run Code Online (Sandbox Code Playgroud)
这对我来说不太有意义,因为它们显然是等效的。FluentAssertions在比较集合的集合时不起作用吗?
我试图找到最流畅的方式来断言某个字符串是有效的 Guid。
iterTags.GUID是一个string.
我的第一次尝试以错误结束,因为string没有实现Guid. 好吧,我看到了它的到来,因为它是在黑暗中拍摄的
iterTags.GUID.Should().BeAssignableTo<Guid>();
Run Code Online (Sandbox Code Playgroud)
所以我想出了这个工作解决方案,但它并不流畅
Guid parsedGuid;
if (!Guid.TryParseExact(iterTags.GUID, "D", out parsedGuid))
Assert.Fail("iterTags.GUID: '{0}' is not a valid guid");
Run Code Online (Sandbox Code Playgroud)
阅读文档后,我发现没有更好的方法来进行断言。
我的问题:是否有一种流畅的方法来断言字符串是有效的Guid
或许,类似...
iterTags.GUID.Should().BeParsedAs<Guid>()
Run Code Online (Sandbox Code Playgroud) 为了说明该问题,请考虑以下三个类:
class Orange
{
public String color { get; set; }
}
class Foo
{
public Int32 size { get; set; }
public Orange orange { get; set; }
}
class Bar
{
public Int32 size { get; set; }
public Orange orange { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我创建 Foo 和 Bar 的实例,并将它们的 Orange 实例设置为不同,然后执行断言(不使用“包含”),则断言将按预期工作;它找到“red”和“bff”之间的区别:
Foo foo = new Foo() { size = 3 };
foo.orange = new Orange() {color = "red" };
Bar bar = new Bar() { size …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含列表的预期对象图,如下所示:
var expectedExperiment = new Experiment
{
Number= "12345",
AllocatedInstrument = "Instrument 1",
Experimenters = new List<Experimenter>
{
new Experimenter
{
Name = "Sue"
Role = "Scientist",
Id = 1,
Initials = "S"
},
new Experimenter()
{
Name = "Mark",
Role = "Technician",
Id = 2,
Initials = "M"
},
}
};
Run Code Online (Sandbox Code Playgroud)
当我只想在子对象列表中包含某些属性时,如何将其与实际对象进行比较。
例如,我想编写这样的代码来比较所有父对象属性和一些子对象属性:
actualExperiment.ShouldBeEquivalentTo(expectedExperiment, options => options
.Including(o => o.Number)
.Including(o => o.AllocatedInstrument)
.Including(o => o.Experimenters.Select(e => e.Role))
.Including(o => o.Experimenters.Select(e => e.Name)));
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外:
System.ArgumentException : Expression <o.Experimenters.Select(e => …Run Code Online (Sandbox Code Playgroud) 与以下 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# .NET 项目,目前正在计划如何进行测试。
在我阅读完所有内容之后,我将安装XUnit 框架(这是第一次——我对 MSTest 更有经验)。现在我想知道我是否应该将它与FluentAssertions(我以前也从未使用过)结合起来,或者更确切地说是编写纯 XUnit 测试。
乍一看,FluentAssertions 听起来很书呆子和时尚,但我不确定它是否真的会让我编写可读性最好的代码,以及它在复杂测试中的扩展性如何。
因此,我正在寻找您的经验和论点。[何时](会 | 会)您使用 FluentAssertions?我很好奇。
我有一个嵌套的classFluentAssertions可以断言它们。
然后我class改为struct测试失败。
(如果我更改IEnumerable<ItemStruct> MyItems { get; set; }为ItemStruct MyItem { get; set; }在两种情况下比较都会成功。所以我想这与 IEnumerable 有关。)
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace CowsCannotReadLogs.FileHandling.UnitTest
{
[TestClass]
public class TestFluentAssertionStruct
{
public struct DataStruct
{
public IEnumerable<string> MyItems { get; set; }
}
// Fails.
[TestMethod]
public void TestSimpleStruct()
{
var data1 = new DataStruct { MyItems = new[] { "A" } };
var data2 = new DataStruct …Run Code Online (Sandbox Code Playgroud) 我正在使用 fluid-assertions 和 sqlite 编写单元测试,它存储了糟糕的十进制类型。
我希望所有的小数比较都忽略浮动部分。
有办法做到吗?
谢谢 !
因为我有一些角度,所以我想检查角度模数 360\xc2\xb0:
\n double angle = 0;\n double expectedAngle = 360;\n angle.Should().BeApproximatelyModulus360(expectedAngle, 0.01);\nRun Code Online (Sandbox Code Playgroud)\n我已经按照教程编写了 Fluent Assertions 框架的扩展:https ://fluentassertions.com/extensibility/
\npublic static class DoubleExtensions\n{\n public static DoubleAssertions Should(this double d)\n {\n return new DoubleAssertions(d);\n }\n}\n\n\npublic class DoubleAssertions : NumericAssertions<double>\n{\n public DoubleAssertions(double d) : base(d)\n {\n }\n public AndConstraint<DoubleAssertions> BeApproximatelyModulus360(\n double targetValue, double precision, string because = "", params object[] becauseArgs)\n {\n Execute.Assertion\n .Given(() => Subject)\n .ForCondition(v => MathHelper.AreCloseEnoughModulus360(targetValue, (double)v, precision))\n .FailWith($"Expected value {Subject}] should be approximatively {targetValue} with {precision} …Run Code Online (Sandbox Code Playgroud) 好的,我正在运行单元测试,以查看Exception.Data属性是否包含针对特定命名键的特定值.
Exception.Data的类型为IDictionary.IDictionary只有2个重载,我无法看到验证字典中的内容的方法.
我有以下代码抛出异常:
public class MyClass
{
public void ThrowMyException()
{
throw new MyException();
}
}
public class MyException : Exception
{
public MyException()
{
this.Data.Add("MyKey1", 212);
this.Data.Add("MyKey2", 2121);
}
}
Run Code Online (Sandbox Code Playgroud)
然后进行测试以验证MyKey1 = 212和MyKey2 = 2121:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
MyClass classUnderTest = new MyClass();
Action test = () =>
{
classUnderTest.ThrowMyException();
};
test.ShouldThrow<MyException>() //.And.Data.Keys.Should().Contain("")
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试数据包含值为212的MyKey1和值为2121的MyKey2.
c# ×10
.net ×3
unit-testing ×3
collections ×1
nunit ×1
tdd ×1
testing ×1
xunit ×1
xunit.net ×1