Javascript Strongloop环回远程挂钩未使用supertest触发?
我们正在使用spec.js文件测试环回API代码,如下所示: 要求LIB:Javascript Strongloop环回远程挂钩未使用supertest触发?,javascript,mocha.js,loopbackjs,strongloop,supertest,Javascript,Mocha.js,Loopbackjs,Strongloop,Supertest,我们正在使用spec.js文件测试环回API代码,如下所示: 要求LIB: var app = rewire('../..'); var request = require('supertest'); var assert = require('chai').assert; 用于标准化标题和内容类型的json帮助器方法: function json(verb, url) { return request(app)[verb](url) .set('Content-Type
var app = rewire('../..');
var request = require('supertest');
var assert = require('chai').assert;
用于标准化标题和内容类型的json帮助器方法:
function json(verb, url) {
return request(app)[verb](url)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.expect('Content-Type', /json/);
}
需要身份验证的自定义远程方法的测试:
describe("Order remote methods", function() {
var accessTokenId, userId;
// authenticate before each test and save token
before(function(done) {
json('post', '/api/People/login')
.send({ email: 'user@email.com', password: 'password' })
.expect(200)
.end(function(err, res) {
accessTokenId = res.body.id;
userId = res.body.userId;
assert(res.body.id);
assert(res.body.userId);
done();
});
});
it("should fetch user orders", function(done) {
json('get', '/api/Orders/specialOrders')
.set('Authorization', accessTokenId)
.send({id: userId})
.expect(200)
.end(function(err, res) {
var orders = res.body.orders;
assert(Array.isArray(orders), "Orders should be an array");
// more asserts for explicit data values
done();
});
});
});
/api/Orders/speciallorders
是一种自定义远程方法,它对订单模型执行自定义查询,并按预期工作。但是当我为这个模型添加一个beforemote
钩子时,它不会被运行测试触发。这是预期的还是我的测试设置没有完成
远程挂钩:
Order.beforeRemote('specialOrders', function(ctx, unused, next) {
console.log('[userOrders]');
console.log('ctx req token: ', ctx.req.accessToken.userId);
console.log('ctx args: ', ctx.req.params.id);
// prevent remote method from being called
// even without a next(), remote is executed!
next(new Error('testing error'));
});
通过Explorer UI运行相同的自定义方法,会按预期触发beforemote
钩子,并报告自定义错误(或在next()
不存在时挂起)
有没有可能让supertest在这样的测试中触发远程钩子,或者我在规范文件中缺少了一些
app
设置?测试的服务器启动脚本是什么?我使用beforeRemote钩子,这是我在常规(非测试)运行中遇到的情况,即即使beforeRemote返回下一个(错误);远程执行,但是我仍然从错误对象获得正确的错误代码和消息,因此我猜测远程钩子是执行的,不管beforeRemote做什么,除了最后我们从beforeRemote钩子返回结果对象(即错误)你在测试中得到的err
和res.body
是什么?我正在使用supertest调用我的环回API,我有带有beforeRemote钩子的模型,这些钩子被正确触发。我想我面临着与您类似的错误。你找到错误了吗?@Kaleidopon抱歉,这是很久以前的事了,我们停止了这种方式的测试。现在我们正在做更多的单元测试,不必担心CAL是通过rest端点生成的,但它仍然会不时出现。