AngularJS Tutorial测试代码不适用于我的CoffeeScript代码

时间:2014-10-09 07:27:48

标签: javascript angularjs coffeescript

我正在学习AngularJS及其教程,我使用的是CoffeeScript。测试代码如下:

Tutorial 5 - XHRs & Dependency Injection

我的CoffeeScript测试代码无法正常工作并返回错误。我无法理解为什么我的代码错了。

原始JS测试代码(效果很好):

describe('PhoneCat controllers', function() {

describe('PhoneListCtrl', function(){
  var scope, ctrl, $httpBackend;

  beforeEach(module('phonecatApp'));

  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).toBeUndefined();
  $httpBackend.flush();

  expect(scope.phones).toEqual([{name: 'Nexus S'},
                               {name: 'Motorola DROID'}]);
});

我的CoffeeScript测试代码(效果不佳):

describe 'PhoneCat controllers', ->

  describe 'PhoneListCtrl', ->
    scope = null
    ctrl = null
    $httpBackend = null

    beforeEach module 'phonecatApp'

    beforeEach inject ( _$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', ->
      expect( scope.phones ).toBeUndefined();
      $httpBackend.flush;

      expect( scope.phones ).toEqual( [ { name: 'Nexus S' }, { name: 'Motorola DROID' } ] )

错误日志:

Chrome 37.0.2062 (Mac OS X 10.9.5) PhoneCat controllers PhoneListCtrl should create "phones" model with 2 phones fetched from xhr FAILED
    Expected undefined to equal [ { name : 'Nexus S' }, { name : 'Motorola DROID' } ].
    Error: Expected undefined to equal [ { name : 'Nexus S' }, { name : 'Motorola DROID' } ].
        at null.<anonymous> (/Users/weed/tmp/angular-phonecat_140814/test/unit/controllersSpec.js:27:37)
Chrome 37.0.2062 (Mac OS X 10.9.5): Executed 1 of 1 (1 FAILED) ERROR (0.027 secs / 0.022 secs)

1 个答案:

答案 0 :(得分:0)

我从未使用过coffeescript,但..看起来像是一个带有分号的问题。
特别是在 ctrl 分配时 我没有足够的声誉来添加评论所以我回复..抱歉。祝你好运