Ship Object - Walkthrough
Steps
- Discuss with your classmates what the domain model would look like for the above user story. Would recommend a pen and paper for this step.
- Create a new test file, which should
describe
your constructor. - Create a new test spec (
it
) for ensuring an instance of your object can be created. - Write the code that makes this test pass.
- Create another test spec for each property and/or method you identify.
- Write the code that makes this test pass.
- Add, commit with a meaningful message, and push to GitHub.
Domain Model
From the user story:
The domain model for this user story might look like this:
Object | Methods | Properties |
---|---|---|
Ship | startingPort |
describe
Ship
We have a Ship
object so we'll describe a Ship
constructor.
Create a __tests__
folder in your project root, and inside create a new file Ship.test.js
.
First, we'll import in our constructor file. It doesn't exist yet, but we're developing using a test-driven approach, so that's okay:
:exclamation: The /* globals describe it expect */
just tells VS Code and the linter that these variables don't exist in the scope (because the jest
test runner brings them into scope), so don't show linter errors for them.
Then below, we'll describe
our constructor:
Save the file, and in your Terminal run npm run test
. You should fail with the following:
The error here is: Cannot find module '../src/Ship.js' from 'Ship.test.js'
. We can solve this by creating the Ship.js
file in a new folder in the project root: src/
. Run your tests again. You should now fail with:
Test an instance of Ship can be created
To fix this error, we can proceed to write our first test:
Run your tests:
Write the code to make this pass
:exclamation: You should be able to do this without the walkthrough. Once you've passed the test, then proceed.
it
has a startingPort
We identified a property of startingPort
during the domain modelling, so write a test to check a Ship
has this property:
Run your tests. They should fail with:
Write the code to make this pass
:exclamation: You should be able to do this without the walkthrough. Once you've passed the test, then proceed.