在AngularJs中,我必须使用茉莉花来测试过滤器。我在控制器中的session中设置了studentNameFormat。我有一个像这样的过滤器:
angular.module('course.filters').filter('studentName',['Session',
function (Session) {
return function (student) {
var format = Session.getState().studentNameFormat;
if(format == '0') {
return student.firstName + " " + student.lastName;
}
else{
return student.lastName + student.firstName;
}
};
}]);
为了测试Jasmine中的方法,我需要将Session对象传递给该方法。如何创建模拟会话变量?
I have test filter like:
describe('studentName', function () {
'use strict';
var $filter;
var student;
var session;
beforeEach(function () {
module('course.filters');
inject(function (_$filter_, _Session_) {
$filter = _$filter_;
session = _Session_;
});
patient = {
"firstName": "John",
"lastName": "Doe"
}
session.setState({
studentNameFormat: 1
});
});
it('return full lastName full FirstName if given 1', function () {
console.log(patient);
var studentFilter = $filter('studentName');
expect(studentFilter (student)).toEqual("Doe John");
});
它给了我错误sessionProvider未定义。 错误:[$ injector:unpr]未知提供者:SessionProvider< - Session< - studentFilter