测试具有父控制器中定义的方法的子控制器时出现问题

时间:2017-07-12 09:47:03

标签: karma-jasmine

我正在尝试测试一个简单的控制器变量,但我的控制器有一个调用父控制器并显示此错误的方法。如何解决此错误。 这是测试文件

describe('SigninController', function () {
    beforeEach(module("app.website"));
    var controller,scope,parentscope;
    var $scope,$controller,$parentScope,$parentcontroller,$mdToast;
    window.ga = function() {};

    beforeEach(module("ngMaterial"));
    beforeEach(module("ngAnimate"));
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('SigninController',{$scope : scope});
    }));

    describe('simple program', function () {
        it('should say true equals true', function () {
            expect(scope.isShowProgress).toEqual(false);
        });
    });
});

TypeError:$ scope.setMetaInfo不是函数         在init(public_html / components / myaccount / signin / controllers / signin_controller.js:77:20)         在new(public_html / components / myaccount / signin / controllers / signin_controller.js:65:9)         在Object.instantiate(public_html / bower_components / angular / angular.js:5055:14)         在$ controller(public_html / bower_components / angular / angular.js:10975:28)         at public_html / bower_components / angular-mocks / angular-mocks.js:2308:14         在对象。 (单位%20Tests / demotest.js:17:22)         在Object.invoke(public_html / bower_components / angular / angular.js:5040:19)         at Object.WorkFn(public_html / bower_components / angular-mocks / angular-mocks.js:3182:20)     错误:声明位置         在window.inject.angular.mock.inject(public_html / bower_components / angular-mocks / angular-mocks.js:3145:25)         在套房。 (单位%20Tests / demotest.js:15:16)         在单位%20Tests / demotest.js:7:1 Chrome 59.0.3071(Mac OS X 10.11.6):执行1 of 1(1 FAILED)(0秒/0.204秒) Chrome 59.0.3071(Mac OS X 10.11.6):执行1 of 1(1 FAILED)ERROR(0.221秒/0.204秒)

1 个答案:

答案 0 :(得分:0)

var MainTodayCtrl,
    rootScope,
    scope;

// Initialize the controller and a mock scope
beforeEach(inject(function($controller, $rootScope) {
    rootScope = $rootScope.$new(); // root for parent controller
    $controller('MainCtrl', { $scope: rootScope });

    // then your MainCtrl will alter that scope
    // for the child controller you need a "derived" scope
    scope = rootScope.$new();

    // now business as usua
    MainTodayCtrl = $controller('MainTodayCtrl', {
        $scope: scope
        // place here mocked dependencies
    });
}));