Javascript 不考虑元素变化的角度指令的单元测试
我有一个问题,使我的单元测试工作。 在broswer中,我的指令非常有效,但当我测试它时,我的测试失败了。 代码如下:Javascript 不考虑元素变化的角度指令的单元测试,javascript,angularjs,unit-testing,Javascript,Angularjs,Unit Testing,我有一个问题,使我的单元测试工作。 在broswer中,我的指令非常有效,但当我测试它时,我的测试失败了。 代码如下: angular.module('helloWorldApp') .directive('helloworld', function () { var helloworld = {}; class Hello { constructor(name) { this.name = name ; }
angular.module('helloWorldApp')
.directive('helloworld', function () {
var helloworld = {};
class Hello {
constructor(name) {
this.name = name ;
}
SayHello() {
//console.log('Hello ' + this.name);
return 'Hello ' + this.name;
}
};
helloworld.template = '<label for="name">Your Name </label> <input type="text" name="name" ng-model="name"/><br/><div id="result"></div>';
helloworld.restrict = 'E';
helloworld.link = (scope,element,attrs) => {
scope.$watch('name', () => {
if (scope.name) {
let HelloMe = new Hello(scope.name);
let helloStr = HelloMe.SayHello();
element.find('#result').text(helloStr);
console.log(element.find('#result').text());
};
});
};
return helloworld;
});
我知道有点晚了 您确定粘贴了正确的代码吗 刚刚更改了angular.module'helloWorldApp',[],但我想您已经在其他地方初始化了此模块 它工作得很好:
'use strict';
describe('Directive: helloworld', function () {
// load the directive's module
beforeEach(module('helloWorldApp'));
var element,
scope;
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope.$new();
element = angular.element('<helloworld></helloworld>');
element = $compile(element)(scope);
}));
describe('Test', function() {
it('Hello Lola', inject(function ($compile) {
var inputElt = element.find('input');
console.log(scope.name);
inputElt.val('Lola');
inputElt.triggerHandler('input');
element = $compile(element)(scope);
scope.$digest();
console.log(scope.name);
expect(scope.name).toBeDefined();
expect(element.find('input').val()).toBe('Lola');
expect(element.find('#result').text()).toBe('Hello Lola');
}));
});
});
LOG: undefined
LOG: ''
LOG: ''
LOG: 'Lola'
Chrome 38.0.2125 (Windows 7) Jasmine__TopLevel_
Hello Lola FAILED
Expected '' to be 'Hello Lola'.
Error: Expected '' to be 'Hello Lola'.