How do I mock azure mobile services in AngularJs? - angularjs

I'm writing an AngularJs front end to an Azure Mobile Services backend and scratching my head wondering the 'angular way' to mock the backend in my tests.
Initial thoughts are that I could create a lookalike service that simply returns expected values but I'm used to using a mocking framework like Moq in my C sharp work to ease the burden.
Any suggestions on how I could accomplish this?

Are you accesing this services using $http or resource?
Two options:
- You can use a mocking framework like Sinon (we have combined it with jasmine for an angularjs based app, mocking $http request).
http://sinonjs.org/docs/
http://www.youtube.com/watch?v=qK-Z0oEdE4Y
http://robdodson.me/blog/2012/05/28/mocking-requests-with-mocha-chai-and-sinon/
- You can use angularmocks and for some given url's return harcoded values.

Related

How to best encapsulate Google JS API on AngularJS app

I want to use the Google JS API on my AngularJS web app. As I'm a newbie using Angular I'm a bit confused on how to encapsulate the gAPI calls. Basically I want to authenticate users and call some Google APIs (eg: spreadsheets, calendar, email, etc).
Considering a basic AngularJS app structure (main module, controllers, services, providers, etc), I decided to create a provider in order to encapsulate the Google oAuth authentication flow. And then I created some services to make the services' API call (calendar, docs, etc). Here I stated to get lost... :-\
Some questions:
How can I control (verify if the user is already authenticated) the access to my views? In each service or view controller? Or maybe on $routeProvider config?
In terms of Angular service/provider design... Is better to expose all the gAPI functionality I want to use in a Angular Service/Provider or simply access the gapi object directly in each angular controller/service?
Maybe consider using a library like https://github.com/maximepvrt/angular-google-gapi. These types of questions are hard to answer without knowing the scope of your application.
In general, I'd consider how much of the GAPI you are using and how much abstraction of the GAPI you will need. If you are doing fairly light weight things, then it may be fine to call the GAPI directly.
If you think you will be reusing the GAPI throughout the application, consider using the linked library or even wrapping the GAPI with your own services and providers.

Using angularjs in nodejs server-side

I am fairly new to nodejs, just been playing around with it for a while.
Now I would like to use angularjs $compile on server-side. In the end I would like to have an API which receives html and json and compiles it together using angulars syntax.
To go in a little more detail, this is what it looks like in angular:
plunkr
$compile(element.contents())(scope);
As you can see the angular function $compile is used to fill a template from a textarea.
I would like to use this $compile function on server-side as a webservice.
tl;dr;:
I would like to use angularjs as a templating engine for express/nodejs on server-side.
I am having troubles integrating angular. Do you think this is a liable approach at all?
Nope, this is not liable, you are drastically over-thinking your architecture, just create your angular.js application, render it using a standard node.js template engine as jade or ejs. Then in your angular app connect to an api provided by node.js and express, and let angular do it's job and node.js its job.
Each tool is used for his main purpose, angular is used for single-page-application that are communicating with the server side using ($http, $resource, sockets etc...). Node.js is a scripting language mostly used for server side, api creation, other operations.
My advice keep the job simple, playful bu simple, you will sleep better at nights.

Strongloop AngularJS reflection services

I've a Java developer who's begun working in the Javascript enterprise domain. Specifically I've begun working with Strongloop/Loopback API and AngularJS in the client.
I'm creating entities with ease with Strongloop and am very impressed with how quickly I can generate an expanded model. When it comes to the client however things appear to slow down as I have to manually create the Angular Controllers, Services and the crud template views.
I've used reflection in Java considerably in the past and I found it to be very effective. I was hoping someone might be able to let me know if there's either a reflective way to initialise Controllers so that I could have one EntityController if you like that would export the CRUD methods. Alternatively if there's a tool that might be able to access the restful api and generate generic templates, controllers and services for the restful API?
Thanks in advance for any help!
Mark.
I believe that you are looking for loopback angularjs SDK.
It will not generate views and controllers but you will have angularJS services generated that contains all the LoopBack models and methods you have defined. You have to register the AngularJS module lbServices as a dependency of your app.
Documentation covers this very well with step-by-step instructions how to setup client application.
After you setup loopback angular client, then it is easy to use models in your controllers. All you have to do is to add your model as a dependency in your controller.
If you want to avoid manual work of generating angular controllers, routes, views etc then you should consider using one of the angular application generators ( i.e. yeoman generator-angular ).
To make long story short:
use angular generator to generate angular application.
generate angular services using loopback angularjs SDK.
add loopback model as dependency to your controller or service
See also Angular SDK built-in models API

AngularJS unit testing: Constructor Test: Windows Azure Invoke Api

I am making an Hybrid App using Ionic(AngularJS).
I have a generic factory which uses an invoke Api method of WindowsAzureService JS SDK. And I am unit testing my application.
var mClient = new WindowsAzure.MobileServiceClient(applicationUrl,applicationKey);
mClient.invokeApi(api, data, header)).done(function (res) {
// do something
})
I am not using $http, so I can't mock test cases with a $httpBackend. I would like some help on how to test api calls using windowsAzureServices.
Also how to I spyOn this constructor?
Github link to SDK
There isn't a test harness for Mobile Services. You'll want to follow JB Nizet's advice and create one which can mockup the results you expect.
Alternatively, you can call the API directly and set the appropriate ZUMO headers for auth. Then you can use $http and $httpBackend. It's worth checking out Mobile Services GitHub and looking at how they run E2E tests against the client. You can probably use something similar.

how to do an integration test with angular js to REST api backend?

In my angularjs front end application I am calling REST services on the backend. I am a bit of a newbie in integration testing for angular. I am looking for a good guideline on how to implement this? I dont want to use httpbackend because it looks like this is mocking the backend.
Integration testing generally has multiple meansings, but if your meaning is:
How do I test a user clicking something and then test that their action went all the way to the REST API and some returned result is as expected then I would take a look here:
Protractor
Protractor has/is becoming the standard for End to End testing with Angularjs. It provides a way to simulate user actions and get their result and make assertions. It's built with Selenium and has a nice JavaScript wrapper so that you can keep everything in JavaScript for the front-end. All the tests are defined then using this JavaScript wrapper and Jasmine.

Resources