【发布时间】:2020-02-27 10:04:09
【问题描述】:
我正在尝试重构我的 Ember 验收测试以不使用 deprecated authorize 方法,因为它会引发警告:
The `authorize` method should be overridden in your application adapter
我查看了docs 和许多其他来源,但它们实际上并没有解释如何 迁移我的代码。这是我目前得到的:
// projectname/app/pods/login/controller.js (excerpt)
export default Controller.extend({
session: service(),
sessionToken: null,
onSuccess: function(res) {
res = res.response;
this.set('sessionToken', res.session);
if (res.state === "authenticated") {
document.cookie = "token="+res.session+";path=/;";
var authOptions = {
success: true,
data : {
session : res.session,
}
};
this.get('session').authenticate("authenticator:company", authOptions);
}
}
});
这一定是我要摆脱的部分:
// project/app/adapters/application.js (excerpt)
export default DS.RESTAdapter.extend(DataAdapterMixin, {
authorize(xhr) { // This is deprecated! I should remove it
let sessionToken = this.get('session.data.authenticated.session');
if (sessionToken && !isEmpty(sessionToken)) {
xhr.setRequestHeader('Authorization', "Token " + sessionToken);
}
},
});
这是我的测试:
import { test, module } from 'qunit';
import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { authenticateSession} from 'ember-simple-auth/test-support';
module('moduleName', function(hooks) {
setupApplicationTest(hooks);
test('moduleName', async function(assert) {
// await authenticateSession(this.application); // Never works
// await authenticateSession(); // Never works
await authenticateSession({
authenticator: "authenticator:company"
}); // Works slightly more?
await visit('/my/other/page');
await assert.equal(currentURL(), '/my/other/page');
});
});
删除authorize 方法并尝试任一注释掉的方法会产生:
Error: Assertion Failed: The `authorize` method should be overridden in your application adapter. It should accept a single argument, the request object.
如果我使用authenticator 块作为参数,那么无论authorize 方法是否存在,我都会得到:
actual: >
/login
expected: >
/my/other/page
我猜是因为它没有登录。
将authorize 方法留在那里,并尝试注释的方法产生:
Error: Browser timeout exceeded: 10s
【问题讨论】:
标签: ember.js ember-simple-auth ember-testing