2014-03-29 21 views
5

我得到this error。這是沿着我注射器無法解決所需的依賴性的東西,但即使我有限的角度知識我很確定這個代碼不應該依賴於任何模塊。

此代碼在瀏覽器中工作得很好,但它似乎並不想在我的測試中工作。我一直在關注examples從文檔

我的角度版本是1.2.13(編輯:現在使用1.12.15)。

這裏是我的代碼:

var app = angular.module('app', []) 

.controller('GreetingCtrl', function ($scope) { 
    $scope.title = "Hello World!"; 
    $scope.message = "Test, test. One? Two?"; 
}); 

這裏是一個的失敗茉莉花測試。

describe('app controllers', function() { 
    beforeEach(module('app')); 

    describe('GreetingCtrl', function() { 
    it('should says hello world', inject(function ($controller) { 
     var $scope = {}; 
     $controller('GreetingCtrl', $scope); 
     expect($scope.title).toBe("Hello World!"); 
    })); 
    }); 
}); 

我不相信它甚至不能運行我的測試,因爲它甚至在運行之前就失敗了。我相信我已經正確地連接了正確的文件。這是我從茉莉花測試跑步者收到的錯誤。

Error: [$injector:unpr] http://errors.angularjs.org/1.2.13/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope (line 4569) (1) 

編輯:嘗試升級到1.12.15,一切都沒有改變。

回答

8

所以顯然在IRC上稍微聊了一會之後,文檔可能已經過時了,&這是一個可行的解決方案。我被連接到這個solution,&相應地糾正了我的測試。

describe('app controllers', function() { 
    var ctrl, scope; 

    beforeEach(module('app')); 

    describe('GreetingCtrl', function() { 
    beforeEach(inject(function ($rootScope, $controller) { 
     scope = $rootScope.$new(); 
     ctrl = $controller('GreetingCtrl', {$scope: scope}); 
    })); 

    it('should says hello world', function() { 
     expect(scope.title).toBe("Hello World!"); 
    }); 
    }); 
}); 

編輯:

我不小心看錯文檔最初&這裏有一個清潔的解決方案更接近文檔。

describe('app controllers', function() { 
    beforeEach(module('app')); 

    describe('GreetingCtrl', function() { 
    it('should says hello world', inject(function ($controller) { 
     var $scope = {}; 
     // this is the line that caused me pain 
     $controller('GreetingCtrl', { $scope: $scope }); 
     expect($scope.title).toBe("Hello World!"); 
    })); 
    }); 
}); 
相關問題