1 00:00:00,420 --> 00:00:02,880 Our check for ship function is looking good, but 2 00:00:02,880 --> 00:00:05,400 we've only proven that it will report a miss. 3 00:00:05,400 --> 00:00:08,900 It tells us when no ship is located at our given coordinates, but 4 00:00:08,900 --> 00:00:13,710 does it also correctly tell us when a ship is located at or given coordinates? 5 00:00:13,710 --> 00:00:16,035 Let's write a new spec that would prove it. 6 00:00:16,035 --> 00:00:21,940 Okay, so back inside shiptest.js, I'll just copy my current spec for 7 00:00:21,940 --> 00:00:26,440 no ship present and paste a new spec, right below it. 8 00:00:26,440 --> 00:00:29,640 Then I'll adjust the description and expectation. 9 00:00:29,640 --> 00:00:33,760 So this description will say should correctly report, 10 00:00:33,760 --> 00:00:36,820 a ship located, at the given coordinates. 11 00:00:42,448 --> 00:00:45,820 This spec test, whether the function correctly finds a ship 12 00:00:45,820 --> 00:00:49,270 that is located at the given location. 13 00:00:49,270 --> 00:00:51,880 So we should change the argument we passed, check for 14 00:00:51,880 --> 00:00:55,120 ship, to match the players one ship location. 15 00:00:55,120 --> 00:00:58,130 So I'll change this to 0,0. 16 00:00:58,130 --> 00:01:01,006 Then I'll adjust the expected result to be true. 17 00:01:04,462 --> 00:01:07,494 So now, when I run npn test in the consul, 18 00:01:07,494 --> 00:01:13,250 the test report assertion error, expected,undefined to be true. 19 00:01:13,250 --> 00:01:17,010 Well currently the only timer function returns something, 20 00:01:17,010 --> 00:01:19,290 is when no ship is found. 21 00:01:19,290 --> 00:01:23,860 So maybe we should add a matching conditional to return true, 22 00:01:23,860 --> 00:01:25,240 when something is found. 23 00:01:26,920 --> 00:01:31,250 So in our check for ship function inside ship methods.J S, 24 00:01:31,250 --> 00:01:35,760 will say if no ship is present, Return false. 25 00:01:35,760 --> 00:01:40,714 Then we'll say else return true. 26 00:01:46,101 --> 00:01:48,170 All right, so let's run the test again. 27 00:01:51,420 --> 00:01:52,060 And cool. 28 00:01:52,060 --> 00:01:52,710 Now it works. 29 00:01:56,220 --> 00:01:58,970 But so far, we've only tested check for 30 00:01:58,970 --> 00:02:03,270 ship using a single ship with a single location. 31 00:02:03,270 --> 00:02:07,607 So we should expand our test to be sure it still works when a player has more than 32 00:02:07,607 --> 00:02:11,303 one ship, and when a ship is located at more than one coordinate. 33 00:02:15,166 --> 00:02:18,350 So again, back inside ship test.js. 34 00:02:18,350 --> 00:02:22,430 I'll copy the spec at the very top and 35 00:02:22,430 --> 00:02:27,250 paste a new one at the bottom, then revise my description and expectations. 36 00:02:30,250 --> 00:02:35,087 So I'll change this description to say should handle ships located at more than 37 00:02:35,087 --> 00:02:36,180 one coordinate. 38 00:02:44,640 --> 00:02:48,112 This spec will test the same behaviors we've already tested, 39 00:02:48,112 --> 00:02:51,110 but, with the ship having multiple locations. 40 00:02:51,110 --> 00:02:55,065 So, inside locations, I'll add a new coordinate of 0,1. 41 00:02:58,200 --> 00:03:01,270 Then I'll create new expectations for this location, and 42 00:03:01,270 --> 00:03:03,060 adjust the expected result to be true. 43 00:03:03,060 --> 00:03:08,640 So, I'll simply copy this expectation, and paste the new one right above it. 44 00:03:08,640 --> 00:03:14,970 And this will say expect check for ship player at 0,1 to be true. 45 00:03:20,720 --> 00:03:25,720 Now, you normally don't want your unit test to have more than one expectation, 46 00:03:25,720 --> 00:03:28,650 because it might be unclear to someone reading it what the test is 47 00:03:28,650 --> 00:03:29,840 supposed to proof. 48 00:03:29,840 --> 00:03:33,570 Now, in this case, since we have individual unit tests for positive and 49 00:03:33,570 --> 00:03:34,610 negative checks, 50 00:03:34,610 --> 00:03:38,810 I'm comfortable using both expectations in this unit test for brevity. 51 00:03:40,060 --> 00:03:43,801 In fact, I'll also add an expectation for the 0,0 location, 52 00:03:43,801 --> 00:03:45,782 just to make sure we can find both. 53 00:03:49,138 --> 00:03:51,200 And I'll set the expected result to be true. 54 00:03:53,260 --> 00:03:57,670 All right so let's save our file and run npm test in the console. 55 00:04:01,320 --> 00:04:03,070 And great that seems good so far. 56 00:04:04,510 --> 00:04:09,000 So it looks like check for ships doesn't break when we give it a bigger ship. 57 00:04:09,000 --> 00:04:11,110 So let's try more than one ship. 58 00:04:12,120 --> 00:04:16,190 So I'll copy the bottom spec inside ship_test, 59 00:04:16,190 --> 00:04:19,360 and paste a new spec right below it. 60 00:04:19,360 --> 00:04:24,269 And I'll change this spec's description to should handle checking multiple ships. 61 00:04:31,235 --> 00:04:33,125 Then I’ll change the expectations. 62 00:04:33,125 --> 00:04:35,345 But first I’ll just add one ship. 63 00:04:35,345 --> 00:04:38,325 To the ship's array with new coordinates. 64 00:04:38,325 --> 00:04:45,570 So I’ll copy the first location and paste the new one right below that. 65 00:04:45,570 --> 00:04:50,320 And change the coordinates to 1, 0 and 1, 1. 66 00:04:52,709 --> 00:04:56,436 Then right about the last expectation, I'll add two new expectations for 67 00:04:56,436 --> 00:04:58,400 this new location. 68 00:04:58,400 --> 00:05:02,470 So I'll copy one of these and paste it below. 69 00:05:02,470 --> 00:05:07,464 And for this I'll say expect (checkForShip(player at 1, 0 .to be true 70 00:05:07,464 --> 00:05:13,490 and will add another one that says expect check ForShip player at [1,1] to be true. 71 00:05:13,490 --> 00:05:20,030 All right so let's save this. 72 00:05:20,030 --> 00:05:23,280 Go over to the console and run npm test. 73 00:05:24,820 --> 00:05:27,490 So now our tests finally catch a bug. 74 00:05:27,490 --> 00:05:30,540 It looks like multiple ships don't work correctly. 75 00:05:30,540 --> 00:05:37,350 So the test passes the first expectations, which look at the first ship in the array. 76 00:05:37,350 --> 00:05:41,490 But the expectations aimed at the second ship fail. 77 00:05:41,490 --> 00:05:43,400 It says assertion error. 78 00:05:43,400 --> 00:05:45,830 Expected false to be true. 79 00:05:45,830 --> 00:05:49,280 But we must have made a mistake with our loop logic. 80 00:05:49,280 --> 00:05:52,390 Why else would everything work for the first ship, that first loop, and 81 00:05:52,390 --> 00:05:54,170 then break during the second? 82 00:05:54,170 --> 00:05:56,598 So let's take another look at our checkForShip function. 83 00:06:03,956 --> 00:06:08,440 And sure enough, I buried one of the return statements too deep. 84 00:06:08,440 --> 00:06:12,550 The loop will currently check for no matches after every ship, so that means 85 00:06:12,550 --> 00:06:16,380 that if the first ship isn't a match for the given location, then the function 86 00:06:16,380 --> 00:06:20,540 returns false right away before getting to loop again to the next ship. 87 00:06:21,540 --> 00:06:24,690 So it looks like we need to reverse this logic a bit. 88 00:06:24,690 --> 00:06:27,410 We want the function to return false for 89 00:06:27,410 --> 00:06:31,710 no matches only after it has checked every single ship. 90 00:06:31,710 --> 00:06:36,160 So that means it should return false only after the loop has finished. 91 00:06:36,160 --> 00:06:40,420 But it should return true immediately when it finds a match. 92 00:06:40,420 --> 00:06:44,160 So we can just switch things around to capture this idea better. 93 00:06:44,160 --> 00:06:49,015 So we'll say if ship present return true, and 94 00:06:49,015 --> 00:06:52,760 I'll delete this else condition. 95 00:06:53,790 --> 00:06:58,420 Then right before the closing bracket, we'll say return false. 96 00:06:59,950 --> 00:07:04,920 All right so let's save this and go over to the console and run npm test. 97 00:07:06,720 --> 00:07:09,830 And great, it looks like the tests all passed now. 98 00:07:11,000 --> 00:07:15,320 And to be double sure, we can expand the test suite a bit as a proof of concept. 99 00:07:15,320 --> 00:07:19,740 So let's add one more ship to the ship's array with new coordinates. 100 00:07:19,740 --> 00:07:24,230 So I'll copy one of the locations and paste the new one at the bottom. 101 00:07:26,000 --> 00:07:30,592 Then I'll change the coordinates to 2,0, 2,1. 102 00:07:30,592 --> 00:07:34,380 And I'll add two new coordinates. 103 00:07:34,380 --> 00:07:37,573 So this one will be 2,2. 104 00:07:37,573 --> 00:07:44,463 And the next will be 2,3. 105 00:07:44,463 --> 00:07:50,410 And then I'll go ahead and add a new expectation. 106 00:07:50,410 --> 00:07:55,657 That says expect check for ship for player at 2,3, 107 00:07:55,657 --> 00:07:58,590 to be true. 108 00:07:58,590 --> 00:08:01,910 So now if I go over to the council and run npm test. 109 00:08:03,155 --> 00:08:03,705 It works great. 110 00:08:06,115 --> 00:08:10,005 That was a lot of work, so pat yourself on the back for doing a great job. 111 00:08:10,005 --> 00:08:13,195 In the next video, we'll keep adding specs to this test suite and 112 00:08:13,195 --> 00:08:14,665 expand our game of Battleship.