diff --git a/app/models/student.js b/app/models/student.js index 99c2c4f..9a05196 100644 --- a/app/models/student.js +++ b/app/models/student.js @@ -6,8 +6,6 @@ function Student(obj){ this.name = obj.name; this.color = obj.color; this.tests = []; - this.onHonorRoll = checkHonorRoll(this); - this.isSuspended = checkSuspended(this); } Object.defineProperty(Student, 'collection', { @@ -19,7 +17,6 @@ Student.prototype.avg = function(){ var sum = this.tests.reduce(function(a,b){return a+b;}); return sum/this.tests.length; - }; Student.prototype.letter = function(){ @@ -39,6 +36,17 @@ Student.prototype.letter = function(){ } }; +Student.prototype.onHonorRoll = function(){ + if(!this.tests.length){return false;} + return this.avg() > 95; +}; + +Student.prototype.isSuspended = function(){ + if(!this.tests.length){return false;} + var numFailed = this.tests.reduce(function(a, b){return a += (b < 60) ? 1 : 0;}, 0); + return numFailed >= 3; +}; + Student.prototype.save = function(cb){ Student.collection.save(this, cb); }; @@ -51,13 +59,3 @@ function reProto(obj){ return _.create(Student.prototype, obj); } -function checkHonorRoll(me){ - if(!me.tests.length){return false;} - return me.avg() > 95; -} - -function checkSuspended(me){ - if(!me.tests.length){return false;} - var numFailed = me.tests.reduce(function(a, b){return a += (b < 60) ? 1 : 0;}, 0); - return numFailed >= 3; -} diff --git a/test/unit/student.js b/test/unit/student.js index 8d43fda..468a98e 100644 --- a/test/unit/student.js +++ b/test/unit/student.js @@ -1,5 +1,5 @@ /* jshint expr: true */ -/* global describe, it, before*/ +/* global describe, it, before, beforeEach */ 'use strict'; var expect = require('chai').expect; @@ -15,51 +15,68 @@ describe('Student', function(){ done(); }); }); + beforeEach(function(done){ + Student.collection.remove(function(){ + s1 = new Student({name:'Jim Jones', color:'blue'}); + done(); + }); + }); describe('Constructor', function(){ it('should create a new instance of Student with properties', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); expect(s1).to.be.instanceof(Student); expect(s1.name).to.equal('Jim Jones'); expect(s1.color).to.equal('blue'); expect(s1.tests).to.have.length(0); - expect(s1.onHonorRoll).to.be.false; - expect(s1.isSuspended).to.be.false; }); }); describe('#avg', function(){ it('should return the average for all tests taken', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [95, 85, 75, 65, 55]; expect(s1.avg()).to.be.closeTo(75, 0.1); }); }); describe('#letter', function(){ it('should return A', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [95]; expect(s1.letter()).to.equal('A'); }); it('should return B', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [85]; expect(s1.letter()).to.equal('B'); }); it('should return C', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [75]; expect(s1.letter()).to.equal('C'); }); it('should return D', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [65]; expect(s1.letter()).to.equal('D'); }); it('should return F', function(){ - s1 = new Student({name:'Jim Jones', color:'blue'}); s1.tests = [55]; expect(s1.letter()).to.equal('F'); }); }); + describe('#onHonorRoll', function(){ + it('should be true if avg is above 95%', function(){ + s1.tests = [98]; + expect(s1.onHonorRoll()).to.be.true; + }); + it('should be false if avg is below 95%', function(){ + s1.tests = [90]; + expect(s1.onHonorRoll()).to.be.false; + }); + }); + describe('#isSuspended', function(){ + it('should be true if 3 tests have been failed', function(){ + s1.tests = [98, 44, 55, 22, 80]; + expect(s1.isSuspended()).to.be.true; + }); + it('should be false if less than 3 tests have been failed', function(){ + s1.tests = [90, 55, 88, 95]; + expect(s1.isSuspended()).to.be.false; + }); + }); describe('#save', function(){ it('should save Student into database', function(done){ var bob = new Student({name:'Bob', color:'green'});