Mocha Chai Expect

by quangcanh2975

HTML

<link rel="stylesheet" href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css">
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.3.0/chai.min.js"></script>
<!-- Mocha test output goes here. -->
<div id="mocha"></div>

JavaScript

mocha.setup('bdd');
var expect = chai.expect;

function sum(numbers) {
  // Giả thiết: numbers là một array các số nguyên
  // Yêu cầu: 
  //	- trả về tổng các số, dùng vòng lặp for ... of
  //  - nếu array trống trả về 0
  var i, sum = 0;
  for(i of numbers){
  	sum += i;
  }
  return sum;
}
console.log(sum([1,2,3]));

describe('sum', function () {
	it('should return 0 if array is empty', function () {
    expect(sum([])).to.equal(0);
  });
  
  it('should return the sum if array is not empty', function() {
  	expect(sum([1])).to.equal(1);
    expect(sum([1, 2, 3])).to.equal(6);
  });
});

mocha.run();