Ich versuche, eine Einheit Tests für Winkel-Controller in Kaffee-Skript geschrieben zu schreiben, und ich habe Probleme zu verstehen, warum einige Tests passieren, auch wenn sie nicht sollten und warum einige nicht sind.Jasmine - Wie spioniert man einen Funktionsaufruf innerhalb einer Funktion und wieder innerhalb einer Funktion auf dem Controller?
Dies ist mein Controller:
add_user_controller.coffee
(->
class AddUserController
@$inject = ['$scope', '$http', '$state', 'OfficesService', 'UserFactory']
constructor: (@$scope, @$http, @$state, @OfficesService, UserFactory) ->
@offices = @OfficesService
@user = new UserFactory()
save: ->
self = this
@user.save().then ->
self.$state.go('app.main.users.list')
angular
.module('app')
.controller('AddUserController', AddUserController)
)()
Das sind meine Tests:
add_user_controller.test.coffee
'use strict'
describe 'Controller: AddUserController', ->
AddUserController = null
scope = {}
http = {};
state = {
go: (value) ->
bar = value
}
OfficesService = {};
UserFactory = null
q = null
# Initialize the controller and scope
beforeEach ->
# Load the main module
module 'app'
inject ($rootScope, $controller, _UserFactory_, $q) ->
UserFactory = _UserFactory_
q = $q
scope = $rootScope.$new
AddUserController = $controller 'AddUserController',
$scope: scope,
$http: http,
$state: state,
OfficesService: OfficesService,
UserFactory: UserFactory
it 'should call "save" on "user"', ->
deferred = q.defer()
spyOn(AddUserController.user, "save").and.returnValue deferred.promise
AddUserController.save()
expect(AddUserController.user.save).toHaveBeenCalled
it 'should move to the "app.main.users.list" after "save" method has been called', ->
spyOn(AddUserController.$state, "go")
AddUserController.save()
expect(AddUserController.$state.go).toHaveBeenCalledWith('app.main.users.list')
erstes Problem
-Test 'should call "save" on "user"'
ist vorbei, auch wenn ich folgende Zeilen in Controller Kommentar:
# @user.save().then ->
# self.$state.go('app.main.users.list')
zweites Problem
-Test 'should move to the "app.main.users.list" after "save" method has been called'
versagt mit:
Erwarteten Spion gehen zu haben wurde mit ['app.main.users.list'] aufgerufen, aber wurde nie aufgerufen.