Last active
October 2, 2015 19:29
-
-
Save aray12/48198033cbe42308cca9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* I have recently started to embrace TDD or BDD or whatever we are calling it today. I was | |
* hoping for some feedback on my how I am structuring my tests. Currently I am writing | |
* functional tests for my API routes. One thing I dislike is that I have included a global | |
* data object in each file and beforeEach test generating json web tokens for auth purposes. | |
* I could probably move that actual logic to some test helper but I still have to store | |
* this global (for the page) object. Just trying to some feedback. Any advice, even | |
* unrelated to this, is gladly appreciated. | |
*/ | |
'use strict'; | |
var should = require('should'), | |
jwt = require('jsonwebtoken'), | |
app = require('app'), | |
request; | |
var data = { | |
resource_prop1: 1, | |
resource_prop2: "some string", | |
resource_prop3: 1, | |
auth: null | |
}; | |
describe('Resource', function() { | |
beforeEach(function(done) { | |
request = require('supertest')(app); | |
var profile = { | |
account_id: 1, | |
account_email: "first@test.com" | |
}; | |
var token = jwt.sign(profile, process.env.SECRET, {expiresInMinutes: 60 * 5}); | |
data.auth = 'Bearer ' + token; | |
done(); | |
}); | |
describe('Add', function() { | |
it('should successfully create a new Resource', function(done) { | |
request | |
.post('/resource') | |
.set('Authorization', data.auth) | |
.send(data) | |
.end(function(err, res) { | |
if (err) { | |
throw new Error(err); | |
} | |
res.should.have.property('status').which.equal(200); | |
/** | |
* A bunch of other assertions | |
*/ | |
done(); | |
}); | |
}); | |
describe('Edit', function() { | |
it('should successfully edit an existing instance of Resource', function(done) { | |
var editData = { | |
"resource_prop1": 2, | |
"resource_prop2": "Some other string" | |
}; | |
request | |
.put('/resource/1') | |
.set('Authorization', data.auth) | |
.send(editData) | |
.end(function(err, res) { | |
if (err) { | |
throw new Error(err); | |
} | |
res.should.have.property('status').which.equal(200); | |
/** | |
* A bunch of other assertions | |
*/ | |
done(); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment