Jos*_*rke 15 .net unit-testing mstest fluent-interface fluent
我最近接触过nUnit中的流畅界面,我喜欢它; 但是,我正在使用msTest.
有没有人知道是否有一个流畅的界面,无论是测试框架不可知还是msTest?
Den*_*men 17
请参阅流利断言.你可以做类似的事情
"ABCDEFGHI".Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);
new[] { 1, 2, 3 }.Should().HaveCount(4, "because we thought we put three items in the
collection"))
dtoCollection.Should().Contain(dto => dto.Id != null);
collection.Should().HaveCount(c => c >= 3);
dto.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(customer);
dt1.Should().BeWithin(TimeSpan.FromHours(50)).Before(dt2);
Action action = () => recipe.AddIngredient("Milk", 100, Unit.Spoon);
action
.ShouldThrow<RuleViolationException>()
.WithMessage("Cannot change the unit of an existing ingredient")
.And.Violations.Should().Contain(BusinessRule.CannotChangeIngredientQuanity
Run Code Online (Sandbox Code Playgroud)
见http://sharptestex.codeplex.com/
注意:SharpTestsEx似乎不再积极开发,推荐替代方案是http://www.fluentassertions.com/.
SharpTestsEx(Sharp Tests Extensions)是一组可扩展的扩展.主要目标是编写简短的断言,其中Visual Studio IDE intellisense是您的指南.#TestsEx可以与NUnit,MsTests,xUnit,MbUnit ...一起使用,甚至可以在Silverlight中使用.
强类型断言的语法示例(取自网页):
true.Should().Be.True();
false.Should().Be.False();
const string something = "something";
something.Should().Contain("some");
something.Should().Not.Contain("also");
something.ToUpperInvariant().Should().Not.Contain("some");
something.Should()
.StartWith("so")
.And
.EndWith("ing")
.And
.Contain("meth");
something.Should()
.Not.StartWith("ing")
.And
.Not.EndWith("so")
.And
.Not.Contain("body");
var ints = new[] { 1, 2, 3 };
ints.Should().Have.SameSequenceAs(new[] { 1, 2, 3 });
ints.Should().Not.Have.SameSequenceAs(new[] { 3, 2, 1 });
ints.Should().Not.Be.Null();
ints.Should().Not.Be.Empty();
ints.Should()
.Contain(2)
.And
.Not.Contain(4);
(new int[0]).Should().Be.Empty();
Run Code Online (Sandbox Code Playgroud)