AngularJS教程测试代码没有'我无法使用CoffeeScript代码

AngularJS Tutorial test code doesn't work with my CoffeeScript code

本文关键字:代码 CoffeeScript 测试 教程 AngularJS      更新时间:2023-09-26

我正在学习AngularJS及其教程,并使用CoffeeScript。以下测试代码来自此页面:

教程5-XHR&依赖注入

我的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分配时
我没有足够的声誉添加评论,所以我回复。。很抱歉。祝好运