これは私のコントローラです。
(function() {
'use strict';
app.controller('NavController', navController);
function navController($rootScope, $scope, $location, $window, NavFactory) {
$scope.menuConfig = null;
$scope.menuItems = [];
$scope.isActive = isActive;
$scope.toggleBurgerMenu = toggleBurgerMenu;
$scope.toggleDeviceSettings = toggleDeviceSettings;
$rootScope.$watch('dictionary', function(dictionary) {
if (dictionary) {
init();
}
});
function init() {
$scope.menuConfig = $rootScope.config.navigation;
NavFactory.GetMenuItems().then(onGetMenuItems, $scope.onAPIError);
}
function onGetMenuItems(response) {
$scope.moduleConfig = response.moduleConfig;
$scope.menuItems = response.menuItems;
}
}
})();
これは私のテストスイート(カルマ、ジャスミン) です( 'からNavController機能'、機能(){
var $rootScope, $scope, $location, $window, $controller, createController, NavFactory, CacheFactory, toastr;
beforeEach(module('mockedDashboard'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$location_, _$window_, _NavFactory_, _toastr_) {
$location = _$location_;
$window = _$window_;
toastr = _toastr_;
$rootScope = _$rootScope_;
$rootScope.dictionary = jsDictionary;
$rootScope.config = jsConfig;
$rootScope.config.contentFolder = '_default';
$scope = _$rootScope_.$new();
$scope.burgerMenuActive = false;
//mock the parent controller function
$scope.navigate = function(path) {
$location.path('/' + path);
};
// end mock
createController = function() {
return _$controller_('NavController', {
'$scope': $scope
});
};
$controller = createController();
}));
// We are using CacheFactory in this project, when running multiple tests on the controller
// we need to destroy the cache for each test as the controller is initialized for each test.
afterEach(inject(function(_CacheFactory_) {
CacheFactory = _CacheFactory_;
CacheFactory.destroy('defaultCache');
}));
describe('init()', function() {
it('should call NavFactory.GetMenuItems and set moduleConfig and menuItems on $scope', inject(function(_$httpBackend_) {
var $httpBackend = _$httpBackend_;
expect($scope.moduleConfig).toBe(undefined);
expect($scope.menuItems.length).toBe(0);
var responseData = [{
"path": "stats",
"url": "./app/modules/dashboard/modules/score/score.index.html",
"icon": "fa fa-fw fa-pie-chart",
"dashboardEnabled": true,
"burgerMenu": true,
"barMenu": false,
"barMenuOrder": -1,
"bottomMenu": true,
"bottomMenuOrder": 3,
"order_sm": 1,
"order_md": 2,
"order_lg": 2
}];
$httpBackend.expectGET('../content/_default/config/modules.json').respond(responseData);
$httpBackend.flush();
expect($scope.moduleConfig).not.toBe(undefined);
expect($scope.menuItems.length > 0).toBe(true);
}));
});});
私は$スコープを実行する必要があることを期待して説明します。$ダイジェスト( )ウォッチャーを起動してinit()を実行させるには、init()を実行しますが、そのまま実行され、テストは成功します。
これは問題ではありません。ウォッチャーがどのように発砲しているのか理解できないのですが、何が起こるのですか?
'$ rootScope.dictionary = jsDictionary;の' jsDictionary'はどこから来ますか? – Jagrut
通常、ブートストラップのjsonファイルから来ますが、テスト目的のために、テストファイルをロードしたグローバルなjavascriptオブジェクトです。それは汚いですが、私は私/私たちのアプリを作った他の間違いを解決することを学んでいる間に動作します。すべてのユニットテストはビルド後に行われます。私たちはあまりにも多くのことを学んでおり、将来私たちはTDDアプローチを取っていきます。 – Stuart