【问题标题】:Factory mock not being called in AngularJS testsAngularJS 测试中未调用工厂模拟
【发布时间】:2015-04-07 00:30:21
【问题描述】:

我对 AngularJS 很陌生。我构建了一个应用程序来了解它是如何工作的,现在我已经开始工作了,我正在尝试学习如何测试我的代码。我对下面代码的问题是我的工厂模拟代码失败了,我不确定我哪里出错了。这是我得到的错误:

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

控制器:ProfileCtrl 显示测试 16 失败 预计间谍节目已被调用。 错误:预期的间谍 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();
    });
});

【问题讨论】:

  • 如果您使用此代码制作了一个 jsfiddle(或类似的东西),它可能会有很大帮助。参见例如我的这个存储库 (github.com/liori/mwe-angular) 举例说明如何在 jsfiddle 中布局代码。

标签: ruby-on-rails angularjs coffeescript jasmine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多