Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. const chai = require('chai');
  2. const sinon = require('sinon');
  3. let mod = require('../mathProject');
  4. let math = require('./math.js');
  5.  
  6. // a)
  7. // testing with 2 positive numbers
  8. describe ('example1', function() {
  9. it('doDivision check if return correct result given 2 positive numbers, to 2 digits after the comma', function () {
  10. var a = math.doDivision(1,3);
  11. // we console log the outcome
  12. console.log(a)
  13. c = Math.ceil(a * 100)/100;
  14. // we console log the number rounded down to 2 digits after comma
  15. console.log(c)
  16. chai.expect(c).to.equal(0.34);
  17. chai.expect(c).to.be.a('number')
  18. });
  19. });
  20.  
  21. // test with NaN
  22. // if we don't add numbers to the function, excpecting NaN
  23. // NaN will be accepted as a number
  24. describe ('example2', function() {
  25. it('doDivision with nothing', function () {
  26. var a = math.doDivision();
  27. console.log(a);
  28. chai.expect(a).to.be.a('Number');
  29. chai.expect(a).to.be.NaN;
  30. });
  31. });
  32.  
  33. // test with zero
  34. // here we will assume an number divided by zero is Infinite
  35. // Infinity will be accepted as a number
  36. describe ('example3', function() {
  37. it('doDivision with zero', function () {
  38. var a = math.doDivision(999, 0);
  39. console.log(a);
  40. chai.expect(a).to.be.a('Number');
  41. chai.expect(a).to.equal(Infinity);
  42. });
  43. });
  44.  
  45.  
  46. // b)
  47. // A single test asserting that the function returns the correct string ("a divided by b is result")
  48. // given two positive numbers. Use a Stub/Fake (using the Sinon module) to replace the call to
  49. // the doDivision function.
  50.  
  51. describe ('example4', function () {
  52. beforeEach(() => {
  53. var fakeCheck = sinon.fake.returns(true);
  54. sinon.replace(mod, 'doDivision', fakeCheck);
  55. });
  56.  
  57. it('should always pass for stingifyDivision', function (done) {
  58. var answer = mod.stringifyDivision( 5, 6 );
  59. chai.expect(answer).to.equal('5 divided by 6 is true');
  60. done();
  61. });
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement