【问题标题】:AngularJS Tutorial test code doesn't work with my CoffeeScript codeAngularJS 教程测试代码不适用于我的 CoffeeScript 代码
【发布时间】:2014-12-16 06:45:53
【问题描述】:

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

【问题讨论】:

    标签: javascript angularjs coffeescript


    【解决方案1】:

    我从来没有使用过 coffeescript,但是.. 后面的分号似乎有问题。
    尤其是在 ctrl 任务中。
    我没有足够的声誉来添加评论,所以我回复.. 对此感到抱歉。祝你好运

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2015-02-13
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多