我想"结合"Fluent Assertion的集合断言和属性断言,例如断言两个IEnumerable是成对相等的,使用逐个属性(可能是"嵌套")比较(即结构相等,用函数式语言说法).
具体例子:
var dic = new Dictionary<int, string>() { {1, "hi"}, {2, "bye" } };
var actual = dic.ToSelectListItems(0).OrderBy(si => si.Text);
var expected = new List<SelectListItem>() {
new SelectListItem() {Selected = false, Text="bye", Value="2"},
new SelectListItem() {Selected = false, Text="hi", Value="1"}
};
Run Code Online (Sandbox Code Playgroud)
在这里,我写了一个扩展方法ToSelectListItems,其转换Dictionary成IEnumerable的SelectListItemS(从ASP.NET MVC).我想断言actual并且expected"在结构上"是相等的,注意引用类型SelectListItem不会覆盖Equals,因此默认使用引用相等.
更新
目前使用以下手动解决方案,仍然希望在FluentAssertions中内置更好的内容:
public static void ShouldBeStructurallyEqualTo<T, U>(this IEnumerable<T> actual, IEnumerable<U> expected) {
actual.Should().HaveCount(expected.Count());
actual.Zip(expected).ForEach(pair => pair.Item1.ShouldHave().AllProperties().IncludingNestedObjects().EqualTo(pair.Item2));
} …Run Code Online (Sandbox Code Playgroud) 我正在实现一个不可变字典的特例,为了方便实现IEnumerable<KeyValuePair<Foo, Bar>>.通常修改字典的操作应该返回一个新实例.
到现在为止还挺好.但是当我尝试为类编写一个流畅的单元测试时,我发现我尝试过的两个流畅的断言库(Should和Fluent Assertions)都不支持对NotBeSameAs()实现的对象的操作IEnumerable- 除非你第一次执行他们来Object.
当我第一次碰到这个时,我认为它只是框架中的一个洞,但当我看到Fluent Assertions有同样的洞时,它让我觉得(因为我是C#的相对新人)我可能会遗漏一些关于C#集合的概念 - 当我提交问题时,应该隐含的作者.
显然还有其他方法来测试这个 - 投射Object和使用NotBeSameAs(),只是使用Object.ReferenceEquals,无论如何 - 但如果有充分的理由不这样做,我想知道那是什么.
我有以下情况:
class A
{
}
class B : A
{
}
Run Code Online (Sandbox Code Playgroud)
我想断言 typeof(B) 的变量可以分配给 typeof(A) 变量。如何通过流畅的断言做到这一点?
我有一堂课:
public class TestClass
{
public int Id { get; set; }
public int CampusId { get; set; }
public int CurrentStudentCount { get; set; }
public int MaxStudentCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及该类的对象集合:
var collection = new[]
{
new TestClass
{
Id = 55,
CampusId = 38,
CurrentStudentCount = 1,
MaxStudentCount = 2
},
new TestClass
{
Id = 127,
CampusId = 38,
CurrentStudentCount = 2,
MaxStudentCount = 2
},
new TestClass
{
Id = 126,
CampusId = …Run Code Online (Sandbox Code Playgroud) 我正在使用F#,Visual Studio单元测试框架(又名MSTest)和FluentAssertions为我的F#库编写单元测试.
测试方法应该具有void或Task的返回类型.在C#中这很容易:
[TestMethod]
public void TestMethod1()
{
false.Should().BeFalse();
}
Run Code Online (Sandbox Code Playgroud)
在F#我有以下内容:
[<TestMethod>]
member this.TestMethod1() =
false.Should().BeFalse(null)
|> ignore
Run Code Online (Sandbox Code Playgroud)
否则返回类型更改为,FluentAssertions.PrimitivesBooleanAssertions因此Test Runner看不到它.
如何避免|> ignore每次测试结束?
f# unit-testing void vs-unit-testing-framework fluent-assertions
我正在处理许多项目,每个项目都包含一个DateProcessed属性(可以为空的DateTime),并希望Assert将该属性设置为当前日期.当它完成处理程序时,日期都略有不同.
我想测试所有DateProcessed属性具有相对性(100ms)最近的DateTime.
Fluent Assertions具有.BeCloseTo方法,适用于单个项目.但我想将它用于整个系列.但是在查看集合时,它不能通过Contains()获得.
一个简化的例子......
[TestFixture]
public class when_I_process_the_items
{
[SetUp]
public void context()
{
items = new List<DateTime?>(new [] { (DateTime?)DateTime.Now, DateTime.Now, DateTime.Now } );
}
public List<DateTime?> items;
[Test]
public void then_first_item_must_be_set_to_the_current_time()
{
items.First().Should().BeCloseTo(DateTime.Now, precision: 100);
}
[Test]
public void then_all_items_must_be_set_to_the_current_time()
{
items.Should().Contain .... //Not sure? :(
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用FluentAssertions来检查我的UnitTest,项目列表中属性的类型是某种类型.
myObj.Items.OfType<TypeA>().Single()
.MyProperty1.GetType()
.Should().BeOfType<TypeB>();
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的测试失败,出现以下错误消息:
预期类型为TypeB,但找到System.RuntimeType.
它为什么说,它找到了System.RuntimeType?我使用调试器来验证,这MyProperty1是类型TypeB,它是......我使用.BeOfType<>错了吗?
提前致谢
我在我的单元测试流利断言工作,但是使用的ShouldBeEquivalentTo,ShouldAllBeEquivalentTo以及BeEquivalentTo目前还不清楚.
例如; 所有以下语句都通过,因此函数看起来是等效的.
List<string> a = new List<string>() { "james", "wood" };
List<string> b = new List<string>() { "james", "wood" };
a.ShouldBeEquivalentTo(b);
a.ShouldAllBeEquivalentTo(b);
a.Should().BeEquivalentTo(b);
Run Code Online (Sandbox Code Playgroud)
我为什么要使用一个而不是另一个?
我一直在摆弄 c#9 的一些新功能,但遇到了一些不那么有趣的事情。如果我尝试在具有 DateTimeOffset 的记录上使用 Should().BeEquivalentTo(),并将 DateTimeOffset 的“使用”选项设置为使用 BeCloseTo,则即使 DateTimeOffset 在允许的精度范围内,测试也会失败。有没有办法让它发挥作用(无需将记录更改为班级哈哈)?非常感谢!
例子:
public class ExampleTest
{
[Fact]
public void FunWithRecords()
{
var thing1 = new Thing
{
SomeDate = DateTimeOffset.UtcNow
};
var thing2 = new Thing
{
SomeDate = DateTimeOffset.UtcNow.AddSeconds(2)
};
thing1.Should().BeEquivalentTo(thing2, o =>
o.Using<DateTimeOffset>(ctx =>
ctx.Subject.Should().BeCloseTo(ctx.Expectation, 10000))
.WhenTypeIs<DateTimeOffset>());
}
}
public record Thing
{
public DateTimeOffset SomeDate {get; init;}
}
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 排序应该相互结合应用,而不仅仅是按顺序应用?