Skip to content

Instantly share code, notes, and snippets.

@dgellow
Created March 21, 2014 15:11
Show Gist options
  • Save dgellow/9688401 to your computer and use it in GitHub Desktop.
Save dgellow/9688401 to your computer and use it in GitHub Desktop.
if (!window.app) {
window.app = {}
}
window.app.Cow = function(name) {
this.name = name || 'Anon cow';
}
window.app.Cow.prototype = {
greets: function(target) {
if (!target) {
throw new Error('missing target');
}
return this.name + ' greets ' + target;
},
lateGreets: function(target, callback) {
setTimeout(function(self) {
try {
callback(null, self.greets(target));
} catch (err) {
callback(err);
}
}, 1000, this);
}
}
var expect = chai.expect
, Cow = window.app.Cow;
describe('Cow', function() {
describe('constructor', function() {
it('should have a default name', function() {
var cow = new Cow();
expect(cow.name).to.equal('Anon cow');
});
it("should set cow's name if provided", function() {
var cow = new Cow('Kate');
expect(cow.name).to.equal('Kate');
});
})
describe('#greets', function() {
it('should throw if no target is passed in', function() {
expect(function() {
(new Cow()).greets();
}).to.throw(Error);
});
it('should greet passed target', function() {
var greetings = (new Cow('Kate')).greets('Baby');
expect(greetings).to.equal('Kate greets Baby');
});
})
describe('#lateGreets', function() {
it('should pass an error if no target is passed', function(done) {
(new Cow()).lateGreets(null, function(err, greetings) {
expect(err).to.be.an.instanceof(Error);
done();
});
});
it('should greet passed target after one second', function(done) {
(new Cow('Kate')).lateGreets('Baby', function(err, greetings) {
expect(greetings).to.equal('Kate greets Baby');
done();
});
});
})
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cow tests</title>
<link rel="stylesheet" media="all" href="vendor/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="vendor/mocha.js"></script>
<script src="vendor/chai.js"></script>
<script src="models/cow.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test/cow_test.js"></script>
<script>mocha.run();</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment