I am tasked with writing an AngularJS app that connects to a RESTful API provided by our client. I want to wrap that API in an angular service and write tests against it using the real API. I am confused on how and where to do this.
Should I be using Protractor for this test? It doesn't involve any UX elements at all, so it doesn't feel like it should use Protractor.
Should I be using Karma? I tried Karma with $httpBackend, ngMockE2E and .passThrough() for all whenGET and whenPOST calls, but I'm having issues with Unexpected request: POST errors.
You should use Karma. Protractor is for testing interaction with web pages.
Your Unexpected request: POST errors should be resolvable if you use $httpBackend.expectPOST() for each POST request you issue.
Related
I am writing unit tests in angular js using karma. I have to mock http requests for these tests. Is there any method to make real http calls so that I can work on server response?
I'm working with a JavaScript framework (wakanda) in back-end that uses XHR requests, and with AngularJS in front-end.
I used angular-loading-bar that works perfectly and automatically with HTTP requests, but it doesn't work automatically with XHR requests.
is there any way to make it work AUTOMATICALLY XHR?
XHR vs HTTP
First XHR stands for the "XmlHTTPRequest API" ().
(see also the W3C spec version & the Mozilla documentation)
This API, introduced by Microsoft, then standardised by WHATWG & W3C,facilitated the creation of Ajax based Websites and Web Applications.
When you make an XHR request, it sends an HTTP request
$http vs HTTP
angular-loading-bar use an angularJS $http interceptor to listen all HTTP requests done using the angular $http API.
The angular $http API is using XHR to make its HTTP requests, but it does not overwrite XMLHttpRequest nor it does for its prototype. So it can not intercept HTTP requests made from code using directly XHR.
angular-wakanda HTTP requests
angula-wakanda is an angular service written on top of the Wakanda Framework data provider. It can then more easily update itself with new features that are occasionally added to the Wakanda HTTP (REST, JSON-RPC) APIs and/or to the Wakanda data provider one.
As this data provider is framework agnostic (can be used with dojo, backbone, or anything else), it does not use the angular specific $http API and its HTTP requests are then not intercepted by the angular-loading-bar
How to fix it?
Introducing low-level adapters layer for the Wakanda data-provider is something that have been discussed by the Wakanda team. It might then allow to choose which HTTP API to use in some dedicated contexts (as in nodejs where XHR is not the native HTTP API). There should be a "data provider" dedicated github repository added to the Github Wakanda Team Account in the future. In the meantime, you can send a feature request to the Angular-Wakanda Github repository (do not hesitate to mention this Stack overflow answer), and why not, also try to do a fork for a proposal ;-)
I am writing e2e test cases using protractor for my angular application. I have a service in my application called UserService which holds user information. I have an API getUser in this service to get the user data. I need to access UserService in my protractor test cases.
I looked at some of the articles and found that I could use browser.executeAsyncScript to access my services. But If I use this method , I am gettiing the injection issue.
In my application, the module is defined as:
var angApp = angular.module('myApp', ['ngCookies','ngResource']);
and all directives, services and controllers are created using angApp module.
Now I need to access my UserService so that I can call getUser API in my protractor testcases.
Could someone help me in accessing userService in my protractor testcases?
The problem that you are facing is that protractor tests are run in a separate process from the browser. Any script that runs in the browser is not directly accessible to your protractor tests. There is no way to directly transfer data between the two (the best you can do is interact through the DOM).
I'm guessing that your UserService is some kind of CRUD API for manipulating Users on the server. My recommendation would be to make REST requests directly from the protractor tests to the server in order to get the users. It will not be easy to share code between the tests and the app.
I have an app I am building, like most I have built in the last few years, with a clean separation: REST API on the back-end, Angular (before that Backbone, before that just jQuery) on the front-end.
When first testing, it is easy to stub the REST API; when developing, I can do things like a static file server (like angular-seed's ./scripts/web-server) and put in place files. But at a certain point, I really need to develop and test against the real REST API.
How do people build against and launch in a case of a separated app like this? In the past, I had a ./dev node script which would set all sorts of test/dev database variables, then require('../../server-dev-dir/app.js') but it seems sort of heavy. Is there a cleaner way to do it?
Same issue for testing: the app really needs to be tested against a true REST API, at a certain point, stubs and mocks only help so much.
In AngularJS, you make all of your HTTP requests using the $http service. It's not your job to test $http. That's the job of AngularJS team. All you care about is this:
does your back-end or 3rd party (API) back-end receive the requests that $http is sending as configured by your AngularJS code?
To that end, AngularJS provides the $httpBackend mock. When angular-mocks.js is loaded in your tests, $http will send HTTP requests to it. You can train $httpBackend on how to respond to any URL.
You can also make assertions on what requests $httpBackend received and what data it received with those requests.
I'm trying to test my backbone front-end using Jasmine against our back-end API.
Our application is built on google app engine, and our build system uses maven, so I'm using the jasmine-maven-plugin to facilitate testing the front-end integration with the API.
But, when I try to run the tests, none of the REST API calls are available (which makes sense since the server jasmine spins up is solely for jasmine testing).
Does any one know of a way to get the Jasmine plug in to use the jetty server that the "gae:run" target spins up?
Because there's a ton of authenticated calls, the server that is doing the front-end testing and the API need to be the same, including port (or it would be all cross-domain requests).
Looking at the Jasmine source code for the server here: https://github.com/searls/jasmine-maven-plugin/blob/master/src/main/java/com/github/searls/jasmine/ServerMojo.java it looks like jasmine is explicitly using the jetty server directly. I don't believe that appengine gives you access to its custom jetty driver directly but you might want to look at the gae-maven-plugin source to see how they do it. You would need to write your own implementation of the ServerMojo for jasmine and plug it in there though.