0

I am having a hard time figuring out why http.post method is not being called within my tests, which is indicated by the message saying ~ "nothing to flush" as well as adding console.log statements to both the success and the error promise methods.

The api code does work when using the app. This is my first angular controller test so I could be missing something simple.

I have been using David Mosher's example as a template 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'

Error

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)
4

1 回答 1

0

正如评论中提到的,我正在使用 angular 1.1.5(用于新的 ng-animate)。为了让我的测试通过,我需要安装 angular-mocks 1.1.5。

bower install angular-mocks-unstable

或更新 bower.js(或 components.js)文件并运行:

bower install

并更新我的 karma.conf.js 文件

files = [
  JASMINE,
  JASMINE_ADAPTER,
  'app/components/angular-unstable/angular.js',
  'app/components/angular-mocks-unstable/angular-mocks.js',   // need unstable here
  '.tmp/scripts/*.js',
  '.tmp/scripts/**/*.js',
  '.tmp/spec/**/*.js'
];
于 2013-07-23T12:55:24.110 回答