我無法完成官方AngularJS教程的第二步。我正在嘗試執行單元測試的說明。Angular JS官方教程 - 第2步單元測試錯誤
我不相信我已經在源代碼中改變了一件事,並且按照教程轉到了T.本教程中特定步驟的鏈接可以找到here。
這裏是我從噶得到錯誤在我的控制檯窗口:
Changed file "/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js".
Chrome 35.0.1916 (Mac OS X 10.9.2) PhoneCat controllers PhoneListCtrl should create "phones" model with 2 phones fetched from xhr FAILED
Expected [ { name : 'Nexus S', snippet : 'Fast just got faster with Nexus S.' }, { name : 'Motorola XOOM™ with Wi-Fi', snippet : 'The Next, Next Generation tablet.' }, { name : 'MOTOROLA XOOM™', snippet : 'The Next, Next Generation tablet.' } ] to equal data [ ].
Error: Expected [ { name : 'Nexus S', snippet : 'Fast just got faster with Nexus S.' }, { name : 'Motorola XOOM™ with Wi-Fi', snippet : 'The Next, Next Generation tablet.' }, { name : 'MOTOROLA XOOM™', snippet : 'The Next, Next Generation tablet.' } ] to equal data [ ].
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:31:28)
Error: No pending request to flush !
at Function.$httpBackend.flush (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:1439:34)
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:32:20)
Chrome 35.0.1916 (Mac OS X 10.9.2): Executed 1 of 5 (1 FAILED) (0 secs/0.069 sChrome 35.0.1916 (Mac OS X 10.9.2) PhoneCat controllers PhoneListCtrl should set the default value of orderProp model FAILED
Expected undefined to be 'age'.
Error: Expected undefined to be 'age'.
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:40:31)
Chrome 35.0.1916 (Mac OS X 10.9.2): Executed 2 of 5 (2 FAILED) (0 secs/0.075 sChrome 35.0.1916 (Mac OS X 10.9.2) PhoneCat controllers PhoneDetailCtrl should fetch phone detail FAILED
Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams
http://errors.angularjs.org/1.2.18/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:78:12
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3741:19
at Object.getService [as get] (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3869:39)
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3746:45
at getService (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3869:39)
at Object.invoke (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3896:13)
at workFn (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2149:20)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2134:25)
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:55:16)
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:45:3)
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:4:1
TypeError: Cannot read property 'phone' of undefined
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/controllersSpec.js:66:19)
Chrome 35.0.1916 (Mac OS X 10.9.2): Executed 3 of 5 (3 FAILED) (0 secs/0.083 sChrome 35.0.1916 (Mac OS X 10.9.2): Executed 4 of 5 (3 FAILED) (0 secs/0.086 sChrome 35.0.1916 (Mac OS X 10.9.2) service check the existence of Phone factory FAILED
Error: [$injector:unpr] Unknown provider: PhoneProvider <- Phone
http://errors.angularjs.org/1.2.18/$injector/unpr?p0=PhoneProvider%20%3C-%20Phone
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:78:12
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3741:19
at Object.getService [as get] (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3869:39)
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3746:45
at getService (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3869:39)
at Object.invoke (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular/angular.js:3896:13)
at workFn (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2149:20)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2134:25)
at null.<anonymous> (/Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/servicesSpec.js:9:46)
at /Users/user/Dropbox/personal projects/learn_angular/angular-phonecat/test/unit/servicesSpec.js:3:1
Chrome 35.0.1916 (Mac OS X 10.9.2): Executed 5 of 5 (4 FAILED) (0 secs/0.088 sChrome 35.0.1916 (Mac OS X 10.9.2): Executed 5 of 5 (4 FAILED) (0.666 secs/0.088 secs)
這裏是我的controllers.js文件:
'use strict';
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope) {
$scope.phones = [
{'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.'},
{'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.'},
{'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.'}
],
$scope.name = "Anon";
});
//phonecatApp.controller('PhoneListCtrl', PhoneListCtrl);
/* Controllers */
var phonecatControllers = angular.module('phonecatControllers', []);
phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone',
function($scope, Phone) {
$scope.phones = Phone.query();
$scope.orderProp = 'age';
}]);
phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone',
function($scope, $routeParams, Phone) {
$scope.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) {
$scope.mainImageUrl = phone.images[0];
});
$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
}
}]);
這裏的controllersSpec.js文件,我的天堂」 t甚至在教程步驟2中的說明中添加,因爲在Karma中已經爲我產生了錯誤。
'use strict';
/* jasmine specs for controllers go here */
describe('PhoneCat controllers', function() {
beforeEach(function(){
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
beforeEach(module('phonecatApp'));
beforeEach(module('phonecatServices'));
describe('PhoneListCtrl', function(){
var scope, ctrl, $httpBackend;
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {$scope: scope});
}));
it('should create "phones" model with 2 phones fetched from xhr', function() {
expect(scope.phones).toEqualData([]);
$httpBackend.flush();
expect(scope.phones).toEqualData(
[{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
});
it('should set the default value of orderProp model', function() {
expect(scope.orderProp).toBe('age');
});
});
describe('PhoneDetailCtrl', function(){
var scope, $httpBackend, ctrl,
xyzPhoneData = function() {
return {
name: 'phone xyz',
images: ['image/url1.png', 'image/url2.png']
}
};
beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData());
$routeParams.phoneId = 'xyz';
scope = $rootScope.$new();
ctrl = $controller('PhoneDetailCtrl', {$scope: scope});
}));
it('should fetch phone detail', function() {
expect(scope.phone).toEqualData({});
$httpBackend.flush();
expect(scope.phone).toEqualData(xyzPhoneData());
});
});
});
任何幫助將不勝感激 - 我已經嘗試了很多不同的東西,但不能得到它的工作。以前在SO上的線程也沒有幫助。
在此先感謝&一切順利!