Angularjs 类型错误:';未定义';在使用带业力的间谍时不是一个函数
我一直在使用Yeoman设置项目,并试图构建一个角度模块项目,该项目将应用功能切换。我正忙于单元级的Jasmine测试,下面是我的一个测试:Angularjs 类型错误:';未定义';在使用带业力的间谍时不是一个函数,angularjs,jasmine,karma-runner,Angularjs,Jasmine,Karma Runner,我一直在使用Yeoman设置项目,并试图构建一个角度模块项目,该项目将应用功能切换。我正忙于单元级的Jasmine测试,下面是我的一个测试: var scope, Rules; var elm; // load the controller's module beforeEach(function() { module('featureToggle.directives', function($provide) { $provide.decorator('Rules', functi
var scope, Rules;
var elm;
// load the controller's module
beforeEach(function() {
module('featureToggle.directives', function($provide) {
$provide.decorator('Rules', function($delegate) {
$delegate.resolveByName = jasmine.createSpy();
return $delegate;
});
});
inject(function(_Rules_) {
Rules = _Rules_;
});
});
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
elm = angular.element(
'<h1 feature-toggle feature-name="hello">' +
'Hello World' +
'</h1>');
scope = $rootScope;
$compile(elm)(scope);
scope.$digest();
}));
it('should render the content when the feature is enabled', function() {
jasmine.spyOn(Rules, 'resolveByName').andReturn(false);
var contents = elm.find('h1');
expect(contents.eq(0)).toHaveClass('hidden');
});
karma.conf非常普通
frameworks = ['jasmine'];
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
'app/components/angular/angular.js',
'app/components/angular-mocks/angular-mocks.js',
'src/*.js',
'src/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
];
大体上,我使用的是PhantomJS,但Chrome中也存在同样的问题(使用相同错误的不同方言)
使用$log,实际上没有未定义的内容。所以我很迷茫。将
jasmine.spyOn()
更改为spyOn()
顺便说一句,我不知道你为什么要监视同一个方法两次。试试看
spyOn(Rules, 'resolveByName').and.returnValue(false);
你找到这个问题的解决办法了吗?
spyOn(Rules, 'resolveByName').and.returnValue(false);