Questions tagged «nunit-3.0»

3
NUnit 3.0和Assert.Throws
我正在用NUnit 3.0编写一些单元测试,与v2.x不同,ExpectedException()它已从库中删除。 根据这个答案,我可以肯定地看到尝试专门捕获测试中期望系统抛出异常的地方的逻辑(而不是仅仅说“测试中的任何地方”)。 但是,我倾向于非常明确地说明我的“安排”,“行动”和“声明”步骤,因此这是一个挑战。 我曾经做过类似的事情: [Test, ExpectedException(typeof(FormatException))] public void Should_not_convert_from_prinergy_date_time_sample1() { //Arrange string testDate = "20121123120122"; //Act testDate.FromPrinergyDateTime(); //Assert Assert.Fail("FromPrinergyDateTime should throw an exception parsing invalid input."); } 现在,我需要执行以下操作: [Test] public void Should_not_convert_from_prinergy_date_time_sample2() { //Arrange string testDate = "20121123120122"; //Act/Assert Assert.Throws<FormatException>(() => testDate.FromPrinergyDateTime()); } 在我看来,这并不可怕,但是却使《法案》和《断言》混为一谈。(显然,对于这个简单的测试,它并不难遵循,但是在大型测试中可能更具挑战性)。 我有一位同事建议我Assert.Throws完全摆脱掉,然后做些类似的事情: [Test] public void Should_not_convert_from_prinergy_date_time_sample3() …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.