Mocha async promises multiple assertions
Here's how I did multiple assertions on a unit that returns it's result as a promise.
Using chai-as-promised:
context("getting 'Best Of' in workshops category", function () {
var subjectresult;
before("filter notes", function () {
subjectresult = subject({
collection: "category",
name: "workshops"
});
});
it("should return two elements", function () {
return expect(subjectresult).to.have.eventually.length(2);
});
it("first should be a favorite element", function () {
return expect(subjectresult).to.have.eventually.deep.property('[0]._id', 'pantsid');
});
it("second should be the most recent element", function () {
return expect(subjectresult).to.have.eventually.deep.property('[1]._id', 'hatsid');
});
});
the chai deep.property
assertions lets me select an element from an array and just test one property:return expect(subjectresult).to.have.eventually.deep.property('[1]._id', 'hatsid');
subjectresult
is a promise containing an array. I'm testing that the second item in the array has the _id
of 'hatsid
'.