2017-07-14 262 views
1

我试图用Pact库执行一些测试,我收到一些错误。下面是测试配置:契约退出代码1

const path = require('path'); 
const Pact = require('pact'); 
const expect = require('expect.js'); 
const config = require('../../../src/server/config'); 
const service = require('../../../src/routes/interactions/interactions.service'); 

describe('@component/interactions tests',() => { 
    const url = 'http://localhost'; 
    const port = 8989; 

    const provider = Pact({ 
     port: port, 
     log: path.resolve(process.cwd(), 'test/component/interactions/log/interactions-pact.log'), 
     dir: path.resolve(process.cwd(), 'test/component/interactions/pacts'), 
     spec: 2, 
     consumer: 'cx_issue', 
     provider: 'interaction', 
     // logLevel: 'WARN' 
    }); 

    config.settingsToExport.INTERACTION_URL = `${url}:${port}`; 

    before(done => { 
     provider.setup() 
      .then(() => { 
       done(); 
      }) 
      .catch(err => { 
       done(err); 
      }); 
    }); 

    after(done => { 
     provider.finalize() 
      .then(() => { 
       done(); 
      }) 
      .catch(err => { 
       done(err); 
      }); 
    }); 

    describe('#createInteraction',() => { 
     before(done => { 
      const INTERACTION_BODY = { 
       contact: 'contact1' 
      }; 
      const TERM = { 
       generate: '0dae5b93-9451-4b08-b7bb-f0b944fbcdf2', 
       matcher: '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$' 
      }; 

      const pactInteractionCreate = { 
       state: 'Creates a new interaction', 
       uponReceiving: 'a new interaction is created successfully', 
       withRequest: { 
        method: 'POST', 
        path: '/interactions', 
        body: INTERACTION_BODY 
       }, 
       willRespondWith: { 
        status: 201, 
        body: { 
         id: Pact.Matchers.term(TERM) 
        } 
       } 
      }; 

      const promises = [ 
       provider.addInteraction(pactInteractionCreate) 
      ]; 
      Promise.all(promises) 
       .then(() => { 
        done(); 
       }); 
     }); 

     it('/api/interactions POST', done => { 

      const interaction = { 
       contact: 'The xx' 
      }; 

      service.createInteraction(interaction) 
       .then(response => { 
        expect(response.id).to.be.equal(TERM.generate); 
        done(); 
       }) 
       .catch(done); 
     }); 
    }); 
}); 

这是我package.json文件内容,与所有依赖我已经安装:

{ 
    "name": "issueAPI", 
    "version": "1.0.0", 
    "private": true, 
    "main": "./src/index.js", 
    "scripts": { 
    "dev": "nodemon -e js ./src/index.js", 
    "start": "node ./src/index.js", 
    "linter": "node ./node_modules/eslint/bin/eslint.js ./src", 
    "test": "mocha test", 
    "test-component": "mocha test/component", 
    "install-test-build": "npm install && npm test && npm run linter", 
    "test-build": "npm test && npm run linter" 
    }, 
    "jshintConfig": { 
    "esversion": 6 
    }, 
    "dependencies": { 
    "ajv": "^4.11.3", 
    "body-parser": "^1.17.2", 
    "express": "^4.15.3", 
    "express-winston": "^2.4.0", 
    "request": "^2.81.0", 
    "winston": "^2.3.1", 
    "yamljs": "^0.2.9" 
    }, 
    "devDependencies": { 
    "@pact-foundation/pact-node": "^4.8.3", 
    "dotenv": "^4.0.0", 
    "eslint": "^4.2.0", 
    "eslint-config-node": "^1.6.0", 
    "expect.js": "^0.3.1", 
    "mocha": "^3.2.0", 
    "nodemon": "^1.11.0", 
    "pact": "^2.3.3", 
    "sinon": "^2.3.8", 
    "supertest": "^3.0.0" 
    } 
} 

这是错误我得到: enter image description here

基本上如果测试结果不好,我现在完全不介意。目前的主要问题是协议模拟服务器没有启动。

这里的奇怪的事情是,我有其他项目,协议测试正常运行。我已经将项目中要测试的服务移到了那些无法正常执行这些测试的项目上,并且它正在运行(至少启动了协议模拟服务器)。在这个其他项目中的依赖关系几乎与问题项目相同:

"dependencies": { 
    "ajv": "^4.11.3", 
    "body-parser": "^1.16.1", 
    "dotenv": "^4.0.0", 
    "express": "^4.14.0", 
    "jsonwebtoken": "^7.4.1", 
    "jwt-simple": "^0.5.1", 
    "morgan": "^1.8.1", 
    "mustache-express": "^1.2.4", 
    "node-env-file": "^0.1.8", 
    "request": "^2.79.0", 
    "when": "^3.7.8" 
    }, 
    "devDependencies": { 
    "@pact-foundation/pact-node": "^4.8.3", 
    "eslint": "^3.17.1", 
    "eslint-config-node": "^1.6.0", 
    "expect.js": "^0.3.1", 
    "mocha": "^3.2.0", 
    "nodemon": "^1.11.0", 
    "pact": "^2.3.3", 
    "sinon": "^1.17.7", 
    "supertest": "^3.0.0", 
    "nock": "^9.0.13" 
    } 

这种情况是怎么回事?

编辑:我已经推出了pact测试与DEBUG标志,这是日志中生成: enter image description here enter image description here

+0

我想知道它是否与https://github.com/pact-foundation/pact-js/issues/64有关? –

回答

1

它看起来像服务器无法正确启动,判断退出码为1.这可能是端口冲突端口8989,所以这是值得检查。

它可能与https://github.com/pact-foundation/pact-js/issues/64(Windows文件路径长度有限)有关。

您能不能请DEBUGlogLevel: 'DEBUG'登录并记下它输出的内容,并分享任何*.log文件。这可能是一个参数在启动服务器时未被正确格式化。

正如您所看到的,它无法启动底层的Ruby进程,因此我们需要深入了解底层。

+0

我已经用DEBUG日志启动了测试,并且在问题中显示了输出。没有生成日志文件,这是我拥有的所有信息。我认为这不是端口冲突,因为如果我在其他项目中执行协议测试,它可以正常工作,但在这一个中,即使我更改了端口号,它也总是给我那个错误。 – christiansr85

+0

是的!这是一个路径长度的问题......但我很困惑,因为我没有看到控制台日志中的任何路径超过260个字符。无论如何,我在较短的位置路径下载了该项目,并启动了服务器。 – christiansr85

+0

@ christiansr85有趣。那么你能回答你自己的问题吗?这可能对其他人有用。 –

0

看来,我认为服务器无法启动正常,这通常是因为该端口已被使用。要看到,如果是这样的话,在你的节点命令行(CMD,输入node去节点命令行),复制/粘贴以下:

require('http').createServer(function(){}).listen(11108); 

的“听”功能需要的端口号码,我从你的屏幕截图中拿走。看看什么节点输出;我的猜测是它会显示如下:

Error: listen EADDRINUSE :::11108 
    at Object.exports._errnoException (util.js:1018:11) 
    at exports._exceptionWithHostPort (util.js:1041:20) 
    at Server._listen2 (net.js:1258:14) 
    at listen (net.js:1294:10) 
    at Server.listen (net.js:1390:5) 
    at repl:1:44 
    at sigintHandlersWrap (vm.js:22:35) 
    at sigintHandlersWrap (vm.js:73:12) 
    at ContextifyScript.Script.runInThisContext (vm.js:21:12) 
    at REPLServer.defaultEval (repl.js:340:29) 

这意味着实际上存在端口冲突。看看你的任务管理器,并确保在后台运行的节点没有任何实例可能干扰你的测试,从而启动一个新的服务器。您可能还想检查任何版本的ruby.exe正在运行;不幸的是,我们必须将ruby.exe与pact-node打包在一起,并且它使用ruby转换了一个新的模拟服务器实例,因为这是它写入的语言。不时发生的情况是,节点简单地“失去了跟踪“ruby实例,并没有正确关闭它。

这是一个众所周知的问题,我们正在积极尝试通过将所有核心协议代码合并到一个共享库中来解决问题,该共享库可用于所有语言和操作系统而无需旋转增加一个额外的实例。请忍受我们,因为我们得到了一些这些扭结:)

+0

如果是港口冲突,冲突是不是在8989港口? 11108看起来是PID。 –

+0

啊,是的,误读了端口号。 –

+0

@J_A_X Matthew指出,端口是8989,而11108是由Pact生成的内部PID,它在每次测试的执行过程中都是不同的。无论如何,我也试图改变端口号,我仍然有同样的问题。 – christiansr85

0

最后,经过@Matthew研究员答案,读他的链接https://github.com/pact-foundation/pact-js/issues/64后,我搬到我的项目,以更短的路径位置,simimlar到D:\myproject\,然后pact模拟服务器发起和我的测试中获得通过。因此,直到找到更好的解决方案,我将在该短路径目录下工作,以避免出现更多问题。

请阅读前面的链接,以获取有关此错误的更多信息,因为有很好的解释。