【发布时间】: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