无法为 Jasmine-karma 模拟 $httpbackend whenPOST
unable to mock $httpbackend whenPOST for Jasmine-karma
我正在使用 angular js 并且我有一个 controller.js 我想为其编写 jasmine 测试用例。我可以在我的测试用例中调用控制器的功能。我的控制器中有一个本地函数,它由范围内的方法调用(我正在为其编写测试用例),它正在使用 ajax 请求调用服务方法。
我无法在我的测试用例中使用 $httpbackend
模拟这个 http post 请求
(function () {
'use strict';
angular
.module('coApp')
.controller('NewController', [
'$http',
'$q',
'$log',
'$modal',
'$scope',
'AgentDataLoader',
'$timeout',
NewController]);
function NewController ($http, $q, $log, $modal, $scope, $state, $rootScope, $filter, AgentDataLoader, $timeout) {
//Some variables declarations
//some methods
function myMethod($filter, a, b) {
console.log("inside myMethod");
if (angular.isDefined(a) && angular.isDefined(b)) {
console.log("inside if ");
console.log("a:: "+a);
console.log("b:: "+b);
dataLoader.callPOST(a, b)
.then(function (data) {
console.log("Data from service :: "+JSON.stringify(data));
/**calling some directive and methods
I am unable to execute this since I am unable to mock http Post of above mentioned dataLoader
**/
console.log("after http request");
} else {
//some other logic
}
}
$scope.methodToBeTested= function (randomData) {
console.log("selectedSystems called ****** ");
//some logic
myMethod($filter, $scope.a, $scope.b);
};
})();
我的 Jasmine 测试套件
describe('NewController',function(){
var ctrl, scope, $httpBackend, $compile, parameters;
beforeAll(function (done) {
});
beforeEach(function () {
module('MYmODULE');
module('myServiceModule');
});
beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, _$compile_) {
scope=$rootScope.$new();
$compile = _$compile_;
$httpBackend = _$httpBackend_;
ctrl=$controller('NewController',{$scope: scope});
// Start listening to xhr requests
//This data i want to send as a success of httpBackEnd Post
success.data = JsonData;
console.log('Setting request in $httpBackend');
//Not working
$httpBackend.whenPOST('/abc/efg').success(function(method, url, data) {
console.log("Getting sample data from httpBackend :: "+url);
return [200, success.data, {}];
});
//Not working
$httpBackend.whenPOST('abc/efg').respond(function(method, url, data, headers){
console.log('Received these data:', method, url, data, headers);
phones.push(angular.fromJson(data));
return [200, {}, {}];
});
$httpBackend.flush();
}));
it("Testing myMethodToBeTested of the page", function(){
scope.randomData=someRandomData;
expect(scope.methodToBeTested(scope.randomData));
//Tried this also not working
$httpBackend.expectPOST('/abc/efg').respond(success.data);
afterEach(function () {
});
});
对于 Testcase Suit,您必须模拟响应数据。我想在上面的代码中我没有看到成功变量在任何地方被初始化。因此,如果您想获得预期的响应,则必须在 Success 变量中分配预期的对象(基于上面发布的代码),例如 Ex:
var Success = {data:{SomeKey:"value"}}
现在您必须在 POST/GET 请求中传递此变量。喜欢
$httpBackend.expectPOST('/abc/efg').respond(Success.data);
希望这对您有所帮助:)
我正在使用 angular js 并且我有一个 controller.js 我想为其编写 jasmine 测试用例。我可以在我的测试用例中调用控制器的功能。我的控制器中有一个本地函数,它由范围内的方法调用(我正在为其编写测试用例),它正在使用 ajax 请求调用服务方法。
我无法在我的测试用例中使用 $httpbackend
模拟这个 http post 请求(function () {
'use strict';
angular
.module('coApp')
.controller('NewController', [
'$http',
'$q',
'$log',
'$modal',
'$scope',
'AgentDataLoader',
'$timeout',
NewController]);
function NewController ($http, $q, $log, $modal, $scope, $state, $rootScope, $filter, AgentDataLoader, $timeout) {
//Some variables declarations
//some methods
function myMethod($filter, a, b) {
console.log("inside myMethod");
if (angular.isDefined(a) && angular.isDefined(b)) {
console.log("inside if ");
console.log("a:: "+a);
console.log("b:: "+b);
dataLoader.callPOST(a, b)
.then(function (data) {
console.log("Data from service :: "+JSON.stringify(data));
/**calling some directive and methods
I am unable to execute this since I am unable to mock http Post of above mentioned dataLoader
**/
console.log("after http request");
} else {
//some other logic
}
}
$scope.methodToBeTested= function (randomData) {
console.log("selectedSystems called ****** ");
//some logic
myMethod($filter, $scope.a, $scope.b);
};
})();
我的 Jasmine 测试套件
describe('NewController',function(){
var ctrl, scope, $httpBackend, $compile, parameters;
beforeAll(function (done) {
});
beforeEach(function () {
module('MYmODULE');
module('myServiceModule');
});
beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, _$compile_) {
scope=$rootScope.$new();
$compile = _$compile_;
$httpBackend = _$httpBackend_;
ctrl=$controller('NewController',{$scope: scope});
// Start listening to xhr requests
//This data i want to send as a success of httpBackEnd Post
success.data = JsonData;
console.log('Setting request in $httpBackend');
//Not working
$httpBackend.whenPOST('/abc/efg').success(function(method, url, data) {
console.log("Getting sample data from httpBackend :: "+url);
return [200, success.data, {}];
});
//Not working
$httpBackend.whenPOST('abc/efg').respond(function(method, url, data, headers){
console.log('Received these data:', method, url, data, headers);
phones.push(angular.fromJson(data));
return [200, {}, {}];
});
$httpBackend.flush();
}));
it("Testing myMethodToBeTested of the page", function(){
scope.randomData=someRandomData;
expect(scope.methodToBeTested(scope.randomData));
//Tried this also not working
$httpBackend.expectPOST('/abc/efg').respond(success.data);
afterEach(function () {
});
});
对于 Testcase Suit,您必须模拟响应数据。我想在上面的代码中我没有看到成功变量在任何地方被初始化。因此,如果您想获得预期的响应,则必须在 Success 变量中分配预期的对象(基于上面发布的代码),例如 Ex:
var Success = {data:{SomeKey:"value"}}
现在您必须在 POST/GET 请求中传递此变量。喜欢
$httpBackend.expectPOST('/abc/efg').respond(Success.data);
希望这对您有所帮助:)