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

问题描述:

我正在学习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) 

我从来没有使用过coffeescript,但..看起来像尾随分号的问题。
特别是在ctrl赋值。
我没有足够的声望添加评论,所以我回复..对不起。祝你好运