使用$ httpBackend测试角度服务时,我得到了一些意想不到的结果.
当我使用httpBackend.expectGET运行测试时,测试按预期工作.但是,如果我使用whenGET运行完全相同的测试,则测试失败,并显示消息"No pending request to flush".
这是受测试的服务:
.factory('CoverageService', ['$http', function ($http) {
return{
GetCoverageReport: function () {
return $http.get('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml');
},
Run Code Online (Sandbox Code Playgroud)
以下是测试:
'use strict';
describe('coverage-manager.js', function () {
describe('CoverageService', function () {
var httpBackend, sut, rootScope;
beforeEach(module('fot'));
describe('GetCoverageReport', function () {
beforeEach(inject(function ($httpBackend, CoverageService, $rootScope) {
httpBackend = $httpBackend;
sut = CoverageService;
rootScope = $rootScope;
}));
it('should return data', function () {
var expectedData = { testData: 'some data' };
var actualData;
httpBackend.expectGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);
sut.GetCoverageReport()
.success(function (response) { actualData …Run Code Online (Sandbox Code Playgroud)我正在测试轮询资源的序列,直到满足条件.
Book = $resource("/books/:id", {id: "@id"});
function poll(success) {
Book.get({id:1}, function() {
if (canStop) {
success();
} else {
$timeout(poll, 1000);
}
});
};
Run Code Online (Sandbox Code Playgroud)
以下测试失败了 Error: Unsatisfied requests: GET /user_workshops/1
describe('poll', function() {
beforeEach(function() {
$httpBackend.expectGET('/books/1').respond(200,{id:1});
$httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1});
poll(function() {
successCalled = true;
});
$httpBackend.flush();
$timeout.flush();
canStop=true;
$httpBackend.flush();
});
it('should call success when canStop is true', function() {
expect(successCalled).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试重新排列测试顺序,将第二个放在第二个expectGET之前,httpBackend.flush()但后来我得到:
Error: Unexpected request: POST /books/1
No more request expected
Run Code Online (Sandbox Code Playgroud) 我正在尝试自动化角度4应用程序。(量角器,黄瓜,打字稿)。坚持模拟HTTP请求。
找到几种方法:
1)httpBackend 2)http-backend-proxy
但是他们所有人都在谈论AngularJs的应用。根据我的理解,他们已经使用ngMockE2E实现了该功能,而我在angular2 + api doc中找不到。
我也试着跟随这个,没有运气。
谁能在我的量角器项目中帮助我在哪里引用以模拟HTTP调用(GET,POST,PUT等)。
提前致谢。
@Ram Pasala,@ vict:您有任何评论!
我有一个书签应用程序,它接收一个网址并自动提取摘要.当客户端请求服务器添加新书签时,服务器会发回一些初始信息并启动提取摘要的过程.
在角度前端,我创建了添加书签和管理书签列表中每个项目的指令.在listitem指令中,有一个checkSummary()方法将轮询服务器以获取摘要.
我对后一个指令的单元测试有问题.它失败了"不满意的请求"但是当我在各个点登录到控制台时,$http.get()请求似乎触发了,我可以看到$ scope变量已更新,所以我真的不明白为什么它会因此而失败.我已经检查了许多不同问题的答案,但实际上找不到能给出一些见解的东西.
代码如下:
bookmarks.js:
angular.module( 'userpages.bookmarks', [
'ui.router',
'ui.bootstrap',
'ui.validate'
])
.config(function config( $stateProvider ) {
$stateProvider.state( 'userpages.bookmarks', {
url: '/bookmarks',
templateUrl: 'userpages/bookmarks/bookmarks.tpl.html',
controller: 'BookmarksController',
data: { pageTitle: 'Bookmarks' },
});
})
.factory('bookmarksApiResource', ['$http', function ($http) {
var bookmarksUrl = '/api/v1/bookmarks/';
var api = {
getList: function() {
return $http.get( bookmarksUrl )
.then( function(response) { return response.data; });
},
getById: function(id) {
return $http.get( bookmarksUrl + id + '/' )
.then( function(response) { return …Run Code Online (Sandbox Code Playgroud) 我有一个API,响应状态202没有数据,但响应有一个标题"位置",指向一个URL.
我查看了$ httpBackend的响应(...)文档,并没有看到如何在响应中模拟标头.
我猜测它可能是这样的:
var expectedUrl = 'http://...';
var responseConfig = {
headers: {
location: 'http://...'
}
};
$httpBackend.when(expectedUrl).respond(202, '', responseConfig);
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我得到预期状态202,但标题('location')返回undefined.
建议?
我正在尝试为控制器编写单元测试,该控制器使用$ http服务获取文章详细信息.
控制器:
.controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){
$scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId;
$http.get($scope.url).then(function(response) {
//console.log(response.data);
$scope.heading = response.data.Headline;
$scope.rawBody = response.data.Body;
$scope.body = $sce.trustAsHtml($scope.rawBody);
$scope.image = response.data.Assets[0].URL;
});
});
Run Code Online (Sandbox Code Playgroud)
单元测试:
'use strict';
describe('Controller: Article', function () {
var scope,
$httpBackend,
articleEndpoint;
// load the controller's module
beforeEach(module('myApp'));
describe('ArticleDetailCtrl', function () {
var ArticleDetailCtrl,
jsonObject,
ArticleId = '123';
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, Article, API_URL, ARTICLE_URL) { …Run Code Online (Sandbox Code Playgroud) 我有一个触发HTTP请求的服务.此请求使用$ rootScope.config对象(存储了我的基本URL),但出于某种原因,当我使用$ httpBackend时,$ rootScope未正确加载.
服务:
myAppServices.factory('Auth', function($rootScope, $http, $cookieStore){
return {
logout: function(success, error) {
$http.get($rootScope.config.apiUrl + '/users/logout').then(function(response){
}, function(response) {
});
}
Run Code Online (Sandbox Code Playgroud)
测试:
describe('services', function() {
var Auth;
var $httpBackend;
var $cookieStore;
var $rootScope;
beforeEach(function() {
module('myApp.services');
});
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$cookieStore = $injector.get('$cookieStore');
$rootScope = $injector.get('$rootScope');
Helper = $injector.get('Helper');
Auth = $injector.get('Auth');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('logout', function() {
it('should make a request and invoke callback', function() {
// I try to set $rootScope …Run Code Online (Sandbox Code Playgroud) httpbackend ×7
angularjs ×5
unit-testing ×4
jasmine ×2
angular ×1
http ×1
http-proxy ×1
karma-runner ×1
mocking ×1
protractor ×1
typescript ×1