0

我正在尝试使用wercker,但我不知道我的测试无法连接到我的 mongodb。

我正在使用sails +sails mongo,当npm test...我总是得到错误可以连接到mongo db,这是我的wercker.yml:

box: nodesource/trusty:0.12.7
services:
  - id: mongo:2.6
# Build definition
build:
  # The steps that will be executed on build
  steps:
    - script:
        name: set NODE_ENV
        code: export NODE_ENV=development
    # A step that executes `npm install` command
    - npm-install
    # A step that executes `npm test` command
    - npm-test
    # A custom script step, name value is used in the UI
    # and the code value contains the command that get executed
    - script:
        name: echo nodejs information
        code: |
          echo "node version $(node -v) running"
          echo "npm version $(npm -v) running"

这是我的错误信息:

warn: `sails.config.express` is deprecated; use `sails.config.http` instead.
Express midleware for passport
error: A hook (`orm`) failed to load!
  1) "before all" hook
  2) "after all" hook

  0 passing (2s)
  2 failing

  1)  "before all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14

  2)  "after all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14
4

1 回答 1

1

开箱即用时,MongoDB 没有身份验证,因此您只需要提供正确的主机和端口即可。

  1. 在您的 Sails 应用程序中定义一个新连接config/connection.js
    mongodbTestingServer: {
        adapter: 'sails-mongo',
        host:    process.env.MONGO_PORT_27017_TCP_ADDR,
        port:    process.env.MONGO_PORT_27017_TCP_PORT
    },

关于MONGO_PORT_27017_TCP_ADDRand MONGO_PORT_27017_TCP_PORT,这两个环境变量是 Wercker 在声明 mongo 服务时创建的。像这样,您将能够使用正确的主机和端口将您的应用程序连接到您的数据库

  1. 在您的 Sails 应用程序中添加一个新环境config/env/testing.js。Wercker 将使用它:
module.exports = {
    models: {
        connection: 'mongodbTestingServer'
    }
};
  1. 在您的 wercker 文件wercker.yml中。我建议您使用ewok 堆栈(基于 Docker),您可以在应用程序的设置中激活它。以下是有关迁移到 Ewok 堆栈的一些有用信息。我的示例使用基于 Docker 映像的框。
# use the latest official stable node image hosted on DockerHub 
box: node
# use the mongo (v2.6) image hosted on DockerHub 
services:
  - id: mongo:2.6
# Build definition
build:
    steps:
    # Print node and npm version
    - script:
        name: echo nodejs information
        code: |
            echo "node version $(node -v) running"
            echo "npm version $(npm -v) running"
    - script:
        name: set NODE_ENV
        code: |
            export NODE_ENV=testing
    # install npm dependencies of your project
    - npm-install
    # run tests
    - npm-test

要查看 Wercker 构建中的所有环境变量,请添加以下行:

- script:
    name: show all environment variables
    code: |
        env

它应该工作。

于 2015-10-20T21:06:32.470 回答