Javascript 摩卡咖啡测试:';类型错误:未定义的不是函数';在Test.serverAddress
我有以下测试:Javascript 摩卡咖啡测试:';类型错误:未定义的不是函数';在Test.serverAddress,javascript,node.js,testing,express,mocha.js,Javascript,Node.js,Testing,Express,Mocha.js,我有以下测试: describe('Testing the GET methods', function() { it('Should be able to get the list of articles', function(done) { // Create a SuperTest request request(app).get('/api/articles/') .set('Accept', 'application/json')
describe('Testing the GET methods', function() {
it('Should be able to get the list of articles', function(done) {
// Create a SuperTest request
request(app).get('/api/articles/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
res.body.should.be.an.Array.and.have.lengthOf(1);
res.body[0].should.have.property('title', article.title);
res.body[0].should.have.property('content', article.content);
done();
});
});
it('Should be able to get the specific article', function(done) {
// Create a SuperTest request
request(app).get('/api/articles/' + article.id)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
res.body.should.be.an.Object.and.have.property('title', article.title);
res.body.should.have.property('content', article.content);
done();
});
});
});
这将产生以下错误:
你知道可能是什么问题吗?我已经检查并安装了所有依赖项,并且是必需的
编辑:
发生这种情况的原因如下:-第11到18行
我的测试在应用程序连接到数据库之前开始。我通过console.logging“app”注意到了这一点,也注意到了这一点
服务器运行于http://localhost:3000/
在第一次测试期间的不同时间记录
如何让mocha在运行测试之前等待应用程序被定义?好的,它是这样的: 更新版本的“mongoose”导致express在我创建
var app=express(db)之前没有定义的“db”连接代码>。但是,如果我添加以下代码:
db.connection.on('connected', callback);
在回调中,我定义了app
,测试在没有定义app的情况下执行
除了有两个不同的版本,一个用于测试环境,另一个用于开发/生产之外,我真的不知道如何解决这个问题。您在这里定义了
app
。当您查看调用堆栈中提到的第一个文件(以supertest\lib'test.js结尾)的第55行时,您可以检查整个源代码是否有用,它是否为:var addr=app.address()代码>?是的。我也找到了问题的核心,但还没有找到解决方案。检查编辑