Xeu*_*eus 6 javascript unit-testing jasmine angularjs karma-runner
我试图通过Karma用Jasmine测试我的AngularJS应用程序.我收到此错误(至少,这是最新的错误):
Uncaught TypeError: Cannot read property '$modules' of null
at /Users/benturner/Dropbox/Code/galapagus/app/static/js/angular-mocks.js:1866
Run Code Online (Sandbox Code Playgroud)
来自我的karma.conf.js:
files: [
'static/js/jquery.min.js',
'static/js/angular.min.js',
'static/js/angular-mocks.js',
'static/js/angular-resource.min.js',
'static/js/angular-scenario.js',
'static/js/angular-loader.min.js',
'static/js/momentous/ctrl_main.js', // contains all my app's code
'test/momentous.js'
],
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
(function () {
"use strict";
var controller = null;
var scope = null;
describe("Services", inject(function($rootScope, Moments) {
var mockedFactory, moments, flag, spy;
moments = [{name: 'test'}];
beforeEach(module('momentous', function($provide) {
scope = $rootScope.$new();
$provide.value('$rootScope', scope);
mockedFactory = {
getList: function() {
return moments;
}
};
spy = jasmine.createSpy(mockedFactory.getList);
$provide.value('Moments', mockedFactory);
}));
it('should return moments from the factory service', function() {
runs(function() {
console.log(scope.getList);
flag = false;
setTimeout(function() {
scope.getList();
flag = true;
}, 500);
});
waitsFor(function() {
return flag;
}, "The call is done", 750);
runs(function() {
expect(scope.moments).toEqual([{name: 'test'}]);
expect(spy).toHaveBeenCalled();
});
});
}));
}());
Run Code Online (Sandbox Code Playgroud)
所以我要做的是模拟我的工厂服务并检查它是否返回一个对象数组并将它们设置为$ scope中的变量.
那里也有一个异步调用,所以我不得不使用runs()和waitsFor().
我仍然不明白我是如何注入我的$ scope以便我可以对它进行测试,并且现在让angular-mocks.js给我一个错误,我觉得我已经越来越远离解决这个问题,而不是更接近.
我从各种文档,指南和stackoverflow答案拼凑了这些.任何指导?谢谢.
Jay*_*012 10
我也遇到了这个确切的错误.我的代码类似于我尝试测试提供程序的地方,因此我调用模块并将其传递给配置提供程序的函数.
解决了:
我发现问题是由于调用"inject"将委托返回到"describe"方法.您只能使用inject将委托返回"it".
例如:
describe('something', inject(function(something) {})); // will throw the $module is null error
Run Code Online (Sandbox Code Playgroud)
但这会奏效:
it('something', inject(function(something) {})); // works :)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4777 次 |
最近记录: |