0

I'm newbie in testing of Cordova app, so could you please give an advice about what is "best practice" in my situation? Situation: I have a module factory:

angular
    .module('app.services')
    .factory('UtilsService', UtilsService);

function UtilsService() {

    var service = {
        isWindows: isWindows,
        isAndroid: isAndroid
    };

    return service;

    function isWindows() {
        return /windows/i.test(device.platform);
    }

    function isAndroid() {
        return /android/i.test(device.platform);
    }
}

and a simple test for isWindows method:

describe('Util Service Tests', function() {

var utilSvc;

beforeEach(function() {
    module('app');
});
beforeEach(function () {
    inject(function($injector) {
        utilSvc = $injector.get('UtilsService');
    });
});

it('should detect windows', function () {
    expect(utilSvc.isWindows).toBe(true);
});

});

I run tests with Chitzpah runner and get an error:

'device' is undefined

I've found the possible solution like chrome-cordova extension, but it doesn't work in my case (or I'm doing something wrong with it). So what should I do here? Mock calls to device method? If yes, how to do that?

Thanks in advance!

AlenSv
  • 125
  • 9

1 Answers1

0

Have you tried using the Cordova browser platform, that can be added with the CLI:

cordova platform add browser

This provides some support for using Cordova APIs / plugins in the browser, but the plugins you're using need to support that platform usually with a JavaScript fallback to whatever their native functionality would do on an actual mobile platform such as iOS or Android. There's a good blog post covering the browser platform here. I'm not clear on how well maintained this platform is but it might be what you're looking for.

Simon Prickett
  • 3,838
  • 1
  • 13
  • 26
  • Thanks for your answer, but unfortunately this didn't work for me, because our app doesn't target browser platform, so I can't use it in tests. I've decided to use another approach, i.e. running jasmine tests on emulator, so that all plugins should be loaded correctly. – AlenSv Feb 11 '16 at 07:28