我有一个身份验证服务,我正在尝试为其编写单元测试。
当 Auth 服务中没有存储先前的转换时,它会重定向到特定的路由。
该应用程序按预期工作,但是此服务的测试出现以下错误:
Uncaught TypeError: Cannot read property 'hasRoute' of undefined
我不知道如何调试/解决这个问题。 这是怎么回事?
这是我的身份验证服务:
import Ember from 'ember';
import config from '../config/environment';
export default Ember.Service.extend({
routing: Ember.inject.service('-routing'),
loginCapable: true,
localStorageKey: config.localStorageKeys.user,
checkAuth(priorTransition){
this.set('priorTransition', priorTransition);
return this.get('isLoggedIn');
},
isLoggedIn: Ember.computed('user', function(){
return !!this.get('user');
}),
init(){
this.set('user', JSON.parse(window.localStorage.getItem( this.get("localStorageKey") )) );
},
login(adID){
return Ember.$.ajax(
config.apiHost + config.apiVersion + "/getLdapInfo",{
type: 'GET',
data: "adID="+adID
}).then(json => {
this.set('user', json);
window.localStorage.setItem( this.get("localStorageKey") , JSON.stringify(json));
var priorTransition = this.get('priorTransition');
if( priorTransition )
{
priorTransition.retry();
this.set('priorTransition', null);
}else{
this.get('routing').transitionTo('index');
}
}, xhr => {
return xhr.responseJSON ? xhr.responseJSON.message : "Login failed";
});
}
});
还有我的路由器:
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('authenticated', { path: '/' }, function(){
this.route('index', { resetNamespace: true });
});
this.route('login');
});
export default Router;
这是该服务的测试:
import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
import startApp from 'recruiter-admin/tests/helpers/start-app';
import Pretender from 'pretender';
var server,
application;
var user = [{
"adID":"uniqueID",
"first_name":"First Name",
"last_name":"Last Name",
"employee_type":"Regular",
"employee_id":"12345",
"manager":"CN=managerID,OU=Employees,OU=company Users,DC=company,DC=com",
"success":true
}];
function jsonResponse(json, status = 200) {
return [status, {"Content-Type": "application/json"}, JSON.stringify(json)];
}
moduleFor('service:auth', 'Unit | Service | auth', {
unit:true,
needs:['router:main'],
beforeEach(){
application = startApp();
server = new Pretender(function(){
this.get('/getLdapInfo', function(req){
return jsonResponse(user.findBy('adID',req.queryParams.adID));
});
});
},
afterEach(){
Ember.run(application, 'destroy');
if( server ){
server.shutdown();
}
}
});
// Replace this with your real tests.
test('it exists', function(assert) {
var service = this.subject();
assert.ok(service);
});
test('isLoggedIn Testing', function(assert){
var service = this.subject();
assert.ok(service.get('isLoggedIn') === false, 'Not logged in');
Ember.run(()=>{
service.set('user', true);
});
assert.ok(service.get('isLoggedIn') === true, 'IS logged in');
});
test('can login', function(assert){
assert.expect(0);
var service = this.subject();
Ember.run(()=>{
service.login('uniqueID');
});
andThen(()=>{
assert.ok(service.get('user'));
});
});
使用 Ember 2.0.0-beta.5
您正在使用私有-routing
服务,它需要初始化和设置router
实例。 此测试不会这样做,因此您必须模拟它或设置路由器。
我不反对稍微调整https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/services/routing.js#L37以防止这个错误(它最终应该返回如果get(this, 'router')
为假则为假)如果您想提交 PR,但以不受支持的方式使用私有 API 并不是错误。
关闭此问题,但我很乐意查看 PR。
比赛迟到了,但我只想展示我解决这些问题的方法。
只需覆盖方法。
const service = this.subject({
routing: {
transitionTo() {
return true;
}
}
})
我这样做是因为我已经为测试不同的路由本身编写了测试,也信任 ember 团队来测试ember/service/routing.js
this.owner.unregister('service:router');
this.owner.register('service:router', mocks.routerService());
一种在不使测试用例失败的情况下对其进行存根的方法
最有用的评论
您正在使用私有
-routing
服务,它需要初始化和设置router
实例。 此测试不会这样做,因此您必须模拟它或设置路由器。我不反对稍微调整https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/services/routing.js#L37以防止这个错误(它最终应该返回如果
get(this, 'router')
为假则为假)如果您想提交 PR,但以不受支持的方式使用私有 API 并不是错误。