angular.js - angular-test的inject时总是出现[$injector:unpr] Unknown provider问题
滿天的星座
滿天的星座 2017-05-15 17:08:32
0
1
579

您好,以下是我遇到的问题:
但是在官方文档上面,它是将inject步骤提到了beforeEach,而我用官方的那种方法写,它总是会显示unknown ControllerProvider的错误,
一下是我按照官方文档写的代码:

describe('indexCtrl',function(){
        var $controller;
        beforeEach(module('controllers'));
        beforeEach(inject(function(_controller_){
            $controller = _controller_;
        }));

        describe('the add function should be right',function(){
            it('should be 34',function(){
                var $scope = {};
                var controller = $controller('indexCtrl',{$scope: $scope});
                $scope.add(14,20);
                except(controller).toBeDefined();
                except($scope.sum).toEqual(34);
            })
        })
})

一下是报的错误:

    Error: [$injector:unpr] Unknown provider: controllerProvider <- controll

er

    http://errors.angularjs.org/1.5.8/$injector/unpr?p0=controllerProvider%2

0%3C-%20controller

一下是我按照网上某个实例修改过的代码:

it('add test', inject(function ($controller) {
            var $scope = {};
            //spec body
            var indexCtrl = $controller('indexCtrl', {$scope: $scope});
            expect(indexCtrl).toBeDefined();
            expect($scope.add(2, 3)).toEqual(5);
 }));
    测试通过

不知道是什么原因,按理说官网所给出的不会有错,这两个代码只是注入的顺序不同而已。希望大家能帮帮忙

滿天的星座
滿天的星座

reply all(1)
某草草

I have the same problem. Please tell me what went wrong and how to solve it?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!