Permalink
Browse files

Updated the tests

  • Loading branch information...
1 parent 719afcf commit 889d60b0701a14de80c16519f6c924eb84f85506 Alex Young committed Jan 31, 2011
Showing with 64 additions and 111 deletions.
  1. +26 −111 test/app.test.js
  2. +38 −0 test/helper.js
View
@@ -1,115 +1,30 @@
-
-// Run $ expresso
-
-// Force test environment
-process.env.NODE_ENV = 'test';
-
var app = require('../app'),
- assert = require('assert');
-
-module.exports = {
- 'Test registration': function(beforeExit) {
- assert.response(app, {
- url: '/users.json',
- method: 'POST',
- data: JSON.stringify({ user: { email: 'alex@example.com', password: 'test' } }),
- headers: { 'Content-Type': 'application/json' }
- }, {
- status: 200,
- headers: { 'Content-Type': 'application/json' }
- },
-
- function(res) {
- var user = JSON.parse(res.body);
- assert.equal('alex@example.com', user.email);
- }
- );
- },
-
- 'Test login': function(beforeExit) {
- assert.response(app, {
- url: '/sessions',
- method: 'POST',
- data: JSON.stringify({ user: { email: 'alex@example.com', password: 'test' } }),
- headers: { 'Content-Type': 'application/json' }
- }, {
- status: 302,
- headers: { 'location': '/documents' }
- }
- );
- },
-
- 'Test document index': function(beforeExit) {
- assert.response(app, {
- url: '/documents.json',
- method: 'POST',
- headers: { 'Content-Type': 'application/json' }
- }, {
- status: 200,
- },
-
- function (res) {
- console.log(res.body);
- }
- );
- },
-
- 'POST /documents.json': function(beforeExit) {
- assert.response(app, {
- url: '/documents.json',
- method: 'POST',
- data: JSON.stringify({ d: { title: 'Test' } }),
- headers: { 'Content-Type': 'application/json' }
- }, {
- status: 200,
- headers: { 'Content-Type': 'application/json' }
- },
-
- function(res) {
- var d = JSON.parse(res.body);
- assert.equal('Test', d.title);
- }
- );
- },
-
- 'HTML POST /documents': function(beforeExit) {
- assert.response(app, {
- url: '/documents',
- method: 'POST',
- data: 'd[title]=test',
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }, {
- status: 302,
- headers: { 'Content-Type': 'text/plain' }
- });
- },
-
- 'GET /documents/id.json': function(beforeExit) {
- },
-
- 'GET /documents.json and delete them all': function(beforeExit) {
- assert.response(app,
- { url: '/documents.json' },
- { status: 200, headers: { 'Content-Type': 'application/json' }},
- function(res) {
- var documents = JSON.parse(res.body);
- assert.type(documents, 'object');
-
- documents.forEach(function(data) {
- app.Document.findById(data._id, function(d) {
- d.remove();
- });
+ assert = require('assert'),
+ zombie = require('zombie'),
+ events = require('events'),
+ testHelper = require('./helper');
+
+app.listen(3001);
+
+testHelper.setup([app.User], function() {
+ // Fixtures
+ var user = new app.User({'email' : 'alex@example.com', 'password' : 'test' });
+ user.save(testHelper.run(exports));
+});
+
+testHelper.tests({
+ 'test login': function() {
+ zombie.visit('http://localhost:3001/', function(err, browser, status) {
+ // Fill email, password and submit form
+ browser.
+ fill('user[email]', 'alex@example.com').
+ fill('user[password]', 'test').
+ pressButton('Log In', function(err, browser, status) {
+ // Form submitted, new page loaded.
+ assert.equal(browser.text('#header a.destroy'), 'Log Out');
+ testHelper.end();
});
- });
- },
-
- 'GET /': function(beforeExit) {
- assert.response(app,
- { url: '/' },
- { status: 302 },
- function(res) {
- process.exit();
- });
+ });
}
-};
+});
View
@@ -0,0 +1,38 @@
+// Force test environment
+process.env.NODE_ENV = 'test';
+var state = {};
+
+function prepare(models, next) {
+ var modelCount = models.length;
+ models.forEach(function(model) {
+ modelCount--;
+ model.find().all(function(records) {
+ var count = records.length;
+ records.forEach(function(result) {
+ result.remove();
+ count--;
+ });
+ if (count === 0 && modelCount === 0) next();
+ });
+ });
+};
+
+exports.tests = function(tests) {
+ state.tests = tests;
+};
+
+exports.run = function(e) {
+ for (var test in state.tests) {
+ e[test] = state.tests[test];
+ }
+};
+
+exports.setup = function(models, next) {
+ state.models = models;
+ prepare(state.models, next);
+};
+
+exports.end = function() {
+ prepare(state.models, process.exit);
+};
+

0 comments on commit 889d60b

Please sign in to comment.