3

我在angular-js中有一个sailsnode-js应用程序,我决定对它进行一些测试,特别是在后端部分,为此我使用JasminengMockE2E工具,因为我想用一些真实的服务器端测试它数据。

这是我要测试的代码的一部分:

app.controller('IdentificationCtrl', function($scope, $rootScope, ... , ajax) {

    _initController = function() {
        $scope.loginData = {};
    };

    $scope.doLogin = function(form) {
        if (form.$valid) {
            ajax.sendApiRequest($scope.loginData, "POST", "session/login").then(
                function(response) {
                    //$state.go('app.dashboard');
                    window.localStorage.setItem("sesion", JSON.stringify(response.data));
                    $rootScope.userTest = response.data;
                },
                (function(error) {
                    console.log("error")
                })
            );
        }
    };

    _initController();

});

这是我的service.js文件,我在其中提供了 ajax 服务:

angular.module('common.services', [])
.service('ajax', function($http, $rootScope) {

    if (window.location.hostname == "localhost") {
        var URL = "http://localhost:1349/";
    } else {
        var URL = "http://TheRealURL/";
    }

    this.sendApiRequest = function(data, type, method) {
        $rootScope.$broadcast('loading:show')
        if (method == "session/login" || method == "session/signup") {
            var authorization = "";
        } else {
            var authorization = JSON.parse(window.localStorage["sesion"]).id;
        }

        data_ajax = {
            url: URL + method,
            method: type,
            headers: {
                'Content-Type': 'text/plain',
                'authorization': authorization
            }
        }

        if (type === "GET" || type != "delete") {
            data_ajax.params = data;
        } else {
            data_ajax.data = data;
        }

        if (window.localStorage['admin-language']) {
            data_ajax.headers['accept-language'] = window.localStorage['admin-language'];
        } else {
            data_ajax.headers['accept-language'] = window.navigator.language.toUpperCase();
        }

        //The test arrives here perfectly
        return $http(data_ajax).success(function(data, status, headers, config) {
            //But does not enter here 
            return data;
            $rootScope.$broadcast('loading:hide')
        }).error(function(data, status, headers, config) {
            //Nor here
            return data;
            $rootScope.$broadcast('loading:hide')
        });
        //And finally achieves this point, but without making the http call
    }
})

这是我加载JasminengMocks和测试文件的 html:

...
<!-- Testing files -->
<link rel="stylesheet" type="text/css" href="lib/jasmine-core/jasmine.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<script type="text/javascript" src="lib/angular-mocks/angular-mocks.js"></script>
<script src="js/TestBackend.js"></script>
...

这是上面引用的testBackend.js文件,我打算在其中进行测试:

describe('FirstCycleController', function() {

beforeEach(module('myApp'));
beforeEach(module('ngMockE2E'));

var $controller;
var $rootScope;
var $httpBackend;

beforeEach(inject(function(_$controller_, _$rootScope_, _$httpBackend_) {

    $controller = _$controller_;
    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;
}));
    describe('User login', function() {
        it('verifys that a user is correctly logged.', inject(function() {
            var $identificationScope = {};
            var identificationController = $controller('IdentificationCtrl', { $scope: $identificationScope });

            var form = {
                $valid: true
            }

            $identificationScope.loginData = {
                email: 'user@test.com',
                password: 'usertest'
            };

             $rootScope.userTest = null;

            //pass through everything
            $httpBackend.whenGET(/^\w+.*/).passThrough();
            $httpBackend.whenPOST(/^\w+.*/).passThrough();

            //call the login function simulating a login
            $identificationScope.doLogin({ $valid: true });

            setTimeout(function() {

                expect($rootScope.userTest).not.toBe(null);

            }, 150);
        }));
    });
});

问题是在运行testBackend.js测试文件时,它没有进行任何http 调用。似乎passThrough()函数没有正确完成他的工作。

我遇到并纠正了没有定义passThrough()函数的问题,这是因为我没有加载ngMockE2E模块(而不是ngMock)。但是这次Jasmine工作正常,错误只是规范是错误的:

Error: Expected null not to be null.

抱歉,如果此问题已经解决,我无法在任何地方找到解决方案。

4

1 回答 1

0

关于 angular github issue对此有详细的讨论

于 2016-12-28T14:16:39.727 回答