using promises correctly in tests

This commit is contained in:
Tim Griesser 2013-05-26 19:53:44 -04:00
parent a3d2fb7aa9
commit 3446fe7461
3 changed files with 100 additions and 132 deletions

View File

@ -26,9 +26,7 @@
results.length.should.equal(2);
done();
}, function (error) {
throw error;
});
}, done);
});
it('can read', function (done) {
@ -41,18 +39,13 @@
firstPost = results.models[0];
posts.read({slug: firstPost.attributes.slug}).then(function (found) {
should.exist(found);
return posts.read({slug: firstPost.attributes.slug});
}).then(function (found) {
should.exist(found);
found.attributes.title.should.equal(firstPost.attributes.title);
found.attributes.title.should.equal(firstPost.attributes.title);
done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
done();
});
});
@ -60,25 +53,24 @@
var firstPost;
posts.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPost = results.models[0];
posts.edit({id: firstPost.id, title: "new title"}).then(function (edited) {
should.exist(edited);
return posts.edit({id: firstPost.id, title: "new title"});
edited.attributes.title.should.equal('new title');
}).then(function (edited) {
done();
}, function (error) {
throw error;
});
should.exist(edited);
}, function (error) {
throw error;
});
edited.attributes.title.should.equal('new title');
done();
}, done);
});
it('can add', function (done) {
@ -101,39 +93,35 @@
});
it('can delete', function (done) {
var firstPostId,
ids,
hasDeletedId;
var firstPostId;
posts.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPostId = results.models[0].id;
posts.destroy(firstPostId).then(function () {
return posts.destroy(firstPostId);
posts.browse().then(function (newResults) {
}).then(function () {
ids = _.pluck(newResults.models, "id");
return posts.browse();
hasDeletedId = _.any(ids, function (id) {
return id === firstPostId;
});
}).then(function (newResults) {
var ids, hasDeletedId;
hasDeletedId.should.equal(false);
ids = _.pluck(newResults.models, "id");
done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
hasDeletedId = _.any(ids, function (id) {
return id === firstPostId;
});
}, function (error) {
throw error;
});
hasDeletedId.should.equal(false);
done();
}, done);
});
});
}());

View File

@ -27,9 +27,7 @@
results.length.should.be.above(0);
done();
}, function (error) {
throw error;
});
}, done);
});
it('can read', function (done) {
@ -43,20 +41,17 @@
firstSetting = results.models[0];
settings.read(firstSetting.attributes.key).then(function (found) {
return settings.read(firstSetting.attributes.key);
should.exist(found);
}).then(function (found) {
found.attributes.value.should.equal(firstSetting.attributes.value);
should.exist(found);
done();
}, function (error) {
throw error;
});
found.attributes.value.should.equal(firstSetting.attributes.value);
}, function (error) {
throw error;
});
done();
}, done);
});
it('can edit single', function (done) {
@ -71,29 +66,26 @@
firstPost = results.models[0];
// The edit method has been modified to take an object of
// The edit method has been modified to take an object of
// key/value pairs
toEdit[firstPost.attributes.key] = "new value";
settings.edit(toEdit).then(function (edited) {
return settings.edit(toEdit);
should.exist(edited);
}).then(function (edited) {
edited.length.should.equal(1);
should.exist(edited);
edited = edited[0];
edited.length.should.equal(1);
edited.attributes.key.should.equal(firstPost.attributes.key);
edited.attributes.value.should.equal('new value');
edited = edited[0];
done();
}, function (error) {
throw error;
});
edited.attributes.key.should.equal(firstPost.attributes.key);
edited.attributes.value.should.equal('new value');
}, function (error) {
throw error;
});
done();
}, done);
});
it('can edit multiple', function (done) {
@ -111,34 +103,32 @@
firstPost = results.models[0];
secondPost = results.models[1];
// The edit method has been modified to take an object of
// The edit method has been modified to take an object of
// key/value pairs
toEdit[firstPost.attributes.key] = "new value1";
toEdit[secondPost.attributes.key] = "new value2";
settings.edit(toEdit).then(function (edited) {
return settings.edit(toEdit);
should.exist(edited);
}).then(function (edited) {
edited.length.should.equal(2);
should.exist(edited);
editedPost = edited[0];
edited.length.should.equal(2);
editedPost.attributes.key.should.equal(firstPost.attributes.key);
editedPost.attributes.value.should.equal('new value1');
editedPost = edited[0];
editedPost = edited[1];
editedPost.attributes.key.should.equal(firstPost.attributes.key);
editedPost.attributes.value.should.equal('new value1');
editedPost.attributes.key.should.equal(secondPost.attributes.key);
editedPost.attributes.value.should.equal('new value2');
editedPost = edited[1];
done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
});
editedPost.attributes.key.should.equal(secondPost.attributes.key);
editedPost.attributes.value.should.equal('new value2');
done();
}, done);
});
it('can add', function (done) {
@ -155,15 +145,11 @@
createdSetting.attributes.value.should.equal(newSetting.value, "value is correct");
done();
}, function (error) {
throw error;
});
}, done);
});
it('can delete', function (done) {
var firstSettingId,
ids,
hasDeletedId;
var firstSettingId;
settings.browse().then(function (results) {
@ -173,28 +159,27 @@
firstSettingId = results.models[0].id;
settings.destroy(firstSettingId).then(function () {
return settings.destroy(firstSettingId);
settings.browse().then(function (newResults) {
}).then(function () {
ids = _.pluck(newResults.models, "id");
return settings.browse();
hasDeletedId = _.any(ids, function (id) {
return id === firstSettingId;
});
}).then(function (newResults) {
hasDeletedId.should.equal(false);
var ids, hasDeletedId;
done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
ids = _.pluck(newResults.models, "id");
hasDeletedId = _.any(ids, function (id) {
return id === firstSettingId;
});
}, function (error) {
throw error;
});
hasDeletedId.should.equal(false);
done();
}, done);
});
});
}());

View File

@ -103,9 +103,7 @@
});
it('can delete', function (done) {
var firstUserId,
ids,
hasDeletedId;
var firstUserId;
users.browse().then(function (results) {
@ -115,33 +113,30 @@
firstUserId = results.models[0].id;
users.destroy(firstUserId).then(function () {
return users.destroy(firstUserId);
users.browse().then(function (newResults) {
}).then(function () {
if (newResults.length < 1) {
// Bug out if we only had one user and deleted it.
return done();
}
return users.browse();
ids = _.pluck(newResults.models, "id");
}).then(function (newResults) {
var ids, hasDeletedId;
hasDeletedId = _.any(ids, function (id) {
return id === firstUserId;
});
if (newResults.length < 1) {
// Bug out if we only had one user and deleted it.
return done();
}
hasDeletedId.should.equal(false);
ids = _.pluck(newResults.models, "id");
done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
hasDeletedId = _.any(ids, function (id) {
return id === firstUserId;
});
}, function (error) {
throw error;
});
hasDeletedId.should.equal(false);
done();
}, done);
});
});