Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const chai = require('chai');
- const sinon = require('sinon');
- let mod = require('../mathProject');
- let math = require('./math.js');
- // a)
- // testing with 2 positive numbers
- describe ('example1', function() {
- it('doDivision check if return correct result given 2 positive numbers, to 2 digits after the comma', function () {
- var a = math.doDivision(1,3);
- // we console log the outcome
- console.log(a)
- c = Math.ceil(a * 100)/100;
- // we console log the number rounded down to 2 digits after comma
- console.log(c)
- chai.expect(c).to.equal(0.34);
- chai.expect(c).to.be.a('number')
- });
- });
- // test with NaN
- // if we don't add numbers to the function, excpecting NaN
- // NaN will be accepted as a number
- describe ('example2', function() {
- it('doDivision with nothing', function () {
- var a = math.doDivision();
- console.log(a);
- chai.expect(a).to.be.a('Number');
- chai.expect(a).to.be.NaN;
- });
- });
- // test with zero
- // here we will assume an number divided by zero is Infinite
- // Infinity will be accepted as a number
- describe ('example3', function() {
- it('doDivision with zero', function () {
- var a = math.doDivision(999, 0);
- console.log(a);
- chai.expect(a).to.be.a('Number');
- chai.expect(a).to.equal(Infinity);
- });
- });
- // b)
- // A single test asserting that the function returns the correct string ("a divided by b is result")
- // given two positive numbers. Use a Stub/Fake (using the Sinon module) to replace the call to
- // the doDivision function.
- describe ('example4', function () {
- beforeEach(() => {
- var fakeCheck = sinon.fake.returns(true);
- sinon.replace(mod, 'doDivision', fakeCheck);
- });
- it('should always pass for stingifyDivision', function (done) {
- var answer = mod.stringifyDivision( 5, 6 );
- chai.expect(answer).to.equal('5 divided by 6 is true');
- done();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement