0
我試圖在業力中設置單元測試來測試角度1. *控制器。每次我運行代碼,我得到角1。*無法在業力單元測試中訪問控制器(webpack)
[ng:areq] Argument 'MoviesListCtrl' is not a function, got undefined
當我檢查了模塊控制器似乎得到加載,所以我不知道爲什麼它不加載。我的項目可以在這裏找到..
我的項目可以用在到位桶
https://bitbucket.org/LAD500/movies-angular-webpack/src
運行測試..
npm install
npm test
任何幫助,將不勝感激。
文件我用的工作..
import '../../../../../src/js/app/services/movieslisting/';
import '../../../../../src/js/app/directives/movieslist/movieslistctrl';
import '../../../../../src/js/app/browserapp.js';
import {expect} from 'chai';
let createCtrl, scope, moviesListing;
beforeEach(angular.mock.module('BrowserApp'));
beforeEach(inject(($rootScope, $controller)=>{
moviesListing = angular.injector(['ng', 'BrowserApp']).get("moviesListing");
scope = $rootScope.$new();
createCtrl =()=>{
return $controller('MoviesListCtrl', {
$scope: scope,
moviesListing: moviesListing
});
};
}));
describe('MoviesListCtrl',()=>{
it('placeholder test',()=>{
let ctrl = createCtrl();
expect(1).to.equal(1);
});
});
控制器
import app from '../../browserapp';
let controllerName = 'MoviesListCtrl';
app.controller('MoviesListCtrl', ['$scope', 'moviesListing', function($scope, moviesListing) {
$scope.moviesList = {
moviesFilter: 'costLowToHigh',
movies: moviesListing.movieCollection
};
$scope.createMovieHeader = movie =>{
let prefix = `${movie.title} (${movie.category}`;
return movie.subcategory ? `${prefix}/${movie.subcategory})` : `${prefix})`;
};
$scope.updateFilter =()=>{
moviesListing.sortMovies($scope.moviesList.moviesFilter);
};
}]);
export default controllerName;
服務那一個依賴
import app from '../../browserapp';
let factoryName = 'moviesListing';
app.factory(factoryName, [function() {
let movieCollection = [];
let currentSortKey = 'costLowToHigh';
let costLowToHigh = (a, b)=>{
if (a.castListTotalCost > b.castListTotalCost) {
return 1;
}
if (a.castListTotalCost < b.castListTotalCost) {
return -1;
}
return 0;
};
let costHighToLow = (a, b)=>{
if (a.castListTotalCost < b.castListTotalCost) {
return 1;
}
if (a.castListTotalCost > b.castListTotalCost) {
return -1;
}
return 0;
};
let sortFunctionsLookup = {
costLowToHigh,
costHighToLow
};
let castListTotalCost = movie => movie.actors.reduce((total, actor) => total + actor.salary, 0);
let addMovie = (movie)=>{
movie.castListTotalCost = castListTotalCost(movie);
movieCollection.push(movie);
movieCollection.sort(sortFunctionsLookup[currentSortKey]);
};
let sortMovies = (sortKey)=>{
currentSortKey = sortKey;
movieCollection.sort(sortFunctionsLookup[currentSortKey]);
};
let reset =()=>{
movieCollection.length = 0;
};
return {
movieCollection,
addMovie,
sortMovies,
reset
};
}]);
export default factoryName;
應用模塊
import * as angular from 'angular';
var app = angular.module('BrowserApp', []);
app.init =()=>{
angular.element(document).ready(function() {
angular.bootstrap(document, ['BrowserApp']);
});
};
export default app;
噶配置
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'es6-shim'],
files: [
'./node_modules/angular/angular.js',
'./node_modules/angular-mocks/angular-mocks.js',
'test/**/*spec.js'
],
preprocessors: {
'test/**/*spec.js': ['webpack', 'sourcemap']
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity,
webpack: {
devtool: 'inline-source-map',
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loader:"babel-loader"},
{ test: /\.html$/, loader: "html" }
]
},
htmlLoader: {
attrs: false
}
},
webpackMiddleware: {
noInfo: true
}
})
}