jeudi 9 juillet 2015

Can't access sails app in integration tests

I'm trying to write some integration tests in sailsjs. I have a bootstrap.test.js file that lifts my server in a global before as the docs suggest.

In my integration test when I try to pass my sails app to supertest I get an error:

app is not defined
agent = request.agent(app.hooks.http.app);
                      ^

bootstrap.test.js

var Sails = require('sails'),
  Barrels = require('barrels'),
  app;

before(function(done) {

  this.timeout(5000);

  Sails.lift({

    log: {
      level: 'error'
    },
    models: {
      connection: 'test',
      migrate: 'drop'
    }
  }, function(err, sails) {
    app = sails;
    if (err) return done(err);

    var barrels = new Barrels();
    fixtures = barrels.data;

    barrels.populate(function(err) {
      done(err, sails);
    });
  });
});

// Global after hook
after(function (done) {
  console.log(); // Skip a line before displaying Sails lowering logs
  Sails.lower(done);
});

integration test

var chai = require('chai'),
  expect = chai.expect,
  request = require('supertest'),
  agent = request.agent(app.hooks.http.app);

describe('Species CRUD test', function() {

  it('should not allow an unauthenticated user create a species', function(done){
    var species = {
      scientificName: 'Latin name',
      commonName: 'Common name',
      taxon: 'Amphibian',
      leadOffice: 'Vero Beach',
      range: ['Florida', 'Georgia']
    };

    agent.post('species')
      .send(species)
      .end(function(err, species) {
        expect(err).to.exist;
        expect(species).to.not.exist;
        done();
      });
  });
});

Aucun commentaire:

Enregistrer un commentaire