So, I'm running some tests on my node.js app.
The test iterates through all my models and checks each model's exposed api. The it
calls check only the public models, depending on the model's public
property.
Before the it
calls are made, I want to pass some test data to the models using beforeEach
at each iteration. Unfortunately, due to asynchronous calls (I'm guessing), the the iterator value in beforeEach
remains the same. I'm guessing the loop is executed even before beforeEach
. I have two it
calls per model resulting in beforeEach
being called twice per model.
UPDATE
I guess I'll have to show my entire use case cause this is a little more complicated than what I've asked here. Cody's solution seems possible, but it still doesn't help my use case. This is because I have a few conditions in the loop which runs the test only for certain models and not all. Also, there are two it
calls, meaning beforeEach
is called twice for every iteration of the loop. Here's the code:
var models = {'Ate': {'property1': 'prop'}, 'Bat': {'property2': 'prop2'}, 'Cat': {'property3': 'prop3'}};
var modelConfigs = {'Ate': {'public': 'true'}, 'Bat': {'public': 'false'}, 'Cat': {'public': 'true'}};
describe('Given an array of model objects', function(){
for(var key in modelConfigs) {
var m = key;
var modelConfig = modelConfigs[m];
var model = models[m].definition;
var modelPlural = models[m].settings.plural;
if(modelConfig.public) { // Condition runs for only some models
beforeEach(function (done) {
var test = this;
var testKey = m;
console.log(testKey); //Output is always Cat.
done();
});
lt.describe.whenCalledRemotely('GET', '/api/' + modelPlural, function() {
it('should have status code 200', function() { //First it call
assert.equal(this.res.statusCode, 200);
});
it('should be sorted DESC by date', function() { //Second it call
var modelRes = this.res.body;
expect(modelRes).to.be.instanceOf(Array);
expect(modelRes).to.have.length.below(11);
console.log(modelRes.length);
if(modelRes.length > 0) {
for(var i=1; i< modelRes.length; i++) {
expect(modelRes[i-1]['date']).to.be.at.least(modelRes[i]['date']);
}
}
});
});
}
}
});