テスト内で http.post メソッドが呼び出されない理由を理解するのに苦労しています。これは、〜「フラッシュするものがありません」というメッセージと、console.log ステートメントを成功とエラーの両方の約束に追加することで示されます。メソッド。
アプリを使用する場合、API コードは機能します。これは私の最初の角度コントローラー テストであるため、単純なものが欠けている可能性があります。
David Mosher の例をテンプレートとして使用しています 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)