简体   繁体   中英

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

I am studying AngularJS with its tutorial and I use CoffeeScript. Below test code is from this page:

Tutorial 5 - XHRs & Dependency Injection

My CoffeeScript test code doesn't work and return error. I cannot understand why my code is wrong.

Original JS test code (works well):

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'}]);
});

My CoffeeScript test code (doesn't work well):

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' } ] )

Error log:

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)

I've never used coffeescript but.. looks like an issue with the trailing semicolons.
Particularly at the ctrl assignment.
I have not enough reputation for add a comment so I reply.. sorry about that. Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM