1 00:00:00,000 --> 00:00:02,410 [No Audio] 2 00:00:02,410 --> 00:00:03,466 Let's take a look now what we 3 00:00:03,466 --> 00:00:05,554 expect to have in our unit test methods. 4 00:00:05,554 --> 00:00:07,520 This will give us a good template on how to code 5 00:00:07,520 --> 00:00:10,000 them, which we can use as we're doing our unit testing. 6 00:00:10,000 --> 00:00:11,864 If you keep this mental template in your 7 00:00:11,864 --> 00:00:13,856 head when you're coding a unit test, you 8 00:00:13,856 --> 00:00:15,466 can't go far wrong in my opinion. 9 00:00:15,466 --> 00:00:17,864 It keeps things nice and simple and provide us with 10 00:00:17,864 --> 00:00:20,890 a structure we can use consistently across all tests. 11 00:00:20,890 --> 00:00:23,660 So it's generally accepted that there are two main 12 00:00:23,660 --> 00:00:26,434 types of logical structure for our unit test methods. 13 00:00:26,434 --> 00:00:28,300 I'll just say unit test now for this, 14 00:00:28,300 --> 00:00:29,456 but just know i'm referring to 15 00:00:29,456 --> 00:00:31,964 the methods which form a single unit test, i.e. 16 00:00:31,964 --> 00:00:33,788 Test a specific aspect of your system and 17 00:00:33,788 --> 00:00:35,986 the test or the production class you're testing, 18 00:00:35,986 --> 00:00:38,335 if you prefer to call it that. Both of these 19 00:00:38,335 --> 00:00:40,378 approaches are pretty much the same, but just 20 00:00:40,378 --> 00:00:42,394 look at testing from a slightly different perspective 21 00:00:42,394 --> 00:00:44,986 and are better suited to different audiences. 22 00:00:44,986 --> 00:00:46,679 We'll come to this in a second, but the two 23 00:00:46,679 --> 00:00:48,766 choices you have for the logical structure of our 24 00:00:48,766 --> 00:00:51,956 unit tests are either Arrange/ Act/ Assert sometimes 25 00:00:51,956 --> 00:00:54,140 referred to as AAA and Given-When- 26 00:00:54,140 --> 00:00:56,804 Then. Let's think for a second about high 27 00:00:56,804 --> 00:00:58,472 level things we need to have happen 28 00:00:58,472 --> 00:01:00,778 when we write a test from a logical viewpoint, 29 00:01:00,778 --> 00:01:02,924 the method we're testing needs to be called. 30 00:01:02,924 --> 00:01:04,604 This is the thing we're testing, so 31 00:01:04,604 --> 00:01:06,290 we definitely need that to happen. 32 00:01:06,290 --> 00:01:08,576 Also, the point of calling the method, is so we 33 00:01:08,576 --> 00:01:10,796 can check what the method did after the fact. 34 00:01:10,796 --> 00:01:12,704 So we need to have some code which 35 00:01:12,704 --> 00:01:14,984 allows us to observe what happened. So we 36 00:01:14,984 --> 00:01:17,453 can evaluate whether the test passed or failed. 37 00:01:17,453 --> 00:01:19,508 That only leaves the other aspect, which is 38 00:01:19,508 --> 00:01:21,344 to set up the context correctly for the 39 00:01:21,344 --> 00:01:23,773 test, before we invoke the method. Now 40 00:01:23,773 --> 00:01:26,564 if we rearrange these into their correct order, which 41 00:01:26,564 --> 00:01:29,458 is set up, invoke the method, then check the results. 42 00:01:29,458 --> 00:01:32,870 We have the AAA formula for Arrange/ Act/ Assert. 43 00:01:32,870 --> 00:01:35,746 In this scheme, we have an Arrange phase. 44 00:01:35,746 --> 00:01:38,432 This is where we put code which will set up our test. 45 00:01:38,432 --> 00:01:40,676 For example, put the system on the test into a 46 00:01:40,676 --> 00:01:42,667 particular state prior to inverting the method being 47 00:01:42,667 --> 00:01:45,298 tested. This might be setting it up with references 48 00:01:45,298 --> 00:01:47,264 to mock collaborators that it will work with 49 00:01:47,264 --> 00:01:49,354 during the execution of the method, and similarly 50 00:01:49,354 --> 00:01:51,598 setting up any state or behavior we require 51 00:01:51,598 --> 00:01:54,358 those mock collaborators to exhibit during the tests. 52 00:01:54,358 --> 00:01:56,818 Once we have that set up done, our Arrange phase, 53 00:01:56,818 --> 00:01:59,098 then we can actually invoke the method we're testing. 54 00:01:59,098 --> 00:02:00,538 This is the Act phase. 55 00:02:00,538 --> 00:02:01,616 We really just want to have 56 00:02:01,616 --> 00:02:03,188 a single method being called here. 57 00:02:03,188 --> 00:02:04,678 Don't call multiple methods. 58 00:02:04,678 --> 00:02:06,490 If you do, you're allowing the implementation 59 00:02:06,490 --> 00:02:08,566 detail of the production class being tested, 60 00:02:08,566 --> 00:02:10,160 to leak into the test case. 61 00:02:10,160 --> 00:02:11,734 So the Act phase is just one method 62 00:02:11,734 --> 00:02:14,350 invocation, the one method that's being tested. 63 00:02:14,350 --> 00:02:16,784 Then once we've invoked the method, we can 64 00:02:16,784 --> 00:02:18,538 do the checking part and check the results. 65 00:02:18,538 --> 00:02:20,158 This is the Assert phase. 66 00:02:20,158 --> 00:02:22,376 Like its name suggests in this part, this is 67 00:02:22,376 --> 00:02:24,284 where you'd put assertions about the state of the 68 00:02:24,284 --> 00:02:26,689 world after the method has been invoked. This 69 00:02:26,689 --> 00:02:28,640 can either be Asserting something about the return 70 00:02:28,640 --> 00:02:30,752 parameter of the method called, if it returns some 71 00:02:30,752 --> 00:02:33,056 results, or might be assertions around the state of 72 00:02:33,056 --> 00:02:35,362 the objects passed into the method called, or possibly 73 00:02:35,362 --> 00:02:37,758 verifying the calls or data, etc, which were 74 00:02:37,784 --> 00:02:39,605 passed to the mock collaborators. 75 00:02:39,605 --> 00:02:41,938 Now there are two schools of thought on unit testing. 76 00:02:41,938 --> 00:02:43,198 State based verification, 77 00:02:43,198 --> 00:02:46,234 you do these with Assertions and behavior verification, 78 00:02:46,234 --> 00:02:48,706 you do these with checking the method calls on mocks. 79 00:02:48,706 --> 00:02:51,401 We won't get into this now, as it's a bit advanced, but 80 00:02:51,401 --> 00:02:53,534 however you're checking the results, just understand 81 00:02:53,534 --> 00:02:55,751 that you put them in the assert phase of the unit 82 00:02:55,751 --> 00:02:58,292 test. Now that we know what a Arrange/ Act/ Assert 83 00:02:58,292 --> 00:03:00,760 is, what about this Given-When-Then format? 84 00:03:00,760 --> 00:03:03,026 Well, this is a style referred to as behavior driven 85 00:03:03,026 --> 00:03:05,685 testing. In behavioral driven testing, what you're 86 00:03:05,685 --> 00:03:07,724 doing is looking at the system from the behavior you 87 00:03:07,724 --> 00:03:09,980 want it to have. Breaking it down. 88 00:03:09,980 --> 00:03:12,548 we have the phases as first off, have a Given 89 00:03:12,548 --> 00:03:14,996 phase which sets up the preconditions or the state of 90 00:03:14,996 --> 00:03:17,979 the system before we invoke the behavior being tested. 91 00:03:17,979 --> 00:03:21,163 Next, have a When phase which performs the behavior, 92 00:03:21,163 --> 00:03:23,371 and finally have a Then phase which asserts the 93 00:03:23,371 --> 00:03:25,568 state of the union after the behavior has been done. 94 00:03:25,568 --> 00:03:27,886 In other words, looks at the post conditions. 95 00:03:27,886 --> 00:03:29,936 This notion of pre and post conditions, by the 96 00:03:29,936 --> 00:03:31,868 way, is a good way of looking at things. 97 00:03:31,868 --> 00:03:33,236 Preconditions are what you want to 98 00:03:33,236 --> 00:03:34,580 be true before the test. 99 00:03:34,580 --> 00:03:36,200 Post conditions are what you're looking 100 00:03:36,200 --> 00:03:37,736 to be true after the test. 101 00:03:37,736 --> 00:03:39,442 You could have, for example, a test scenario 102 00:03:39,442 --> 00:03:41,984 like this, given the user is logged in. 103 00:03:41,984 --> 00:03:43,424 When the user logs out, then 104 00:03:43,424 --> 00:03:45,285 the user cannot make a purchase. 105 00:03:45,285 --> 00:03:48,404 Tests like these or more specifically, using Given When 106 00:03:48,404 --> 00:03:50,996 Then wording usually lend themselves to being at a 107 00:03:50,996 --> 00:03:53,600 higher level of abstraction than a simple unit test. 108 00:03:53,600 --> 00:03:56,276 In fact, Given unit tests are more low level anyway. 109 00:03:56,276 --> 00:03:58,376 Focusing on a single class, you tend to 110 00:03:58,376 --> 00:04:00,572 encounter Arrange/ Act/ Assert a lot more. 111 00:04:00,572 --> 00:04:02,530 Nevertheless, it's a valid style of writing 112 00:04:02,530 --> 00:04:04,450 test cases, so it's worth mentioning. 113 00:04:04,450 --> 00:04:06,286 Typically, you'll find these more in tests, 114 00:04:06,286 --> 00:04:07,784 which are for business consumption by 115 00:04:07,784 --> 00:04:10,342 business analysts, testers, and product owners. 116 00:04:10,342 --> 00:04:12,224 In other words, you find them more in integration and 117 00:04:12,224 --> 00:04:15,178 acceptance tests, so it's less common in unit tests. 118 00:04:15,178 --> 00:04:16,868 So which one should you use? 119 00:04:16,868 --> 00:04:18,091 Well, it's up to you, really. 120 00:04:18,091 --> 00:04:20,613 Arrange/ Act/ Assert is popular with developers. 121 00:04:20,613 --> 00:04:22,305 It's a bit more technical oriented 122 00:04:22,305 --> 00:04:23,613 in its choice of wording. 123 00:04:23,613 --> 00:04:25,019 The focus is on what the unit test 124 00:04:25,019 --> 00:04:27,812 does by its implementation. Given-When-Then 125 00:04:27,812 --> 00:04:29,696 however, it's popular with business analysts and 126 00:04:29,696 --> 00:04:31,424 testers because it focuses more on looking 127 00:04:31,424 --> 00:04:33,394 at the behavioral aspects, and it reads 128 00:04:33,394 --> 00:04:35,638 easier for those less technical audiences. 129 00:04:35,638 --> 00:04:36,860 Whichever one you use, though, 130 00:04:36,860 --> 00:04:38,120 the structure is the same. 131 00:04:38,120 --> 00:04:39,992 Just make sure you're consistent in using them. 132 00:04:39,992 --> 00:04:42,200 In other words, pick one style and stick to it. 133 00:04:42,200 --> 00:04:44,314 I usually do Arrange/ Act/ Assert and have comments 134 00:04:44,314 --> 00:04:46,412 in the test code with those words so it's very 135 00:04:46,412 --> 00:04:48,898 clear which bit of code is doing which phase. 136 00:04:48,898 --> 00:04:50,576 It's a good practice to get into and 137 00:04:50,576 --> 00:04:51,848 you can even set up a template in 138 00:04:51,848 --> 00:04:54,497 your IDE to generate this automatically for you. 139 00:04:54,497 --> 00:04:57,106 So now we know how to structure our test methods 140 00:04:57,106 --> 00:04:59,606 with either Arrange/ Act/ Assert or Given-When-Then 141 00:04:59,606 --> 00:05:01,805 this should put you in a good place to write your tests 142 00:05:01,805 --> 00:05:04,616 from here. Just remember the different phases, stick to 143 00:05:04,616 --> 00:05:06,956 them and you'll be writing very clean, very focused and 144 00:05:06,956 --> 00:05:08,502 easy to understand test code.