'Data Type Jasmine

I'm a beginner in angular test with jasmine and i need to test data type(e.g. String, int, etc.)

This is my controller, my data is instantiated to null and later it will be a string :

_this.instance = {
      template_fields: {
        guid: null
      }
}

// It becomes string
<input type="radio" ng-model="prCreateCtrl.instance.template_fields.guid" value="{{template.guid}}" required>

This is my test :

beforeEach(inject(function ($state, $controller, $rootScope, $q) {
    state = $state.get('app.provision');
    ctrl = $controller('ProvisionCreateCtrl', {
      instance: {
        template_fields: {
          guid : {}
        }
      }
    });
}));

it('should resolve data', function () {
  expect(ctrl.instance.template_fields.guid instanceof String).toBeTruthy();
});

I don't understand when should I give a String and how can I test it. Is using instanceof String the right way ?



Solution 1:[1]

You can simply use jasmine.any() function.

In your case, inside it() block:

expect(ctrl.instance.template_fields.guid).toEqual(jasmine.any(String));

Solution 2:[2]

You can try using toBeInstanceOf matcher:

Inside it() block:

expect(ctrl.instance.template_fields.guid).toBeInstanceOf(String);

I think it's simple & makes your test more readable. Have fun :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jagrut
Solution 2 Shadowalker