Giter Site home page Giter Site logo

testing-student-object's Introduction

Testing a Student Object

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;

Create package.json

$ npm init

Follow the prompts and just use the defaults if you prefer

Install Mocha and Chai

$ npm install mocha --save-dev
$ npm install chai --save-dev

Create the student library file

$ touch student.js

Paste the code above into this file

Create the test directory and test 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

Write tests for every method of Student

  • Use describe to explain your tests
  • Use either assert, expect, or should for your tests. The choice is yours.

Challenge

  • 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.

testing-student-object's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.