Mocha使用supertest,typeError:无法读取undefined的属性'call'

问题描述 投票:0回答:3

我正在学习为我的基本Todo应用程序使用异步测试。但我发现在为我的应用程序开发测试套件时出现了一个错误,

我想用我的测试套件删除待办事项。

这是我的代码:

app.delete('/todos/:id', (req,res) => {
    const id = req.params.id ;

    if(!ObjectID.isValid(id))
        return res.status(400).send();

    Todo.findByIdAndRemove(id)
    .then((todo) => {
        res.send(todo);
    }, (error) => {
        res.status(404).send();
    });        
});

以下是测试套件的代码:

const todos = [{
        _id: new ObjectId(),
        text: 'first Todo'
    },
    {
        _id: new ObjectId(),
        text: 'Second Todo'
    }
];

beforeEach((done) => {
    Todo.remove({}).then(() => {
        return Todo.insertMany(todos);
        done();
    }).then(() => {
        done();
    }).catch(e => {
        console.log(e);
        done();
    });
});

describe('DELETE /todos/:id', () => {
    it('should delete a todo', (done) => {

        request(app)
            .delete(`/todos/${todos[1]._id.toHexString()}`)
            .expect(200)
            .end(done());
    });
});

我发现了一个类似的错误:

 Uncaught TypeError: Cannot read property 'call' of undefined
      at Test.assert (node_modules/supertest/lib/test.js:181:6)
      at Server.assert (node_modules/supertest/lib/test.js:131:12)
      at emitCloseNT (net.js:1655:8)
      at _combinedTickCallback (internal/process/next_tick.js:135:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

谢谢

node.js testing mocha supertest
3个回答
1
投票

在测试用例结束之前,您正在调用done()。这似乎是个问题。

request(app)
  .delete(`/todos/${todos[1]._id.toHexString()}`)
  .expect(200)
  .end(done); // pass the callback, not the result of executing the callback

0
投票

我猜,这是由不整洁的done电话引起的。我的建议是避免使用done,因为mocha通过指定return来保证函数来支持promise。

我正在帮助您改进代码,如下所示:

const todos = [{
    _id: new ObjectId(),
    text: 'first Todo'
  },
  {
    _id: new ObjectId(),
    text: 'Second Todo'
  }
];

beforeEach(() => {
  // I removed `done` and add `return` 
  return Todo.remove({})
    .then(() => {
      return Todo.insertMany(todos);
    }).catch(e => {
      console.log(e);
    });
});

describe('DELETE /todos/:id', () => {
  it('should delete a todo', () => {
    return request(app)
      .delete(`/todos/${todos[1]._id.toHexString()}`)
      .expect(200);
  });
});

它更干净,不是吗?

希望能帮助到你


0
投票

我有同样的问题。因为使用.end(done())而不是.end(done)这是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.