Jasmine test for object methods

Logan Shreve

Hello I am a bit inexperienced in Javascript and jasmine and am attempting to write some simple tests for a javascript object.

var googleMap = {
    geoCode: function(code, fn) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': code
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                fn(results[0].geometry.location);
            } else {
                alert("ALL IS LOST");
            }
        })
    },
    render: function(LatLng) {
        var mapOptions = {
            zoom: 8,
            center: LatLng
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        var marker = new google.maps.Marker({
            map: map,
            position: LatLng
        });
    }
};

-Here is my jasmine script; I am attempting to make sure that the render function is being called whenever we run the geocode function...

describe("Testing of googleMap.js", function() {
    it("Test GeoCode", function() {
        var input = "Columbus";
        spyOn(googleMap, 'render');
        googleMap.geoCode(input, googleMap.render);
        expect(googleMap.render).toHaveBeenCalled();
    });
});

whenever I run this my specRunner returns an error claiming the expect claimed spy render to have been called, I am a bit baffled as to why it wouldn't have been.... is this to do with how I am attempting to set up my spy, or is the javascript object I made impossible to test. I am having trouble finding similar examples to work from.

friedi

The method .geocode of the object google.maps.Geocoder is asynchronous.
Therefore at the time of checking if the callback function was executed, you haven't got your response back and this means your function will not be called.

If all you want is to test if the function will be called then you can do something like the following:

describe("Testing of googleMap.js", function() {
    it("Test GeoCode", function(done) {
        var input = "Columbus";
        googleMap.geoCode(input, function() {
            // here you know that your callback function was called
            done();
        });
    });
});

Or if you want still use a mock function you can do this (with jasmine 2.0):

describe("Testing of googleMap.js", function() {
    it("Test GeoCode", function(done) {
        var input = "Columbus";
        var callbackSpy = jasmine.createSpy("callback").and.callFake(function() {
            done();
        });
        googleMap.geoCode(input, callbackSpy);
    });
});


With the given code blocks you get an timeout error if the callback function doesn't get called:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.


Here is a demo (with an mocked google geocoder).



This is how you can fix your code. The question is: Is it reasonable to make a test like this?
In unit tests you should test your code separated from other resources (like google geocoder). So imho in this situation it is better to mock the geocoder object and test if the mock was called with the right arguments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jasmine test for object properties

From Dev

Jasmine test for scope methods fails

From Dev

jasmine test complaining with undefined is not an object

From Dev

angular 2 - jasmine- test methods in typescript

From Dev

Jasmine test complaining about 'undefined' is not an object

From Dev

AngularJS Jasmine test: TypeError: 'undefined' is not an object

From Dev

AngularJS TypeError: 'undefined' is not an object' Jasmine test

From Dev

'undefined is not an object' in karma/jasmine unit test

From Dev

Jasmine/JavaScript: Test that a new object was created

From Dev

karma-jasmine - how to test return object

From Dev

how to check if methods have been called jasmine unit test

From Dev

Jasmine test state of object on Ajax method spy call

From Dev

Jasmine Test: Object doesn't support property or method

From Dev

How to test service object methods are called?

From Dev

Use an object in other methods of a test class

From Dev

How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

From Dev

How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?

From Dev

Test controller with promises with jasmine

From Dev

jasmine 2 integration test

From Dev

Accessing IIFE in Jasmine test

From Dev

Jasmine - How to test errors?

From Dev

"Test Finished" handler in Jasmine?

From Dev

Controller undeclared in Jasmine test

From Dev

Jasmine test, addEventListener

From Dev

Understand $controller in Jasmine test

From Dev

Jasmine Test in Angular for controller

From Dev

jasmine test a Restangular response

From Dev

Jasmine test always passing

From Dev

Testing if a Jasmine Test Fails

Related Related

HotTag

Archive