describe("App defination", function() {
beforeEach(module('app'));
var MainCtrl, scope;
describe("ModuleTwo", function() {
beforeEach(inject(function($controller,$rootScope){
scope= $rootScope.$new();
MainCtrl =$controller('TestCtrl',{ $scope: scope});
}));
it("check scope", function() {
expect(scope).toBeDefined();
});
});
});
我无法访问该块中的范围。它给我一个错误,如“预期未定义到定义”。当我将TestCtrl重命名为anystring时,它根本不会影响茉莉花。为什么会这样?
答案 0 :(得分:0)
它对我有用。检查您的模块是否为app
或App
。
angular.module('App', []);
angular.module('App').controller('TestCtrl', function($scope) {
});
describe("App", function() {
beforeEach(module('App'));
var MainCtrl, scope;
describe("TestCtrl", function() {
beforeEach(inject(function($controller, $rootScope){
scope = $rootScope.$new();
MainCtrl = $controller('TestCtrl', { $scope: scope });
}));
it("should have a scope", function() {
expect(scope).toBeDefined();
});
});
});

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/boot.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-mocks.js"></script>
&#13;
答案 1 :(得分:0)
您需要做两件事:
1)添加对控制器的引用以及karma.conf.js
中的angular / angular-mock:
files: [
'./node_modules/angular/angular.js', // angular
'./node_modules/angular-mocks/angular-mocks.js', // angular-mocks
'./app/views/js/controllers/sugestionController.js', // <---- your controller name
'./test/**/*.js'
],
2)在测试文件中,您必须注入模块和控制器:
beforeEach(angular.mock.module('socialMeal')); //<---- module injection
var $controller;
var sugestionController;
beforeEach(angular.mock.inject(function(_$controller_){ //<---- controller injection
$controller = _$controller_;
sugestionController = $controller('SugestionController', {});
}));