如何在Travis CI中访问环境变量

时间:2016-01-20 17:46:49

标签: javascript unit-testing mocha travis-ci chai

我使用SQL Server和Node.js构建了一个站点,我使用Mocha和Chai进行测试。这一切都在本地工作正常,任何不需要访问数据库的测试都可以在Travis CI上正确运行,但因为我的密码,用户名,数据库路径等存储在我的.env文件中,这是gitignored Travis can'访问数据库进行测试。

我尝试根据these instructions from the Travis docs设置环境变量,但登录失败。我知道正在找到环境变量,因为错误消息是:Unhandled rejection SequelizeConnectionError: Login failed for user 'EventAdmin'.而'EventAdmin'是来自环境变量的用户名,但由于某种原因,密码未被接受。我知道密码是正确的,因为我直接从我的.env文件中复制了密码。

我的.travis.yml文件如下所示:

language: node_js
node_js:
  - "4.1"
  - "4.0"
  - "0.12"
  - "0.11"
  - "0.10"
  - "iojs"
before_install:
  - npm install -g grunt-cli
script: grunt test

我的测试(在本地工作)看起来像这样:

'use strict';

var chai = require('chai');
var expect = chai.expect;
var assert = chai.assert;
var chaihttp = require('chai-http');


chai.use(chaihttp);

require('../server.js');

describe('Test /showfullteam route', function() {
  it('should load all MS contacts from /showfullteam', function(done) {
    chai.request('localhost:3000')
      .get('/showfullteam')
      .end(function(err, res) {
        expect(err).to.eql(null);
        for (var i = 0; i < res.body.length; i++) {
          expect(res.body[i]).to.include.keys('firstName', 'lastName', 'email', 'newsletterSubscription', 'contactDescription', 'msTeamMember', 'msTeamTitle', 'showOnHomePage', 'headShot', 'company', 'address', 'country', 'interestId', 'allowNotifications', 'allowPersonalInfoSharing');
          expect(res.body[i].msTeamMember).to.eql(true);
          expect(typeof res.body[i].firstName).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].lastName).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].email).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          if (res.body[i].newsletterSubscription) {
            assert.typeOf(res.body[i].newsletterSubscription, 'boolean');
          }
          if (res.body[i].msTeamMember) {
            assert.typeOf(res.body[i].msTeamMember, 'boolean');
          }
          if (res.body[i].showOnHomePage) {
            assert.typeOf(res.body[i].showOnHomePage, 'boolean');
          }
          if (res.body[i].allowNotifications) {
            assert.typeOf(res.body[i].allowNotifications, 'boolean');
          }
          if (res.body[i].interestId) {
            assert.isNumber(res.body[i].interestId);
          }
          expect(typeof res.body[i].contactDescription).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].headShot).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].company).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].address).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].country).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
          expect(typeof res.body[i].allowPersonalInfoSharing).to.be.a('string') || expect(typeof res.body[i].firstName).to.be.a('null');
        };
        done();
      });
  });
});

您可以在GitHub repo上查看完整项目,在Travis CI上看到失败的测试

如果您需要更多信息,请与我们联系。提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我想出了如何设置环境变量,但这不是问题。问题是SQL Server与travis不兼容。我切换到mariadb,测试通过: - )