advanced side of testing
audience like you knows assert()
analogy: koans
function sprintf(str) { var params = [].slice.call(arguments,1) var types = { "%s": function(x) { return x + "" } } var matches = 0 return str.replace(/(%[is])/g,function(match) { return types[match](params[matches++]) }) } it("formats correctly",function() { assert.equal("MU!",sprintf("%s","MU!")); })
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { var siteLink = '<a href="' + config().url + '">' + config().url + '</a>', resetUrl = config().url.replace(/\/$/, '') + '/ghost/reset/' + token + '/', resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>', message = { to: email, subject: 'Reset Password', html: '<p><strong>Hello!</strong></p>' + '<p>A request has been made to reset the password on the site ' + siteLink + '.</p>' + '<p>Please follow the link below to reset your password:<br><br>' + resetLink + '</p>' + '<p>Ghost</p>' }; return mailer.send(message); }).then(function success() { var notification = { type: 'success', message: 'Check your email for further instructions', status: 'passive', id: 'successresetpw' }; return api.notifications.add(notification).then(function () { res.json(200, {redirect: config.paths().webroot + '/ghost/signin/'}); }); }, function failure(error) { // TODO: This is kind of sketchy, depends on magic string error.message from Bookshelf. // TODO: It's debatable whether we want to just tell the user we sent the email in this case or not, we are giving away sensitive info here. if (error && error.message === 'EmptyResponse') { error.message = "Invalid email address"; } res.json(401, {error: error.message}); }); },
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { var siteLink = '<a href="' + config().url + '">' + config().url + '</a>', resetUrl = config().url.replace(/\/$/, '') + '/ghost/reset/' + token + '/', resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>', message = { to: email, subject: 'Reset Password', html: '<p><strong>Hello!</strong></p>' + '<p>A request has been made to reset the password on the site ' + siteLink + '.</p>' + '<p>Please follow the link below to reset your password:<br><br>' + resetLink + '</p>' + '<p>Ghost</p>' }; return mailer.send(message); }).then(function success() { // ... }, function failure(error) { // ... }); },
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { // ... }).then(function success() { // ... }, function failure(error) { // TODO: This is kind of sketchy, depends on magic string error.message from Bookshelf. // TODO: It's debatable whether we want to just tell the user we sent the email in this case or not, we are giving away sensitive info here. if (error && error.message === 'EmptyResponse') { error.message = "Invalid email address"; } res.json(401, {error: error.message}); }); },
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { // ... }).then(function success() { var notification = { type: 'success', message: 'Check your email for further instructions', status: 'passive', id: 'successresetpw' }; return api.notifications.add(notification).then(function () { res.json(200, {redirect: config.paths().webroot + '/ghost/signin/'}); }); }, function failure(error) { // ... }); },
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then( api._sendMessage.bind(null,config,email) ).then( api._notifySuccess(res,{ type: 'success', message: 'Check your email for further instructions', status: 'passive', id: 'successresetpw' }) , api._handleFailure ); },
var helloName = sprintf.bind(null,"Hello %s") helloName("Sue") // "Hello Sue"
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { // ... message = { to: email, // ... }).then(function success() { // ...
'generateResetToken': function (req, res) { var email = req.body.email; api.users.generateResetToken(email).then( api._sendMessage.bind(null,config,email) ).then(function success() {
_sendMessage: function(config,email,token) { // ... }
var defaultConfig = require('../../../config'); describe("Mail", function () { beforeEach(function () { // Mock config and settings fakeConfig = _.extend({}, defaultConfig); // ... it('should setup SMTP transport on initialization', function (done) { fakeConfig[process.env.NODE_ENV].mail = SMTP; mailer.init().then(function () { mailer.should.have.property('transport'); mailer.transport.transportType.should.eql('SMTP'); mailer.transport.sendMail.should.be.a.function; done(); }).then(null, done); });
Learn C the Hard Way, Zed Shaw
it("creates a user",function(done) { app.post("/users",validUserAttrs) .then(function() { assert.equal(1,User.count()) done() },done) })
it("creates a user",function(done) { assert.change(function(check) { app.post("/users",validUserAttrs) .then(function() { check() done() },done) },function() { return User.count() }) })
it("creates a user",function(done) { assert.change(function(check) { app.post("/users",validUserAttrs) .then(function() { check() assert.equal(validUserAttrs.name,User.first().name) done() },done) },function() { return User.count() }) })
it("creates a user",function(done) { assert.change(function(check) { app.post("/users",validUserAttrs) .then(function() { check() assert.equal(validUserAttrs.name,User.first().name) done() },done) },function() { return User.count() },{by: 1}) })
var string = " \t\t\t\t \n \n \t \t\t\t" while(string == false) string += ["\t","\n"," "][Math.random() * 3 | 0]
var possibleGitObjectNames = [ { name: "aeff938482", expected: git.SHA }, { name: "master", expected: git.REF }, ] possibleGitObjectNames.forEach(function(setup) { it(util.format("correctly identifies '%s' as a '%s'", setup.name,setup.expected),function() { assert.equal(setup.expect, git.identifyObjectNameType(setup.name)) }) }) var invalidObjectNames = [ "../", "refs/heads/master", "\t" ] invalidObjectNames.forEach(function(name) { it(util.format("identifies '%s' as an invalid object name",name),function() { assert.throws(function() { git.identifyObjectNameType(name) }) }) })
npm install --save phantomjs karma karma-mocha karma-chai vim karma.conf.js
module.exports = function(config) { config.set({ frameworks: ['mocha','chai'], browsers: ['Chrome', 'Firefox'], files: [ 'vendor/*.js', 'src/*.js', 'tests/*_test.js' ], client: { mocha: { ui: 'bdd' } } }); };
describe("karma testing",function() { it("is clearly in the browser, I'm parsing URLs" + "with an anchor tag",function() { var urlStr = "http://example.com" var url = parseUrl(urlStr) assert.equal(url.hostname,"example.com") }) }) function parseUrl(str) { if(!parseUrl.parser) parseUrl.parser = document.createElement("a") parseUrl.parser.href = str return ["hostname","protocol","path"].reduce(function(h,k) { h[k] = parseUrl.parser[k] return h },{}) }
describe("annoying cookie widget",function() { it("can be commanded to leave by the user",function() { var widget = new AnnoyingCookie var cares = true widget.ondoesnotcare = function() { cares = false } widget.render() $(widget.el).find(".does_not_care").click() assert.isFalse(cares,"user unable to dismiss " + " stupid cookie warning") }) })
var app = require("../app.js") var request = require("supertest") describe("math server 1.1",function() { describe("addition",function() { it("can add",function(done) { request(app) .get('/add/10/15') .expect(/answer/) .expect(/:\s*25/) .expect(200,done) }) // I wonder if we should test other types of numbers? // are there edge cases for numbers at all? it("validates numbers",function(done) { request(app) .get('/add/spoon/15') .expect(/error/) .expect(400,done) }) }) })
test("#hostname",function() { assert("example.com" === url.hostname) }) test("#hostname",function() { assert.equal("example.com",url.hostname) }) test("provides access to hostname",function() { assert.equal("example.com",url.hostname) }) it("provides access to hostname",function() { expect(url.hostname).to.equal("example.com") }) it("provides access to hostname",function() { url.hostname.should.equal("example.com") })
In 2005 I drunkenly released a dumb hack. It was called RSpec. You are victims on one of the biggest trolls ever committed. You’re welcome.
— Steven R. Baker (@srbaker) June 13, 2013@shinypb I do BDD in Ruby with MiniTest. And I don’t use the RSpec syntax.
— Steven R. Baker (@srbaker) June 15, 2013x.should.equal(y) x.should.be.equalTo(y) x.should.be.equal.to(y) expect(x).to.equal(y) expect(x).to.be.equalTo(y) expect(x).equals(y)
Given the URL '://example.com/links/?uri_id=cow' Then as a programmer I can access the hostname as 'example.com'