我需要一些帮助来解决我在nodejs代码上测试的问题。我使用的是mocha和supertest。我对supertest中的实现感到困惑。我不知道解决它。我试图自动下载文件。
`describe('GET /entry/:entryId/file/:id/download', function(){
it('should pass download function', function(done){
this.timeout(15000);
request(app.webServer)
.get('/entry/543CGsdadtrE/file/wDRDasdDASAS/download')
.set('Authorization', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGco')
.expect(200)
.end(function(err, res){
if (err) return done(err);
console.log(err, res);
done();
});
});
});
答案 0 :(得分:16)
在测试快递应用时,我收到了来自摩卡的类似错误。错误全文:
0 passing (185ms)
2 failing
1) loading express responds to /:
TypeError: app.address is not a function
at Test.serverAddress (test.js:55:18)
at new Test (test.js:36:12)
at Object.obj.(anonymous function) [as get] (index.js:25:14)
at Context.testSlash (test.js:12:14)
2) loading express 404 everything else:
TypeError: app.address is not a function
at Test.serverAddress (test.js:55:18)
at new Test (test.js:36:12)
at Object.obj.(anonymous function) [as get] (index.js:25:14)
at Context.testPath (test.js:17:14)
我通过将其添加到我的express server.js来修复它,即导出服务器对象
module.exports = app
答案 1 :(得分:5)
遇到此错误的打字稿用户检查两件事:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if let text = cell.textLabel?.text {
let indexOfCellString = event.index(text)
// do stuff with indexOfCellString
}
return cell
}
(感谢@Collin D)module.exports = app
import * as app from "./app"
答案 2 :(得分:1)
我正面临着同样的问题,以上解决方案对我不起作用,有些人穿鞋 请关注这个家伙的
server.js中的导出应该
module.exports.app = app;
如果您有多个模块而不是使用es6功能
module.exports = {
app,
something-else,
and-so-on
}
我的package.json用于版本交叉引用。
{
"name": "expressjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha **/*.test.js",
"start": "node app.js",
"test-watch": "nodemon --exec npm test"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4",
"hbs": "^4.0.1"
},
"devDependencies": {
"mocha": "^5.2.0",
"supertest": "^3.3.0"
}
}