Using what we learned today with Mocha and Chai, let's develop some tests for student
objects.
Here's the code we've just written and want to test:
// student.js
function Student(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Student.prototype.getFirstName = function() {
return this.firstName;
}
Student.prototype.getLastName = function() {
return this.lastName;
}
Student.prototype.getFullName = function() {
return this.firstName + ' ' + this.lastName;
}
Student.prototype.getGreeting = function() {
return 'Good day mate, my name is ' + this.firstName;
}
module.exports = Student;
$ npm init
Follow the prompts and just use the defaults if you prefer
$ npm install mocha --save-dev
$ npm install chai --save-dev
$ touch student.js
Paste the code above into this file
$ mkdir test && cd test
$ touch student_tests.js
Your directory structure should look something like this now:
├── node_modules/
├── package.json
├── student.js
└── test/
└── student_tests.js
- Use
describe
to explain your tests - Use either
assert
,expect
, orshould
for your tests. The choice is yours.
- Modify the
student
object so that if there's no last name passed, it defaults to 'Doe'. Write a new test to ensure that it works as expected. - Modify the
student
object so that an age can also be passed in. Write tests that ensure it's a valid number and that the value is within a reasonably acceptable range.