对象不是函数 - SuperTest

时间:2015-07-08 07:12:42

标签: node.js koa supertest

我不确定为什么它会抛出这个错误,而且它究竟是什么意思不是一个功能。

    'use strict';

  var koa = require("koa");
    var app = koa();
    var chai = require('chai');
    var expect = chai.expect;

    var request = require('supertest');

    describe('Feature: Car REST Endpoint', function () {

        context('Scenario: GET a Car', function () {

            var url = 'http://localhost/search/cars';

            describe('Given: the resource is accessed at the resource url' + url, function () {

                describe('Then: we receive a successful response', function(){

                    it('status code should be 200', function (done){
                        request(app)
                            .get(url)
                            .expect(200,done);
                    });
                });

它说这是行.expect(200,已完成),但我可能错了。

我也试过这个没有运气:

 request(app)
                    .get(url)
                    .expect(200)
                    .end(function(err, res){
                        if (err) return done(err);
                        done()
                    });

我也试过var request = require('supertest').agent(koa);

enter image description here

1 个答案:

答案 0 :(得分:2)

您的问题是您将http://localhost传递给#get(url)。将url更改为/search/cars。我有一个完整的复制品(使用快递)

var request = require('supertest'),
express = require('express');

var app = express();

app.get('/user', function(req, res) {
  res.send(200, {
    name: 'tobi'
  });
});

request(app)
  .get('http://localhost/user')
  .expect(200)
  .end(function(err) {
    if (err) throw err;
    console.log('Success!');
  });

输出:

    if (res.status !== status) {
           ^
TypeError: Cannot read property 'status' of undefined
    at Test.assert (C:\workspace\choose-your-own\node_modules\supertest\lib\test.js:202:12)
    at Server.assert (C:\workspace\choose-your-own\node_modules\supertest\lib\test.js:131:12)
    at Server.g (events.js:199:16)
    at Server.emit (events.js:104:17)
    at net.js:1392:10
    at process._tickCallback (node.js:355:11)

当我使用/user时,我得到:

Success!