javascript - AngularJS Jasmine spyOn : number is not a function

标签 javascript angularjs testing jasmine karma-runner

我在运行使用模型进行服务调用(从 Web sql 数据库检索位置)的测试时遇到问题。

这是 Controller :

.controller('LocationDetailCtrl', function ($scope, $stateParams, LocationDbService, ProjectDbService) {

    $scope.getLocation = function () {

        LocationDbService.get($stateParams.locationId,
            //Success
            function (tx, results) {

                console.log(JSON.stringify(results.rows.item(0)));

                if (results.rows.length > 0) {
                    console.log("We are in the if statement");
                    $scope.location = {
                        id: results.rows.item(0).id,
                        name: results.rows.item(0).name,
                        address: results.rows.item(0).address,
                        lat: results.rows.item(0).latitude,
                        lng: results.rows.item(0).longitude,
                        radius: results.rows.item(0).radius
                    }
                    $scope.$apply();
                }
            },
            //Error
            function () {
                console.log("Could not retrieve the location");
            })
    }

每次测试前:

var ctrl, scope, locationDbService, projectDbService, stateparams;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(angular.mock.inject(function ($controller, $rootScope, _LocationDbService_, _ProjectDbService_) {
    // Create a new scope that's a child of the $rootScope
    scope = $rootScope.$new();

    stateparams = {locationId: 1}; //mock your stateparams object with your id

    // Create the controller
    ctrl = $controller('LocationDetailCtrl', {
        $scope: scope,
        $stateParams: stateparams,
        LocationDbService: _LocationDbService_,
        ProjectDbService: _ProjectDbService_
    });
    locationDbService = _LocationDbService_;
    projectDbService = _ProjectDbService_;
}));

测试:

 it('a location should be retrieved',
    function () {
        spyOn(locationDbService, 'get').andCallFake(function(success, fail){

            var results = [];
            results.push(
                {
                    "id": 1,
                    "name": "Jeroen",
                    "address": "Kontitch",
                    "latitude": "27.6648274",
                    "longitude": "-81.51575350000002",
                    "radius": 50
                });

            var rs = {
                //rs is a SQLResultSetobject
                insertId: 0,
                rowsAffected: 0,
                rows: {
                    //rows is a SQLResultSetRowListobject
                    length: results.length,
                    item: function(index) {
                        return results[index];
                    }
                }
            };

            success(null, rs);
        });

        expect(scope.location).toBeUndefined();
        scope.getLocation();
        expect(scope.location).toBeDefined();
    });

我得到以下测试失败:

Number is not a function

有人知道我为什么会收到这个错误吗?提前致谢!

最佳答案

我不相信你可以使用 angular.mock.inject 为你模拟你自己的服务。我总是创建一个对象,上面有所需的字段/函数,以通过 spy 模拟服务。 尽你所能注入(inject):

inject(function ($controller, $rootScope)

模拟你不能模拟的东西

stateparams = {locationId: 1};
locationDbService = jasmine.createSpyObj('LocationDbService', ['method1', 'method2']);
projectDbService= jasmine.createSpyObj('ProjectDbService', ['method3', 'method4']);

然后用这些创建你的 Controller :

ctrl = $controller('LocationDetailCtrl', {
    $scope: scope,
    $stateParams: stateparams,
    LocationDbService: locationDbService ,
    ProjectDbService: projectDbService
});

通过这种方式,您可以完全控制注入(inject) Controller 的内容,从而真正将其隔离以进行单元测试。

关于javascript - AngularJS Jasmine spyOn : number is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22980591/

相关文章:

javascript - 多次出现的数组值

javascript - Node 中的条件 app.use - Express

javascript - 在 Google Apps 脚本中解码 JWT

javascript - angularjs表单发布(使用ngUpload上传文件)和nodejs服务器的跨域问题

c# - 如何针对 WPF 编写 specflow

javascript - 看不到我使用 React-dnd 拖动的项目

AngularJS -$compileProvider.preAssignBindingsEnabled 不是函数

javascript - NG-INIT 无法正常工作

unit-testing - boost 1_48 什么bjam? bjam 错误不匹配的 Boost 版本

testing - 如何在 Go 中使用 gomock 模拟函数?