Javascript AngularJS模块NgMockE2E';s passThrough()方法没有';行不通
我在angular js中有一个sails,node js应用程序,我决定对它进行一些测试,特别是在后端部分,我正在使用Jasmine和ngMockE2E工具,因为我想用一些真实的服务器端数据来测试它 下面是我要测试的代码的一部分:Javascript AngularJS模块NgMockE2E';s passThrough()方法没有';行不通,javascript,angularjs,http,jasmine,ngmock,Javascript,Angularjs,Http,Jasmine,Ngmock,我在angular js中有一个sails,node js应用程序,我决定对它进行一些测试,特别是在后端部分,我正在使用Jasmine和ngMockE2E工具,因为我想用一些真实的服务器端数据来测试它 下面是我要测试的代码的一部分: app.controller('IdentificationCtrl', function($scope, $rootScope, ... , ajax) { _initController = function() { $scope.lo
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
}
})
下面是我加载Jasmine、ngmock的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测试文件时,它不会进行任何http调用。似乎passThrough()函数没有正确执行其工作
我面对并纠正了没有定义passThrough()函数的问题,这是因为我没有加载ngMockE2E模块(而不是ngMock)。但这一次,茉莉花很好地发挥了作用,错误仅仅在于规范是错误的:
Error: Expected null not to be null.
抱歉,如果此问题已经解决,我在任何地方都找不到解决方案。在github上对此进行了详细讨论
Error: Expected null not to be null.