Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angularjs - Angular Unit Test Unknown provider: $scopeProvider

Hello I am writing my first angular test with Jasmine but I keep getting the error

------ Test started: File: C:UsersReganDocumentsVisual Studio 2013WebSitesReganestAppTestProject g-testsMainCtrlSpec.js ------ Test 'MainCtrl with inline mock:should have lables' failed Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- MainCtrl

I have tried playing around with it but am stuck. If you see the problem please let me know. If you need more code as well please let me know but I think the problem is in these two files.

MainCtrlSvc.js

/// <reference path="../../Scripts/angular/angular.js" />
/// <reference path="../../Scripts/angular/angular-mocks.js" />
/// <reference path="../../Scripts/chartjs/Chart.js" />
/// <reference path="../../Scripts/angular-chart.js-master/dist/angular-chart.js" />
/// <reference path="../../Scripts/controller/main-controller.js" />
/// <reference path="../../Scripts/service/data-service.js" />
/// <reference path="../../libs/jasmine/jasmine.js" />

describe("MainCtrl with inline mock", function () {
    beforeEach(module("ChartApp"));

    var ctrl, mockDataSrv;

    beforeEach(module(function($provide) {
        mockDataSrv = {
            labels: ["Reading", "Coding", "Thinking About Coding", "Reddit", "StackOverflow"],
            data: [500, 300, 300, 40, 220],
            type: "PolarArea",
            title: "Angular Chart Expriment"
        };
        $provide.value("DataSrv", mockDataSrv);
    }));

    beforeEach(inject(function ($controller) {
        ctrl = $controller("MainCtrl");
    }));

    it("should have lables", function () {
        expect(scope.labels).toBeDefined();
    });
});

MainCtrl.js

var app = angular.module("ChartApp", ["chart.js"]);

    app.controller("MainCtrl", ["$scope",
      function ($scope, DataSrv) {
          $scope.labels = DataSrv.labels;
          $scope.data = DataSrv.data;
          $scope.type = DataSrv.type;
          $scope.title = DataSrv.title;
      }
    ]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since there is no $scope service, $controller provider cannot instantiate the injected $scope argument.

You need to provide the scope while instantiating a controller using the $controller provider. You can inject $rootScope in your setUp and you can get a child scope by doing $rootScope.$new(). Inject it as an argument to the $controller contstructor. i.e $controller("MainCtrl", {$scope:scope }) where scope is the new child scope of even you can pass in the $rootScope.

i.e

   var ctrl, mockDataSrv, scope;
    //... Your code
    //...
    beforeEach(inject(function ($controller, $rootScope) {
        scope = $rootScope.$new(); //get a childscope
        ctrl = $controller("MainCtrl", {$scope:scope }); //Pass it as argument as $scope's value
    }));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...