有没有办法用shouldly测试异常消息?
一个例子:
public class MyException: Exception{
}
Run Code Online (Sandbox Code Playgroud)
要测试的方法:
public class ClassUnderTest
{
public void DoSomething()
{
throw new MyException("Message");
}
}
Run Code Online (Sandbox Code Playgroud)
我通常会以这种方式测试它:
[TestMethod]
public void Test()
{
try
{
new ClassUnderTest().DoSomething();
Assert.Fail("Exception not thrown");
} catch(MyException me)
{
Assert.AreEqual("Message", me.Message);
}catch(Exception e)
Assert.Fail("Wrong exception thrown");
}
}
Run Code Online (Sandbox Code Playgroud)
我应该现在可以测试是否抛出异常:
[TestMethod]
public void TestWithShouldly()
{
Should.ThrowException<MyException>(() => new ClassUnderTest().DoSomething());
}
Run Code Online (Sandbox Code Playgroud)
但是我如何测试异常的消息呢?
.NET的Shouldly断言库以某种方式知道调用断言方法的表达式,因此它能够将它显示在消息中.我试图找出它是如何工作但在源代码中丢失了.我怀疑它会查看已编译的代码,但我真的很想知道这是如何发生的.从文档中
map.IndexOfValue("boo").ShouldBe(2); // -> map.IndexOfValue("boo") should be 2 but was 1
Run Code Online (Sandbox Code Playgroud)
不知何故应该知道表达式map.IndexOfValue("boo")并且能够在测试失败消息中显示它.有谁知道这是怎么回事?
我在我的 xUnit 测试中使用了优秀的Shouldly库,我发现自己在不同的测试中使用了断言的集合序列,所以我将它们组合成新的断言扩展方法 - 但是当我这样做时,我丢失了Shouldly上下文断言消息。
这是我的旧代码,它Shouldly用于在Shouldly断言错误中包含源级信息和调用站点上下文:
[Fact]
public void Dict_should_contain_valid_Foobar_Bar_entry()
{
IDictionary<String,Bar> dict = ...
dict.TryGetValue( "Foobar", out Bar bar ).ShouldBeTrue();
bar.ShouldNotBeNull();
bar.ChildList.Count.ShouldBe( expected: 3 );
bar.Message.ShouldBeNull();
}
[Fact]
public void Dict_should_contain_valid_Barbaz_Bar_entry()
{
IDictionary<String,Bar> dict = ...
dict.TryGetValue( "Barbaz", out Bar bar ).ShouldBeTrue();
bar.ShouldNotBeNull();
bar.ChildList.Count.ShouldBe( expected: 3 );
bar.Message.ShouldBeNull();
}
Run Code Online (Sandbox Code Playgroud)
我在同一个项目中将它转换为这个新的扩展方法:
public static void ShouldBeValidBar( this IDictionary<String,Bar> dict, String barName )
{
dict.ShouldNotBeNull();
dict.TryGetValue( barName, out Bar bar ).ShouldBeTrue();
bar.ShouldNotBeNull(); …Run Code Online (Sandbox Code Playgroud) 在我的测试构造函数中,我设置了我的传奇:
public When_Testing_My_Saga()
{
_mySaga = new MySaga
{
Data = new MySaga.MySagaData()
};
}
Run Code Online (Sandbox Code Playgroud)
我的测试断言未接收重要数据会导致失败:
[Fact]
public void Not_Providing_Data_Should_Cause_A_Failure()
{
var context = new TestableMessageHandlerContext();
Should.Throw<NoDataProvidedFailure>(() =>
{
_mySaga.Handle(new ImportDataReadMessage
{
ImportantData = null
}, context).ConfigureAwait(false);
});
}
Run Code Online (Sandbox Code Playgroud)
SqlSaga中的实际代码:
public async Task Handle(ImportantDataReadMessage message, IMessageHandlerContext context)
{
if (message.ImportantData == null)
{
throw new NoDataProvidedFailure("Important data was not provided.");
}
await context.Send(Endpoints.MyEndpoint, new DoStuffWhenImportantDataProvided
{
Reference = message.Reference
});
}
Run Code Online (Sandbox Code Playgroud)
引发预期的失败,但测试表明相反:
shouldly.ShouldAssertException
_mySaga.Handle(new ImportantDataReadMessage { Reference = string.Empty, ImportantData …
我正在使用 ShouldBe 运行 c# 测试,并且我有以下代码:
int x = 3;
int y = 3;
x.ShouldBeSameAs(y);
Run Code Online (Sandbox Code Playgroud)
问题是它抛出异常:
'Shouldly.ShouldAssertException' 类型的异常出现在 Shouldly.dll 中,但未在用户代码中处理
附加信息:x
should be same as
Run Code Online (Sandbox Code Playgroud)
3
but was
Run Code Online (Sandbox Code Playgroud)
3
如何使用 ShouldBe 测试整数的相等性?
我在课堂上有一个房产
public class User {
public string FiscalCode {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我想用两个条件测试属性财务代码.如果财务代码为空或财务代码由方法验证,则测试正常
public bool FiscalCodeIsCorrect(string fiscalcode)
{
....
}
Run Code Online (Sandbox Code Playgroud)
如果验证了两个条件中的一个,我该如何在一条线上进行测试?
我想在测试项目中使用这个条件,所以代码行可以
user.FiscalCode.ShouldBeOneOf()
Run Code Online (Sandbox Code Playgroud)
但我不能因为null而且string是两种不同的类型.
如何根据某些属性进行单元测试检查对象列表是否不包含重复元素。
这是我尝试做的:
[Fact]
public void RecupererReferentielContactClient_CasNominal_ResultOk()
{
// Arange
var contactCoreService = Resolve<IContactCoreService>();
int clientId = 56605;
ICollection<Personne> listPersone = new List<Personne>();
// Act
WithUnitOfWork(() => listPersone = contactCoreService.RecupererReferentielDeContactClient(clientId));
// Assert
listPersone.ShouldSatisfyAllConditions(
() => listPersone.ShouldNotBeNull(),
() => listPersone.ShouldBeUnique());
}
Run Code Online (Sandbox Code Playgroud)
如何使用 shouldly 进行单元测试?