2014-10-09 41 views
0

我正在學習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) 

回答

0

我從來沒有使用過coffeescript,但..看起來像尾隨分號的問題。
特別是在ctrl賦值。
我沒有足夠的聲望添加評論,所以我回復..對不起。祝你好運

相關問題