처음으로 jasmine을 배우고 각도 서비스에서 focus() 기능을 테스트하려고 할 때이 오류가 발생합니다. 여기 는 서비스 : 여기 angularjs + jasmine : 서비스에서 포커스 테스트
myApp.service('MyService', function($timeout, $window) {
var service = {
focusElem: focusElem
};
return service;
function focusElem(id) {
console.log('id of element is = ', id);
if (id) {
$timeout(function() {
var element = $window.document.getElementById(id);
console.log('element is = ', element);
if (element) {
element.focus();
}
});
}
};
});
내 사양 파일입니다
describe('myApp', function() {
var element, dummyElement;
beforeEach(function() {
// Initialize myApp injector
module('myApp');
// Inject instance of service under test
inject(function($injector) {
MyServiceObj = $injector.get('MyService');
});
element = angular.element('<input id="firstName" name="firstName"/>');
dummyElement = document.createElement('input');
dummyElement.setAttribute('id', 'lastName');
});
it('should have focus if the focus Service is used on an element', function() {
console.info('------------------');
spyOn(element[0], 'focus');
spyOn(dummyElement, 'focus');
MyServiceObj.focusElem(dummyElement.getAttribute('id'));
expect(dummyElement.focus).toHaveBeenCalled();
});
});
내 오류 : 당신 ngMock
많은 서비스를 사용하고 이렇게 변경하면
myApp should have focus if the focus Service is used on an element
Expected spy focus to have been called.
Error: Expected spy focus to have been called.
[카르마와 앵글로 쟈스민 모의 체인으로 연결된 방법] ([http://stackoverflow.com/questions/36285896/jasmine-mock-chained-methods-with-karma-and-angular)의 가능한 복제본 – estus
http : 'focus'가'callThrough'이고 포커스 이벤트를 트리거하는 경우 //stackoverflow.com/a/33742298/3731501 – estus
이 기능을 사용하셨습니까? – tasseKATT