对象不是函数 - SuperTest

Object is not a Function - 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,done) 行,但我可能错了。

我也尝试过,但没有成功:

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

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

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

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!