1

我正在为一个函数编写测试代码,该函数将检查 twitter 中是否存在用户名。我正在使用 Hapi 框架和实验室进行测试。

当我运行时,npm test我收到以下错误:

> NameGen@0.0.0 test /Users/mario/projects/NameGen
> ./node_modules/lab/bin/lab -c


/Users/mario/projects/NameGen/test/test.js:5
Lab.experiment( "Test Username Existence", function() {
    ^
TypeError: Object #<Object> has no method 'experiment'
    at Object.<anonymous> (/Users/mario/projects/NameGen/test/test.js:5:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js [as origLoader] (module.js:474:10)
    at Object.require.extensions..js (/Users/mario/projects/NameGen/node_modules/lab/lib/coverage.js:32:26)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at /Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:85:23
    at Array.forEach (native)
    at Object.internals.traverse (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:81:19)
    at Object.exports.run (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:30:29)
    at Object.<anonymous> (/Users/mario/projects/NameGen/node_modules/lab/bin/lab:3:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

我的测试代码是:

var Lab = require("lab");
var server = require('../'); // require index.js
var request = require('request');

Lab.experiment( "Test Username Existence", function() {
  // tests

  Lab.test( "Test username exists", function(done){
    var options = {
      url: 'http://twitter.com/BarackObama'
    };

    request(options, function(error, response, body){
      Lab.expect(response.statusCode).to.equal(200);
    });
      done();
    });

    Lab.test("Test username does not exists", function(done){
      var options = {
        url: 'http://twitter.com/jhkhksdhkjahsdfkjhasdf'
      };

      request(options, function(error, response, body){
        Lab.expect(response.statusCode).to.equal(404);
      });
        done();
      });

  });

package.json我在我的文件中安装了最新版本的节点和所有必要的依赖项。什么可能导致此错误?

4

2 回答 2

6

正如@Jordonias 所说。但是,当我使用他的示例时,它仍然不起作用。我不得不将第二行更改为:

lab = exports.lab = Lab.script()
于 2014-09-24T06:26:51.010 回答
1

根据实验室文档:Test files must require the lab module, and export a test script

测试.js

var Lab = require('lab');
var lab = module.exports = Lab.script();

lab.experiment( "Test Username Existence", function() {
  lab.test( "Test username exists", function(done){

   });
});
于 2014-08-22T17:07:52.493 回答