繁体   English   中英

如何在Jasmine测试中测试$ scope?

[英]How to test $scope in Jasmine test?

我试图用Jasmine为Angularjs编写单元测试。 这是我的控制器:

function HomeController($scope, fav, news, materials) {
    console.log('home controller');
    $scope.testMe = true;
}

module.controller('HomeController', HomeController);

并测试

describe('Home controller tests', function() {
    var $rootScope, $scope, controller;

    beforeEach(function() {
        module('ap.designer');

        inject(function($injector) {
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.new();
            controller = $injector.get('$controller')('HomeController', {$scope: $scope});
        });
    });

    describe('test controller functions', function() {
        it('Should return true', function() {
            expect($scope.testMe).toBe(true);
        });
     });
});

即使我试图测试expect(true).toBe(true) ,测试也失败了;

Jasmine,Karma,Angular和Angular-mocks在我的index.html中的jasmine调试页面中,脚本也带有测试。

我发现如果我删除beforeEach()块,则传递expect(true).toBe(true)

这是一个错误:

minErr/<@http://localhost:9876/base/bower_components/angular/angular.js:68:12
forEach@http://localhost:9876/base/bower_components/angular/angular.js:322:11
loadModules@http://localhost:9876/base/bower_components/angular/angular.js:4548:5
createInjector@http://localhost:9876/base/bower_components/angular/angular.js:4470:19
workFn@http://localhost:9876/base/bower_components/angular-mocks/angular-mocks.js:2954:44
angular.mock.inject@http://localhost:9876/base/bower_components/angular-mocks/angular-mocks.js:2934:35
@http://localhost:9876/base/src/js/modules/ap.designer/test/controllers/home/HomeControllerSpec.js:12:9
window.__karma__.loaded@http://localhost:9876/debug.html:42:9
@http://localhost:9876/debug.html:78:5

检查模块依赖性。 也许您的一个依赖项没有加载到karma配置文件部分,因此您的模块创建失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM