shouldly

How does the Shouldly assertion library know the expression the assertion was applied to?

五迷三道 提交于 2020-12-08 05:30:42
问题 The Shouldly assertion library for .NET somehow knows what expression the assertion method was called on so it is able to display it into the message. I tried to find out how it works but got lost in the source code. I suspect it looks into the compiled code but I would really like to see how this happens. From the documentation map.IndexOfValue("boo").ShouldBe(2); // -> map.IndexOfValue("boo") should be 2 but was 1 Somehow Shouldly knows the expression map.IndexOfValue("boo") and was able to

How does the Shouldly assertion library know the expression the assertion was applied to?

我怕爱的太早我们不能终老 提交于 2020-12-08 05:29:11
问题 The Shouldly assertion library for .NET somehow knows what expression the assertion method was called on so it is able to display it into the message. I tried to find out how it works but got lost in the source code. I suspect it looks into the compiled code but I would really like to see how this happens. From the documentation map.IndexOfValue("boo").ShouldBe(2); // -> map.IndexOfValue("boo") should be 2 but was 1 Somehow Shouldly knows the expression map.IndexOfValue("boo") and was able to

C# in my unit test i want to see if it is null or an empty string

送分小仙女□ 提交于 2019-12-24 03:07:05
问题 I'm using Machine spec / Shouldly and testing a mapping profile (mundane work). The mapping expectations are sometimes inconsistent. Sometimes I am seeing an "" explicit empty string get returned and sometimes it's a null value. Since we're mapping to a spreadsheet it doesn't matter on that front but it gets a little annoying when asserting. Is there a way in Machine spec to do something like assertionValue.ShouldEqual(null || "") ? 回答1: Assert on the outcome of string.IsNullOrEmpty() 来源:

Testing Exception Messages with Shouldly

核能气质少年 提交于 2019-12-09 14:02:19
问题 Is there a way to test the exception messages with shouldly? An example: public class MyException: Exception{ } The method to be tested: public class ClassUnderTest { public void DoSomething() { throw new MyException("Message"); } } I would usually test this in this way: [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

Testing Exception Messages with Shouldly

被刻印的时光 ゝ 提交于 2019-12-03 22:08:51
Is there a way to test the exception messages with shouldly? An example: public class MyException: Exception{ } The method to be tested: public class ClassUnderTest { public void DoSomething() { throw new MyException("Message"); } } I would usually test this in this way: [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"); } } With shouldly I can now test if a exception is thrown: [TestMethod] public void