在AngularJS测试中没有调用工厂模拟

时间:2015-02-06 19:06:17

标签: ruby-on-rails angularjs coffeescript jasmine

我对AngularJS很新。我构建了一个应用程序来掌握它是如何工作的,现在我已经有了它,我正在努力学习如何测试我的代码。我的代码问题是我的工厂模拟代码失败了,我不确定我哪里出错了。这是我得到的错误:

控制器:ProfileCtrl更新测试FAILED     已经调用了预期的间谍更新。     错误:已调用间谍Profile.show已被调用。

控制器:ProfileCtrl显示测试16 FAILED     预期的间谍节目被称为。     错误:已调用间谍Profile.show已被调用。

厂:

angular.module("profileUpdate").factory "Profile", [ 
    "$resource"
    "$q"
    ($resource, $q) ->
        Profile = ->
            @service = $resource("/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}})
            return
        Profile::show = (userId) ->
            @service.get(id: userId)
        Profile::update = (updatedProfileObject) ->
            deferred = $q.defer()
            @service.update(id: updatedProfileObject.id, profile: updatedProfileObject).$promise.then ((data) ->
            deferred.resolve ("Your profile has been successfully updated!")
                ),(err) ->
                    deferred.reject ("Oops...something is wrong..try again later.")
        return deferred.promise
        return new Profile
]

控制器:

 angular.module("profileUpdate").controller "ProfileCtrl", [
    "$scope"
    "$routeParams"
    "Profile"
    "$route"
    "$rootScope"
    "$timeout"
    ($scope,$routeParams,Profile,$route,$rootScope,$timeout) -> 
        $rootScope.$on "$routeChangeSuccess", ->
            $scope.profile = Profile.show($routeParams.id)
            return
        $scope.range = (min, max, step) ->
        step = (if (step is `undefined`) then 1 else step)
        input = []
        i = min
        while i <= max
            input.push i
            i += step
        input
        $scope.update = ->
            promise = Profile.update($scope.profile)
            promise.then((success) ->
                $scope.alert = success
                $timeout ->
                    $scope.alert = false
                , 5000
            ,(err) ->
                $scope.error = err
                $timeout ->
                    $scope.error = false
                , 5000)

]

测试代码:

  "use strict";


describe('Controller: ProfileCtrl', function ($provide) {
    var ProfileCtrl;
    var $scope;
    var ProfileMock;

    //load the controller's module
    beforeEach(function(){
        ProfileMock = {
            update: function () {}
        };
        module('profileUpdate', function($provide) {
            $provide.value("Profile", ProfileMock);
        });
    })

    beforeEach(inject(function ($controller, $rootScope, Profile) {
            $scope = $rootScope.$new();
            Profile = Profile
            ProfileCtrl = $controller('ProfileCtrl', {
                $scope: $scope,
                Profile: Profile
            });
        }))




    it('should have 3 items', function() {
        var things = $scope.range(1,3,1);
        expect(things.length).toBe(3);
    });
    it('update test', function() {
    spyOn(ProfileMock, 'update');
        $scope.update
        expect(ProfileMock.update).toHaveBeenCalled();
    });
    it('show test', function() {
        spyOn(ProfileMock, 'show');

        var $routeParams = {
                id: 16 
        }
        $scope.$broadcast('$routeChangeSuccess', $routeParams.id);
        $scope.$apply()
        expect(ProfileMock.show).toHaveBeenCalled();
    });
});

1 个答案:

答案 0 :(得分:0)

您没有在ProfileMock上定义show方法,因此永远无法调用它