如何模拟 AJAX 请求? [英] How to mock an AJAX request?
本文介绍了如何模拟 AJAX 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在端到端测试期间修改场景.js 以模拟 AJAX 请求的最简单方法是什么?
What is the simplest way to modify scenarios.js to mock an AJAX request during an end-to-end test?
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My Test AngularJS App</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script language="javascript" type="text/javascript">
angular.module('myApp', ['ngResource']);
function PlayerController($scope,$resource){
$scope.player = $resource('player.json').get();
}
</script>
</head>
<body ng-controller="PlayerController">
<p>Hello {{player.name}}</p>
</body>
</html>
从服务器上名为 player.json 的文件中正确获取播放器,并且以下测试通过.我如何将不同的 json 传递到此测试中并防止提取回服务器?
Player gets correctly fetched from a file named player.json on the server and the following test passes. How do I pass different json in to this test and prevent the fetch back to the server?
/*
How do I pass in
player.json -> {"name":"Chris"}
*/
describe('my app', function() {
beforeEach(function() {
browser().navigateTo('../../app/index.html');
});
it('should load player from player.json', function() {
expect(element('p:first').text()).
toMatch("Hello Chris");
pause();
});
});
推荐答案
你应该使用 $httpBackend从 ngMockE2E 模块模拟 http 请求.查看文档.
You should use $httpBackend from ngMockE2E module to mock http requests. Take a look at the docs.
这篇关于如何模拟 AJAX 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文