私はしばらくの間angularJSを(テストなしで)やっていますが、きちんとやりたいです!私はそのように定義されたコントローラーを持っています
(function () {
'use strict';
angular.module('app')
.controller('CarehomeListCtrl', ['$scope', 'carehomesDataService', carehomeListCtrl]);
function carehomeListCtrl($scope, carehomesDataService) {
var vm = this;
vm.carehomeCollection = [];
vm.activate = activate;
function activate() {
vm.carehomeCollection = carehomesDataService.getAllCarehomes();
}
activate();
}
})();
それから私のスペック
describe("Carehomes tests", function () {
var $scopeConstructor, $controllerConstructor;
beforeEach(module('app'));
beforeEach(inject(function ($controller, $rootScope) {
$controllerConstructor = $controller;
$scopeConstructor = $rootScope;
}));
describe("CarehomeListCtrl", function () {
var ctrl, dataService, scope;
function createController() {
return $controllerConstructor('CarehomeListCtrl', {
$scope: scope,
carehomesDataService: dataService
});
}
beforeEach(inject(function ($injector) {
scope = $scopeConstructor.$new();
dataService =$injector.get('carehomesDataService') ;
}));
it("should have a carehomesCollection array", function () {
ctrl = createController();
expect(ctrl.carehomesCollection).not.toBeNull();
});
it("should have 3 items in carehomesCollection array when load is called", function () {
ctrl = createController();
expect(ctrl.carehomeCollection.length).toBe(3);
});
});
});
ここでの問題は、コントローラーをインスタンス化するための呼び出しが、空のオブジェクト {} または $scope : scope} にかかわらず、引数を指定して呼び出すたびにエラーで失敗することです。そのため、問題は carehomesDataService ではないことがわかります。
結果 StackTrace: エラー: [ng:areq] Argument 'CarehomeListCtrl' is not a function, got undefined http://errors.angularjs.org/1.2.26/ng/areq?p0=CarehomeListCtrl&p1=not%20a%20function%2C %20got%20undefined
ただし、このようにコントローラー$controllerConstructor('CarehomeListCtrl');
を引数なしでインスタンス化すると、インスタンス化されます。私は困惑しています!
carehomesDataService は私が作成したカスタム サービスですが、独自のテストに合格し、アプリケーションのコントローラーに正しく挿入されています。
どんな助けでも大歓迎です。
注: $scope ではなくビュー モデルとしてコントローラーのプロパティを定義することに完全には同意しませんが、私は Jesse Liberty のpluralsight コースに従っており、それが彼のやり方です...さらに、スコープの注入は現在完全に機能していません。これは迷惑です。前もって感謝します。