0
我很难弄清楚为什么http.post方法没有在我的测试中被调用,这是由消息说〜“什么都不冲洗”,以及添加console.log语句同时指向成功和错误承诺方法。无法通过角度控制器测试
使用应用程序时,api代码可以正常工作。这是我的第一个角度控制器测试,所以我可能会错过简单的东西。
我一直用大卫·毛思迪的例子作为模板https://github.com/davemo/lineman-angular-template/blob/master/spec/controllers/login_controller_spec.coffee
# controller
"use strict"
angular.module("woddlyApp").controller "SignupCtrl", ($scope, $http, $location) ->
$scope.user = {}
$scope.save = (user, role) ->
user.role = role
$http.post('/users', user)
.success (data, status, headers, config) ->
$location.path '/dashboard'
.error (data, status, headers, config) ->
# tests
'use strict'
describe 'Controller: SignupCtrl', ->
# load the controller's module
beforeEach -> module('woddlyApp')
# Initialize the controller and a mock scope
beforeEach inject ($controller, $rootScope, @$location, @$httpBackend) ->
@scope = $rootScope.$new()
@redirect = spyOn($location, 'path')
$controller 'SignupCtrl', { $scope: @scope, $location: $location }
afterEach ->
@$httpBackend.verifyNoOutstandingRequest()
@$httpBackend.verifyNoOutstandingExpectation()
describe 'Successful signup', ->
beforeEach ->
@$httpBackend.expectPOST('/users', @scope.user).respond(200)
@scope.save(@scope.user, 'member')
@$httpBackend.flush()
it 'redirects the user to the dashboard page', ->
expect(@redirect).toHaveBeenCalledWith '/dashboard'
错误
Chrome 28.0 (Linux) Controller: SignupCtrl Successful signup redirects the user to the dashboard page FAILED
Error: No pending request to flush !
at Error (<anonymous>)
at Function.$httpBackend.flush (/home/chris/projects/personal/woddly/client/app/components/angular-mocks/angular-mocks.js:1195:34)
at null.<anonymous> (/home/chris/projects/personal/woddly/client/.tmp/spec/controllers/signup.js:25:34)
Expected spy path to have been called with [ '/dashboard' ] but it was never called.
Error: Expected spy path to have been called with [ '/dashboard' ] but it was never called.
at null.<anonymous> (/home/chris/projects/personal/woddly/client/.tmp/spec/controllers/signup.js:30:38)
Error: Unsatisfied requests: POST /users
at Error (<anonymous>)
at Function.$httpBackend.verifyNoOutstandingExpectation (/home/chris/projects/personal/woddly/client/app/components/angular-mocks/angular-mocks.js:1228:13)
at null.<anonymous> (/home/chris/projects/personal/woddly/client/.tmp/spec/controllers/signup.js:19:32)
你试过设置断点来验证保存方法实际上被调用?检查控制台是否有错误消息? – ivarni
我没有添加断点,但是我在$ http.post调用之前和之后添加了一些console.log语句,并且在测试期间都被触发了 – chris
您是否已编译javascript?我几年没碰过CoffeeScript ......如果我正确地阅读了代码,我就像你一样困惑,但如果你有一个简单的方法将它编译为JS并将其放在pastebin或github的要点或某物上我可能会有更多的运气。 – ivarni