Introducing ngCordovaMocks

  • Estimated read time: 5 min read
  • Written by Chad Campbell on Aug 19th 2014

From the official ngCordova site:

"ngCordova is a set of AngularJS extensions on top of the Cordova API to make it easier to build, test, and deploy Cordova apps with AngularJS."

These extensions are great for building and deploying apps that will run on a device. However, you may want to develop your app in the browser and deploy it to a device later. For that reason, I'm excited to introduce ngCordovaMocks.

What is ngCorodvaMocks?

ngCordovaMocks is a set of services designed to complement ngCordova during development and testing. It was designed for developers|engineers|architects who prefer to use the browser before testing on real devices. ngCordovaMocks provides:

  • Faster development cycles. Deploying to an emulator or real-device is time-consuming. With ngCordovaMocks, you can use emulation tools, like those in Google Chrome, to build your app.

  • Deeper testing. Create end-to-end tests with frameworks like Jasmine. ngCordovaMocks exposes properties so that you can quickly create end-to-end tests. For instance, you may want to test a scenario where $cordovaNetwork is offline... you got it. You may want to test a scenario where $cordovaGeolocation returns a specific geolocation... no problem. No fancy services to learn. Just plug-and-chug. The properties are detailed here

  • Smaller production code. Instead of modifying code to enable deeper testing, which would later go into production, ngCordovaMocks relies on you to modify your build process. This keeps your production code smaller, but enables deep testing. Modifying your build process is explained in Getting Started with ngCordovaMocks.

  • Easy switching between fake, mock mode and real mode. This is enabled during your build process. This is described in Getting Started with ngCordovaMocks.

As crazy as it may sound... ngCordovaMocks should never make it to your production code. It's there to help make your production code better.

Why did you create ngCordovaMocks?

There are two reasons I created ngCordovaMocks. First, while working on some apps, I learned ngCordova wasn't enough for me. While ngCordova is great, I still had two challenges:

  1. Developing in the browser.
  2. Easily simulting certain conditions.

I created ngCordovaMocks to help me with my challenges. I decided to open source ngCordovaMocks thinking it might help you.

The other reason I created ngCordovaMocks is to get some personal visibility. As of the time of writing, I'm between projects as an independent consultant. I felt that other individuals may find value in ngCordovaMocks. So, I decided to open source it. I hope you find it valuable. In addition, if you're looking for some help with your software project(s), please contact me.

Do I need to change my code?

Assuming you're already using ngCordova, I view it as a config change. I have a config setting in my build process that replaces two things:

  1. The ngCordova file to load. (use ngCordovaMocks.min.js instead of ngCordova.js)
  2. The module to load. (use ngCordovaMocks instead of ngCordova)

Technically, this could be viewed as a code change. It will be dependent on how your project and build process are structured. Either way, you can learn more here.

Why did or didn't you...?

There are other ways of solving these challenges. In fact, in my opinion, most, if not all of the $cordova services should have a $cordovaBackend service. This would help make ngCordova more consistent with AngularJS itself (I'm basing this on $http and $httpBackend). This is my opinion. You may have others.

The point is, I needed a quick way to test some scenarios. These services were the fastest for me to write. I believe the code adds real value. I did not feel I could productize it. For those reasons, I decided to open source it. If it adds value for you, please let other developers know. You can let other developers know with the bar below. Simply like this post, Plus 1 this post, or tweet this post out to make others aware.

What are those unit tests doing?

The unit tests are little more then getters and setters. They are there to validate the API exists. There are some things testing that a callback or promise is working as expected. Beyond that, there is little actual logic.

How complete is this?

I built what I needed, added a build process, and some documentation. I will probably add more as I need it. For now, it meets my needs. It is by no means complete and is provided "AS IS". Use at your own risk. There is a LOT of work to be done to make this thing complete. Some of this is little more than shells to prevent code from breaking.

Here are some of the gotchas I could think of off of the top of my head:

  • $cordovaCamera does not handle user permissions. It also does not return any picture data at this time.
  • $cordovaCapture does not handle user permissions. For example, what if a user chooses NOT to allow an app access to their media capturing components?
  • $cordovaContacts has a very ROUGH approach for searching contacts.
  • $cordovaDeviceMotion does not handle user permissions.
  • $cordovaDeviceOrientation does not handle user permissions.
  • $cordovaFile is FAR from complete. I would like to get around to implementing an in-memory JSON file system. It wouldn't be very robust. Just something for testing purposes.
  • $cordovaGeolocation does not handle user permissions.
  • $cordovaGlobalization does very little as of the time of this writing.
  • $cordovaVibration does not handle vibration patterns.

You might be thinking, what does this library do? That is a fair question. I think it does a lot. You can view some of the additions here. I also think more will be added over time. I had a debate. Wait to share until it was done. Or, share what I had thus far. I went for the latter to see how it goes. I hope you like it.


comments powered by Disqus

Chad Campbell
Chad Campbell

Chad is an independent software professional. He has been named a Microsoft MVP five times. His books have been translated into multiple languages and distributed worldwide. He holds a computer science degree from Purdue University, where he also studied psychology.

Chad has built sites, apps, frameworks, libraries, and platforms using Java, .NET, and Node. He's ran his own startups and has created software for Fortune 100 companies. In short, Chad knows how to create software. From ideation to delivery. From start-to-finish.

Follow Chad Online