Angular JS官方教程 - 第2步单元测试错误

时间:2014-06-21 02:48:41

标签: javascript angularjs testing

我无法完成官方AngularJS教程的第二步。我正在尝试执行单元测试的说明。

我不相信我在源代码中更改了一个内容并按照教程进行了操作。可以找到教程中特定步骤的链接here

这是我在控制台窗口中从Karma获得的错误:

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文件,我甚至没有在教程第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上的先前线程也没有帮助。

提前致谢&amp;一切顺利!

1 个答案:

答案 0 :(得分:1)

controller.jscontrollersSpec.js的版本可能是step 4或更高版本。

以下代码在步骤4中添加。

$scope.orderProp = 'age';

https://docs.angularjs.org/tutorial/step_04

https://github.com/angular/angular-phonecat/compare/step-3...step-4


我想,你需要再次Workspace Reset Instructions。如下:

git checkout -f step-2