AngularJS: Where can I see $httpBackend responses in my browser directly?

matimo2

My background is not in web development so my terminology might be off, apologies.

In short, I have trouble figuring out where $httpBackend is serving data. This question is as much about finding a fix as it is about understanding better how $httpBackend works in general. Here more details.

I am creating a single page app with AngularJS. As seeder I'm using the AngularJS "Phone Catalog" sample project from their website, which uses npm and bower.

I will need the app to access some RESTful web services, which for the time being I want to mock. I came across ngMockE2E, which I am now using. So instead of just loading the main app.js in my index.html, I am now loading devapp.js as well, which has my app and ngMockE2E as dependencies: var devapp = angular.module('devapp', ['app', 'ngMockE2E']);.

Within devapp I mock my web service like so:

devapp.run(function($httpBackend) {
    var items = [{{"id": 1, "price": 5}, {"id": 2, "price": 10}}];

    $httpBackend.whenGET('/json/items').respond(items);

    // let all other requests through to app
    $httpBackend.whenGET(/^\w+.*/).passThrough();
});

Now when I run my app and I build services that send $http calls to /json/items, it all works fine.

But I'd like to see the output in my actual browser. For some reason I can't seem to find the URL!

My index.html kicks in when I go to localhost:8000/app/, and most addresses for my partials look like localhost:8000/app/#/login and so on. But where do I have to go to get the JSON form my fake $httpBackend?

I tried localhost:8000/json/items which gives me "Not Available", and localhost:8000/app/#/json/items or localhost:8000/app/json/items, which just sends me to my index.html with no partials loaded, and I've tried every combination I could think of.

Claies

The point of $httpBackend is to Fake the HTTP backend for Unit Testing. You aren't actually trying to Test the backend, though. The goal is to be able to test any method that uses the $http service and return the fake data instead of the real server data.

from the Angular.js Documentation:

When an Angular application needs some data from a server, it calls the $http service, which sends the request to a real server using $httpBackend service.

Essentially, the $httpBackend.whenGET() function is a hook into the $http service, which gets evaluated before any calls to your server get processed. Any time you make a call using $http, it will get sent back fake data if it matches the route rule you provided. This allows you to write your controllers as you normally would, test them, then remove the fake data stub without affecting the controller that was tested.

Put another way, the $httpBackend service doesn't handle any routes, or return any data directly, it instead changes the way that $http responds back to other modules. In order to verify that you are indeed getting back fake data, you would need to create an actual service to respond to a route which would make an $http request to that data endpoint.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Browser where I can add my own custom CSS or Javascript

분류에서Dev

Where can I see the version of Itunes that is installed?

분류에서Dev

I want to see only my URL in the URL line on a browser

분류에서Dev

How can I see the Microsoft Edge start page in another browser?

분류에서Dev

Bug with my own JavaScript library. Can't see where it is

분류에서Dev

Where can I see the name of the remote add repository and how to delete it?

분류에서Dev

Where I can find my WADL? (CXF)

분류에서Dev

How can I avoid cheat engine to see my strings at runtime

분류에서Dev

How can I see when my PC was rebooted?

분류에서Dev

How can I see the number of visitors in my website via CLI

분류에서Dev

Where can I access man pages for "aptitude" if it is not on my system?

분류에서Dev

Where can I find my Python directory in Ubuntu

분류에서Dev

Can I use the access token directly from FB.login on my server?

분류에서Dev

Can I use GMail to load my IMAP emails and still have access to it directly along with GMail?

분류에서Dev

How can I see which process hangs my Banana-pi for few seconds every couple of minutes?

분류에서Dev

Can I see who started a currently running job on my SQL Server?

분류에서Dev

Why can't I see a QXL device in my windows 10 kvm guest

분류에서Dev

How can I see a list of active Network Filters (NDIS?) on my Windows 10 PC

분류에서Dev

Where do I see the reason for the warning?

분류에서Dev

How can I have my navigation menu fill the browser window without gaps on ends?

분류에서Dev

Exported shapefile to SVG - but can't see in browser window

분류에서Dev

How can I directly inject $http into a provider?

분류에서Dev

Can I add a controller directly to a module with chaining?

분류에서Dev

Can I connect to the ethernet wall socket directly

분류에서Dev

Why can't I see gnome extensions?

분류에서Dev

I can not see the table dba_object

분류에서Dev

Can I see the number of PCI slots with a command?

분류에서Dev

Why can not I see the screen output consecutively?

분류에서Dev

How can I add code into my directive with transclude and pass in a parameter in AngularJS?

Related 관련 기사

  1. 1

    Browser where I can add my own custom CSS or Javascript

  2. 2

    Where can I see the version of Itunes that is installed?

  3. 3

    I want to see only my URL in the URL line on a browser

  4. 4

    How can I see the Microsoft Edge start page in another browser?

  5. 5

    Bug with my own JavaScript library. Can't see where it is

  6. 6

    Where can I see the name of the remote add repository and how to delete it?

  7. 7

    Where I can find my WADL? (CXF)

  8. 8

    How can I avoid cheat engine to see my strings at runtime

  9. 9

    How can I see when my PC was rebooted?

  10. 10

    How can I see the number of visitors in my website via CLI

  11. 11

    Where can I access man pages for "aptitude" if it is not on my system?

  12. 12

    Where can I find my Python directory in Ubuntu

  13. 13

    Can I use the access token directly from FB.login on my server?

  14. 14

    Can I use GMail to load my IMAP emails and still have access to it directly along with GMail?

  15. 15

    How can I see which process hangs my Banana-pi for few seconds every couple of minutes?

  16. 16

    Can I see who started a currently running job on my SQL Server?

  17. 17

    Why can't I see a QXL device in my windows 10 kvm guest

  18. 18

    How can I see a list of active Network Filters (NDIS?) on my Windows 10 PC

  19. 19

    Where do I see the reason for the warning?

  20. 20

    How can I have my navigation menu fill the browser window without gaps on ends?

  21. 21

    Exported shapefile to SVG - but can't see in browser window

  22. 22

    How can I directly inject $http into a provider?

  23. 23

    Can I add a controller directly to a module with chaining?

  24. 24

    Can I connect to the ethernet wall socket directly

  25. 25

    Why can't I see gnome extensions?

  26. 26

    I can not see the table dba_object

  27. 27

    Can I see the number of PCI slots with a command?

  28. 28

    Why can not I see the screen output consecutively?

  29. 29

    How can I add code into my directive with transclude and pass in a parameter in AngularJS?

뜨겁다태그

보관