AngularJS + Jasmine模拟单元测试POST方法的$ http工厂
问题说明
如果使用post方法,如何对$ http工厂进行单元测试,例如:
How I can make unit-test of $http factory, if it using post method, for example:
// controller
$scope.logOut = function (){
logOutFactory.logOut().then(function(resp){
});
};
// service
app.factory('logOutFactory', ['$http', '$q', 'CONST', function ($http, $q, CONST){
var logoutApiUrl = CONST.URL 'logout';
return {
logOut: function() {
var deferred = $q.defer();
$http({
method: "post",
url: logoutApiUrl
})
.success(function (data) {
deferred.resolve(data);
})
.error(function (data) {
deferred.reject('error in $http request');
console.log(data, status, headers, config);
});
return deferred.promise;
}
}
}]);
// unit test
describe("myApp", function () {
beforeEach(module('app'));
describe("Ctrl", function () {
var scope, httpBackend, fakedMainResponse;
beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
httpBackend.expectPOST('https://url/logout').respond(200);
$controller('Ctrl', {
$scope: scope,
$http: $http
});
}));
it("success response - empty array from server", function () {
//httpBackend.flush();
});
});
});
我如何在茉莉花测试中模拟$ http响应??? 我正在尝试,但是我看到一个错误错误:意外请求:POST/url/logout 预计不会有更多请求 "
How i can mock $http response in Jasmine test ??? I'm trying but i see an error "Error: Unexpected request: POST /url/logout No more request expected "
正确答案
您想为$ http工厂编写一个单元测试,但是在您的测试中,里面有一个$ controller.也许您应该将此测试分为2个单元测试,一个用于控制器,一个用于工厂.
You want to write a unit-test for the $http factory, but in your test, there is a $controller inside. Maybe you should separate this test to 2 unit-tests, one for the controller, one for the factory.
在测试logOutFactory时,您应该创建一个$ httpBackend来模拟后端,以及 logOutFactory .
When you test the logOutFactory, you should create a $httpBackend for mocking the back end, and also logOutFactory.
var logOutFactory, httpBackend;
在beforeEach
中,只需初始化这两个变量:
In the beforeEach
, it just need to initialize these 2 variables :
beforeeach(inject(function($httpBackend, logOutFactory) {
httpBackend = $httpBackend;
logOutFactory = logOutFactory;
}));
然后在测试方法中模拟httpBackend:
And mock the httpBackend in the test method :
it("success response - empty array from server", function () {
httpBackend.expectPOST('https://url/logout').respond(200);
var success;
// call logOut
logOutFactory.logOut().then(function () {
success = true;
});
httpBackend.flush();
// verification
expect(succeeded).to.be.true;
});
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhchbbfe
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20