Writing Our First Test Suite - Part 28:15 with Guil Hernandez
In this video, we’ll continue writing our BDD outline for Battleship. We’ll write new test specs that correctly test when a ship is located at a given coordinate.
Our check for ship function is looking good, but 0:00 we've only proven that it will report a miss. 0:02 It tells us when no ship is located at our given coordinates, but 0:05 does it also correctly tell us when a ship is located at or given coordinates? 0:08 Let's write a new spec that would prove it. 0:13 Okay, so back inside shiptest.js, I'll just copy my current spec for 0:16 no ship present and paste a new spec, right below it. 0:21 Then I'll adjust the description and expectation. 0:26 So this description will say should correctly report, 0:29 a ship located, at the given coordinates. 0:33 This spec test, whether the function correctly finds a ship 0:42 that is located at the given location. 0:45 So we should change the argument we passed, check for 0:49 ship, to match the players one ship location. 0:51 So I'll change this to 0,0. 0:55 Then I'll adjust the expected result to be true. 0:58 So now, when I run npn test in the consul, 1:04 the test report assertion error, expected,undefined to be true. 1:07 Well currently the only timer function returns something, 1:13 is when no ship is found. 1:17 So maybe we should add a matching conditional to return true, 1:19 when something is found. 1:23 So in our check for ship function inside ship methods.J S, 1:26 will say if no ship is present, Return false. 1:31 Then we'll say else return true. 1:35 All right, so let's run the test again. 1:46 And cool. 1:51 Now it works. 1:52 But so far, we've only tested check for 1:56 ship using a single ship with a single location. 1:58 So we should expand our test to be sure it still works when a player has more than 2:03 one ship, and when a ship is located at more than one coordinate. 2:07 So again, back inside ship test.js. 2:15 I'll copy the spec at the very top and 2:18 paste a new one at the bottom, then revise my description and expectations. 2:22 So I'll change this description to say should handle ships located at more than 2:30 one coordinate. 2:35 This spec will test the same behaviors we've already tested, 2:44 but, with the ship having multiple locations. 2:48 So, inside locations, I'll add a new coordinate of 0,1. 2:51 Then I'll create new expectations for this location, and 2:58 adjust the expected result to be true. 3:01 So, I'll simply copy this expectation, and paste the new one right above it. 3:03 And this will say expect check for ship player at 0,1 to be true. 3:08 Now, you normally don't want your unit test to have more than one expectation, 3:20 because it might be unclear to someone reading it what the test is 3:25 supposed to proof. 3:28 Now, in this case, since we have individual unit tests for positive and 3:29 negative checks, 3:33 I'm comfortable using both expectations in this unit test for brevity. 3:34 In fact, I'll also add an expectation for the 0,0 location, 3:40 just to make sure we can find both. 3:43 And I'll set the expected result to be true. 3:49 All right so let's save our file and run npm test in the console. 3:53 And great that seems good so far. 4:01 So it looks like check for ships doesn't break when we give it a bigger ship. 4:04 So let's try more than one ship. 4:09 So I'll copy the bottom spec inside ship_test, 4:12 and paste a new spec right below it. 4:16 And I'll change this spec's description to should handle checking multiple ships. 4:19 Then I’ll change the expectations. 4:31 But first I’ll just add one ship. 4:33 To the ship's array with new coordinates. 4:35 So I’ll copy the first location and paste the new one right below that. 4:38 And change the coordinates to 1, 0 and 1, 1. 4:45 Then right about the last expectation, I'll add two new expectations for 4:52 this new location. 4:56 So I'll copy one of these and paste it below. 4:58 And for this I'll say expect (checkForShip(player at 1, 0 .to be true 5:02 and will add another one that says expect check ForShip player at [1,1] to be true. 5:07 All right so let's save this. 5:13 Go over to the console and run npm test. 5:20 So now our tests finally catch a bug. 5:24 It looks like multiple ships don't work correctly. 5:27 So the test passes the first expectations, which look at the first ship in the array. 5:30 But the expectations aimed at the second ship fail. 5:37 It says assertion error. 5:41 Expected false to be true. 5:43 But we must have made a mistake with our loop logic. 5:45 Why else would everything work for the first ship, that first loop, and 5:49 then break during the second? 5:52 So let's take another look at our checkForShip function. 5:54 And sure enough, I buried one of the return statements too deep. 6:03 The loop will currently check for no matches after every ship, so that means 6:08 that if the first ship isn't a match for the given location, then the function 6:12 returns false right away before getting to loop again to the next ship. 6:16 So it looks like we need to reverse this logic a bit. 6:21 We want the function to return false for 6:24 no matches only after it has checked every single ship. 6:27 So that means it should return false only after the loop has finished. 6:31 But it should return true immediately when it finds a match. 6:36 So we can just switch things around to capture this idea better. 6:40 So we'll say if ship present return true, and 6:44 I'll delete this else condition. 6:49 Then right before the closing bracket, we'll say return false. 6:53 All right so let's save this and go over to the console and run npm test. 6:59 And great, it looks like the tests all passed now. 7:06 And to be double sure, we can expand the test suite a bit as a proof of concept. 7:11 So let's add one more ship to the ship's array with new coordinates. 7:15 So I'll copy one of the locations and paste the new one at the bottom. 7:19 Then I'll change the coordinates to 2,0, 2,1. 7:26 And I'll add two new coordinates. 7:30 So this one will be 2,2. 7:34 And the next will be 2,3. 7:37 And then I'll go ahead and add a new expectation. 7:44 That says expect check for ship for player at 2,3, 7:50 to be true. 7:55 So now if I go over to the council and run npm test. 7:58 It works great. 8:03 That was a lot of work, so pat yourself on the back for doing a great job. 8:06 In the next video, we'll keep adding specs to this test suite and 8:10 expand our game of Battleship. 8:13
You need to sign up for Treehouse in order to download course files.Sign up