[toc]
A lot of people are still having trouble with A3. So I thought I would help out.
Sometimes the best way to make an assignment easier is to look at it from another perspective or to reorganize the information in such a way that it is better to comprehend.
One way that I find helpful, at least in terms of this assignment, is to organize the information into a class diagram since we are doing something involved with object oriented programming (OOP). Often times, when you are doing an assignment where OOP is involved, there's a class diagram to provide a blueprint of our project.
For A3, this is what it looks like:
classDiagram
class commandType {
<<enumeration>>
"MODE_CHANGE"
"MOVE"
"STATUS_CHECK"
}
class Command{
Command constructor(commandType:String<commandType>,value:Any)
commandType:String<commandType>
value:Any
}
class Message{
Message constructor(name:String,commands:Array<Command>)
name:String
commands:Array<Command>
}
class Rover{
Rover constructor(position:Number)
Object receiveMessage(message:Message)
position:Number
mode:String<roverMode> = 'NORMAL'
generatorWatts:Number = 110
}
class roverMode {
<<enumeration>>
"LOW_POWER"
"NORMAL"
}
Command ..> commandType : Uses
Message "0" ..> "*" Command : Contains
Rover ..> Message : Uses
Rover ..> roverMode : Uses
ℹ️ NOTE: In that class diagram, you only need to create the
Command,Message, andRoverclasses.commandTypeis supposed to represent an enumeration (list) of accepted values thecommantTypevariable inCommandmay accept.
⚠️ WARNING!: Do not create the enumerated lists as separate objects. These lists are illustrated to show what will be processed in the final product and intended to show what will be constructed INSIDE one of the functions in the class. You should only create the three classesCommand,Message, andRover.
However, don't go writing all this up in JavaScript just yet. If you do, you'll want to consider commenting out the parts of the program that we haven't built yet. In fact, you shouldn't build everything in the diagram just yet. Some of the unit tests will need you to build your project as you test.
I believe the Unit Testing Chapter has flaws. While there is information provided on how to unit test, in terms of instructions, the purpose and motivation for doing so seems lacking. You can tell an artists to paint a picture, but unless you tell them what they should paint or for what reason, you'll only make things more frustrating or confusing.
We created this story about the Mars Rover, which is a good plot point and story, but for a lot of people it's a lot like trying to tell someone to fetch a pail of water from a well that is dry.
Maybe if we had made a game out of this or made it feel more like a scavenger hunt rather than "do this write that".
A test list should be sort of like a clue sheet, or like in charades where you need to solve a multiword answer. Once you get the gist of the game or find the majority of the words, the rest of the game (or in this case the tasks) is easy.
Submitted for your approval, I want to rewrite this assignment. If the class diagram can describe our three modules should be written, the tests lists should be the "what-if" part of the game.
You need to create Six files. For every Module you make (command.js, message.js, and rover.js) you must create a spec file that contains tests for each class (spec/command.spec.js, spec/message.spec.js, spec/rover.spec.js)
ℹ️ NOTE: Because this is an assignment, I won't be listing the answers here. I will on the other hand provide class diagrams to show what you three main classes should look like at each test step.
- Module:
command.js- Spec:
spec/command.jsClass
This class builds an object with two properties:
constructor(commandType,value).
commandTypeis a string that represents the type of command from thecommandTypeenumerated list:'MODE_CHANGE','MOVE', and'STATUS_CHECK'.valueis a value related to the type of command. Not all commands will use it, as you will find out later.Tests
- Create a
Commandthat throw an error if acommandTypeis not passed into the constructor as the first parameter. (Basically, useassert.throws()becauseCommandwas used without any arguments.)- Create a
Commandthat test to see ifcommandTypeis set. (Basically, useassert.isStrictEqual()to test thatcommandTypein theCommandconstructor produces the value thatcommandTypewas assigned to when it was created.)- Create a
Commandwith a second argument. Much like with Test 2, test to see if thevaluein theCommandconstructor produces the value thatvaluewas assigned to when it was created. (You don't need to know a propervaluein order to write this test.)
classDiagram
class Command{
Command constructor(commandType:String)
commandType:String
}
Use an assert.throws() to test that an exception was thrown if commandType is not used in the Command constructor.
classDiagram
class Command{
Command constructor(commandType:String<commandType>)
commandType:String<commandType>
}
Use an assert.isStrictEqual() to test that commandType exists by testing if it is a String.
classDiagram
class Command{
Command constructor(commandType:String<commandType>,value:Any)
commandType:String<commandType>
value:Any
}
User an assert.isStrictEqual() to test that value exists.
- Module:
message.js- Spec:
spec/message.spec.jsClass
This class builds an object with two properties:
constructor(name, commands)
nameis a string that is the name of the message.commandis an array ofCommandobjects.You will need to include the
command.jsfile inmessage.jsso there should be a line near the top ofmessage.jsthat saysconst Command = require('./command.js');You will also need to export the
Messageclass, so there should be a line at the bottom that saysexports { Message }🔖 PICK UP HERE
Tests
- Module:
rover.js- Spec:
spec/rover.spec.js
Once you've completed this test
#LaunchCode