如何从回报角度的$ httpBackend文件内容? [英] How to return a file content from angular's $httpBackend?

查看:152
本文介绍了如何从回报角度的$ httpBackend文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个端到端的测试套件中的角,并需要返回使用$ httpBackend罐头回应。这将是很好,如果我可以只返回一个文件内容,例如

I'm trying to setup an e2e test suite in angular, and need to return canned responses using $httpBackend. It would be nice if I could just return a file content, e.g.

  $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
    return getContentOf("/somefile");
  });

我试着使用$ HTTP,沿东西线

I tried to use $http, something along the lines of

  $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
    return $http.get("/responses/phones.js");
  });

但它没有工作,你猜角不支持从$返回承诺httpBackend?

but it didn't work, guess angular doesn't support returning promises from $httpBackend ?

我能做到这一点的方法之一是引用的JS与应用程序加载的响应文件,并指定文件的内容到变量,但它会好得多才能够按需加载数据。

One way I could do it is to reference js files with responses on app load, and assign file's content to variables, but it would be much nicer to be able to load data on demand.

推荐答案

由于$ httpBackend不退还承诺工作,你能做到这一点的方法之一是同步您的数据。 $ HTTP没有开箱的同步选项,因此你必须拨打电话到您的文件没有它,像这样:

Since $httpBackend doesn't work with returned promises, one way you can do this is to get your data synchronously. $http doesn't have a synchronous option out of the box, so you would have to make the call to your file without it, like so:

$httpBackend.whenPOST('/phones').respond(function(method, url, data) {
  var request = new XMLHttpRequest();

  request.open('GET', '/responses/phones.js', false);
  request.send(null);

  return [request.status, request.response, {}];
});

这篇关于如何从回报角度的$ httpBackend文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆