我的茉莉花测试(使用业力)抛出错误:Argument 'salesListController' is not a function, got undefined
在搜索了很多这个问题之后似乎接近我的错误,但这是单元测试而不是 e2e 场景测试。
我的 test-main.js
(function (window, require) {
'use strict';
var file, requireModules;
requireModules = [];
for (file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
// console.log('loaded file'+ file);
if (file.substring(file.length - 26, file.length) === 'salesListControllertest.js') {
console.log('Added file to testing..');
requireModules.push(file);
}
}
}
//requireModules.push('appModule');
//requireModules.push('mocks');
deps: requireModules,
require({
baseUrl: '',
paths:{
'angular': '/base/app/bower_components/angular/angular',
'angularResource': '/base/app/bower_components/angular-resource/angular-resource',
'angularMocks': '/base/app/bower_components/angular-mocks/angular-mocks',
'appModule': '/base/app/scripts/appModule',
'appRoutes':'/base/app/scripts/appRoutes',
'services/dependencyResolverFor':'/base/app/scripts/services/dependencyResolverFor',
'salesListController' : '/base/app/scripts/controllers/salesListController'
},
shim:{
'angular' :{
exports:'angular'
},
'appRoutes': {exports:'appRoutes'},
'services/dependencyResolverFor' : {exports:'services/dependencyResolverFor'},
'appModule': {
deps: ['appRoutes', 'services/dependencyResolverFor'],
exports: 'appModule'
},
'angularResource': {
deps: ['angular'],
exports: 'angularResource'
},
'angularMocks': {
deps: ['angularResource'],
exports: 'angularMocks'
} ,
'salesListController': {
deps: ['appModule'],
exports: 'salesListController'
}
}
}, requireModules, function () {
window.__karma__.start();
}, function (err) {
var failedModules = err.requireModules;
console.log("err", err);
if (failedModules && failedModules[0]) {
throw new Error("Module could not be loaded: " + failedModules);
} else {
throw new Error("unknown error:" + err);
}
}); }(window, require));
我的 example.spec
define(['appModule','angular', 'angularResource', 'angularMocks','salesListController'],
function(app, angular, angularResource, angularMocks, saleslstCtrl) {
describe('SalesListController1', function(){
beforeEach(module('AngularSampleBhoomiApp'));
var SalesListController, scope;
var sales = [{Customer:"A1",Number:1,Id:1},{Customer:"B1",Number:2,Id:2}];
beforeEach(inject(function($controller, $rootScope,$injector){
scope = $rootScope.$new();
SalesListController = $controller('salesListController', {$scope:scope, getAllSalesResolved:sales});
}));
it('should have 0 items when loaded', function(){
expect(scope.sales).toBeUndefined();
});
});});
我的示例控制器
define(['appModule'], function(myApp){
function SalesLstCtrl(){
myApp.lazy.controller('salesListController' ,['$scope', 'getAllSalesResolved',
function ($scope, sales) {
console.log('before sales');
$scope.sales= sales;
console.log( $scope.sales.length);
}]); }
return SalesLstCtrl;});
由于我使用延迟加载,我正在解决这样的依赖项:
define([], function()
{
return function(dependencies)
{
var definition =
{
resolver: ['$q','$rootScope', function($q, $rootScope)
{
var deferred = $q.defer();
require(dependencies, function()
{
$rootScope.$apply(function()
{
deferred.resolve();
});
});
return deferred.promise;
}]
}
return definition;
} });
我的 github repo 与整个示例在这里
请帮忙。