The Google Maps Geocoder is a handy utility that lets you do this: var geocoder = new google.maps.Geocoder(); geocoder.geocode({ ‘address’: ‘1600 Amphitheatre Parkway, Mountain View, CA 94043′ }, function(results) { // address results of Google HQ }); When writing unit tests though, we don’t really want to be hitting the Google API each time. So […]
Unit Testing
Mocking Angular’s $http Promise return type using only Mocks
Last week, we had a look at Mocking Angular’s $http Promise return type using the $q library. This gives you a great deal of control over when your mock promises are resolved. But, it comes at the cost of requiring both the $q library and manually calling $rootscope.$apply(). If you want to avoid those (and […]
Mocking Angular’s $http Promise return type
The Angular $http service provides an easy way to interact with an API endpoint. If you want to query some data, you can simply do this: var request = $http.get(“/api/items”); request.success(function(data) { console.log(“I have data”, data); }); The request object above is an Angular HTTP promise (ng.IHttpPromise if you’re using TypeScript). While easy enough to […]
Using Jasmine Spies to Create Mocks and Simplify the Scope of Your Tests
Jasmine spies are a great and easy way to create mock objects for testing. By using a Spy object, you remove the need to create your own function and class stubs just to satisfy test dependencies. Some TypeScript Code Let’s say you have this service for saving a person: iperson-service.ts /// <reference path=”resources/idata-context.ts” /> /// […]
Use Karma and Grunt to Run Your Jasmine Tests in Real-Time
As JavaScript applications become more common and more complex, the need for good unit test coverage also increases. Hopefully you’re already writing tests. If not, why not? When I’m doing TDD with C#, I use NCrunch to monitor all tests within the Visual Studio Solution and run them as they change. This saves me having […]
Unit Testing Database Access
The goal of unit testing is to create a small, self-contained tests for a piece of code that tests the functionality of that piece of code in isolation from the rest of the system. Put more simply, it should test the code you want to test and nothing more. This can be a little tricky […]