Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Unit Testing!
You have completed JavaScript Unit Testing!
Preview
In order to test a random space picker, weβll need to refactor our code a little to capture a testable output.
Challenge review
- The way
computerFireis written is not testable; the only tests we can write for it are trivial and uninformative - Refactor the two "computer" functions Iβve written so that theyβre more testable
- Try to rewrite the set of functions so that we can still generate random locations
- You might need to move the code generating random numbers into their own functions in order to isolate the problem
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
But if you end up with code before tests,
0:00
it can be hard to make a meaningful
unit test for existing functions.
0:01
That's because some code is not testable.
0:05
The particular nature of
JavaScript means that we have
0:08
access to some code and not other.
0:11
To demonstrate this,
in the latest project files for
0:13
this challenge I have written a pair
functions that handle a computer player.
0:16
To make a computer player we have
to give it a programmatic way of
0:20
placing ships without us knowing.
0:23
And guessing locations during their turn.
0:25
So I've named these functions
computerPlaceShip and computerFire.
0:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up