1818``` cs
1919TestAA
2020 .Act (テスト対象コード)
21- .Assert (テスト対象コードの戻り値の検証, 例外の検証, その他の検証 );
21+ .Assert (テスト対象コードの戻り値の検証, 例外の検証);
2222```
2323
2424Arrange に相当する処理は ` TestAA.Act() ` の呼び出しより前に記述します。
@@ -30,7 +30,7 @@ Arrange に相当する処理は `TestAA.Act()` の呼び出しより前に記
3030TestAA .Act (.. .)
3131```
3232
33- ` Act() ` の引数には、テスト対象となるメソッドまたはコードのラムダ式やデリゲートを渡して下さい。テストの対象ではないメソッドまたはコードも含めますと 、そこから発生した例外がテスト対象コードから生じたものとして扱われてしまい、正しい検証が行えなくなります。
33+ ` Act() ` の引数には、テスト対象となるメソッドまたはコードのラムダ式やデリゲートを渡して下さい。テストの対象ではないメソッドやコードを含めますと 、そこから発生した例外がテスト対象コードから生じたものとして扱われてしまい、正しい検証が行えなくなります。
3434``` cs
3535TestAA .Act (() => { /* ここでテスト対象のメソッドを呼ぶ */ })
3636```
@@ -39,63 +39,72 @@ TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ })
3939``` cs
4040 .Act (() => int .Parse (" 123" ))
4141 .Assert (
42- @return : ret => { /* ここで戻り値の検証 */ },
43- exception : ex => { /* ここで例外の検証。省略した場合、Act() で例外が生じていれば再スローされる */ },
44- others : () => { /* ここで上記以外の検証。不要なら省略 */ }
42+ @return : ret => { /* ここで戻り値の検証。Act で例外が生じた場合は戻り値が無いので呼ばれない */ },
43+ exception : ex => { /* ここで例外の検証 */ }
4544 );
4645```
4746
47+ 検証はラムダ式やデリゲートではなく値を直接入力する事でも可能です。テストの失敗は、既定では ` TestAssertFailedException ` のスローによって通知されます。
48+ ``` cs
49+ TestAA .Act (() => int .Parse (" 123" )).Assert (123 ); // OK
50+ TestAA .Act (() => int .Parse (" abc" )).Assert (ret => { }, new FormatException ()); // OK
51+ TestAA .Act (() => int .Parse (" abc" )).Assert (123 ); // TestAssertFailedException
52+ ```
53+
4854
49- ## Usage
55+ ## Examples
56+ ### Basic
5057``` cs
51- public void IntParseTest () {
52- // Success
53- TestAA .Act (() => int .Parse (" 123" )).Assert (
54- ret => ret .Is (123 )
55- );
58+ TestAA .Act (() => int .Parse (" 123" )).Assert (123 );
59+ ```
60+ ### Exception
61+ ``` cs
62+ TestAA .Act (() => int .Parse (" abc" )).Assert (ret => { }, exception : new FormatException ());
63+ ```
5664
57- // FormatException
58- TestAA .Act (() => int .Parse (" abc" )).Assert (
59- ret => { },
60- ex => ex ? .GetType ().Is (typeof (FormatException ))
61- );
62- }
65+ ### Out parameter
66+ ``` cs
67+ int result = default ;
68+ TestAA .Act (() => int .TryParse (" 123" , out result )).Assert (true );
69+
70+ // Additional Assert
71+ Assert .AreEqual (123 , result );
6372```
6473
65- 下記は * MSTest * を利用した、より実践的な例です:
74+ ### Lambda assert
6675``` cs
67- [DataTestMethod ]
68- [DataRow (0 , null , null , typeof (ArgumentNullException ))]
69- [DataRow (1 , " 123" , 123 , null )]
70- [DataRow (2 , " abc" , null , typeof (FormatException ))]
71- public void IntParseTest (int testNumber , string input , int expected , Type expectedExceptionType ) {
72- var msg = " No." + testNumber ;
76+ TestAA .Act (() => int .Parse (" 123" )).Assert (
77+ @return : ret => Assert .AreEqual (123 , ret )
78+ , exception : ex => Assert .IsNull (ex )
79+ );
80+ ```
7381
74- TestAA .Act (() => int .Parse (input )).Assert (
75- ret => Assert .AreEqual (expected , ret , msg ),
76- ex => Assert .AreEqual (expectedExceptionType , ex ? .GetType (), msg )
77- );
82+ ### Replace default assert
83+ ``` cs
84+ class MSTestAssert : TestAssert {
85+ public override void Is <T >(T actual , T expected , string message ) {
86+ Assert .AreEqual (expected , actual , message );
87+ }
7888}
89+ .. .
90+ TestAA .TestAssert = new MSTestAssert ();
91+
92+ TestAA .Act (() => int .Parse (" 123" )).Assert (123 ); // Assert.AreEqual()
7993```
80- または
94+
95+ ### Test cases
8196``` cs
82- [TestMethod ]
83- public void IntParseTest () {
84- Action TestCase (int testNumber , string input , int expected , Type expectedExceptionType = null ) => () => {
85- var msg = " No." + testNumber ;
86-
87- TestAA .Act (() => int .Parse (input )).Assert (
88- ret => Assert .AreEqual (expected , ret , msg ),
89- ex => Assert .AreEqual (expectedExceptionType , ex ? .GetType (), msg )
90- );
91- };
92-
93- foreach (var action in new [] {
94- TestCase ( 0 , null , expected : 0 , expectedExceptionType : typeof (ArgumentNullException )),
95- TestCase ( 1 , " abc" , expected : 0 , expectedExceptionType : typeof (FormatException )),
96- TestCase ( 2 , " 123" , expected : 123 ),
97- }) { action (); }
98- }
97+ Action TestCase (int testNumber , string input , int expected , Exception expectedException = null ) => () => {
98+ var msg = " No." + testNumber ;
99+
100+ TestAA .Act (() => int .Parse (input )).Assert (expected , expectedException , msg );
101+ };
102+
103+ foreach (var action in new [] {
104+ TestCase ( 0 , null , expected : 0 , expectedException : new ArgumentNullException ()),
105+ TestCase ( 1 , " abc" , expected : 0 , expectedException : new FormatException ()),
106+ TestCase ( 2 , " 123" , expected : 123 ),
107+ }) { action (); }
99108```
100109
101110
0 commit comments