Task 2: Command Class
Command Class Description
We’ll follow TDD practices for the creation of
Rover, but for this class,
Command, we’ve provided the functionality.
Command is already written for you and you do not need to modify it to write passing tests. Open up and examine the file
This class builds an object with two properties:
commandTypeis a string that represents the type of command. We will go over the details of the types when we get to the
Roverclass and tests. At this time, note that a command type will be one of the following:
- To peek ahead at the full functionality of these types, refer to the Command Types Table .
valueis a value related to the type of command.
let modeCommand = new Command('MODE_CHANGE', 'LOW_POWER'); let moveCommand = new Command('MOVE', 12000);
MOVE are passed in as the
LOW_POWER and 12000 are passed in as the
value. Different command types require different kinds of values.
STATUS_CHECK takes no value.
Don’t worry about the mode options for now. To peek ahead, see the Rover Modes Table .
Now that we’ve gone over the class, let’s check out the tests.
To begin, open and examine
spec/command.spec.js. One test has been created for you. When a user creates a new
Command object from the class, we want to make sure they pass a command type as the first argument.
Note that the test description reads, “throws error if a command type is NOT passed into the constructor as the first parameter”.
- So far, you have used many expectations to check for equality. In the chapter on exceptions, we shared an example of how we might use an expectation to check if an exception is thrown. Refer back to that example for guidance on the syntax.
- Run the command
npm testwithin your terminal to verify that the test passes. Next, comment out lines 4-6 in
npm testagain to verify that the test fails (the expected error is not thrown when the
Commandclass is called).
- Restore lines 4-6 to
throw Error("Command type required.");.
- Change ‘Command type required.’ on line 9 in
command.spec.jsto ‘Oops’. Run
npm testagain to verify that the test fails (the error message did not match
"Command type required.").
- Restore line 9 to ‘Command type required.’.
Create a second
Command test using “constructor sets command type” as the description. This test checks that the
constructor in the
Command class correctly sets the
commandType property in the new object.
- Without editing
command.js, it contains the correct code. Run
npm testto verify that the first and second tests both pass.
- You do not need to use
- You may not need to know the specific types of commands to write this test.
Code a third test using “constructor sets a value passed in as the 2nd argument” as the description. This test checks that the
constructor correctly sets the
value property in the new object. You may not need to know a proper
value in order to write this test.
npm test to verify that all 3 command tests pass.
As you move through the remaining instructions, the amount of guidance will decrease. Refer to your earlier, passing tests to help you construct new tests and passing code.
Great job, astronaut! When you are ready to keep going, check out the Message Class !