vaibhav
vaibhav

Reputation: 784

Jasmine - How to Write Test for Array with Named properties and Object

I ran into a strange situation today, thanks to Javascript. I have a Object that look something like this.

$scope.main = [{main : 1},service:true];

Now when I try to expect this inside the jasmine test case for equating the Objects :

expect($scope.main).toEqual([{main : 1},service:true]);

This gives me an error :

Unexpected Token.

Strangely, This is a valid object for Javascript. But Jasmine is not able to accept that.

Is there any way to test this?

Thanks in advance!

EDIT : Attaching a structure screenshot.

structure image

Upvotes: 0

Views: 156

Answers (1)

Seth Flowers
Seth Flowers

Reputation: 9190

Update

I see now based on your screenshot that you are creating the main object in multiple steps. I've shortened it to the following:

var main = [{main: 1}];
main.service = true;

In dev-tools, you are seeing main as something that looks like this: [{main: 1}, service: true].

However, don't be mislead. Dev-tools is showing you a structure that is just meant to be informative. You can't actually create that structure in one line of javascript, because it is invalid. You have to create it in multiple steps, like you have.

This is why when you try to create it in your test in one line, you are getting an Unexpected Token. error. In your test, you have to create the expected object in a similar fashion to how you created your main object. For example:

var expected = [{main: 1}];
expected.service = true;

expect(main).toEqual(expected);

Upvotes: 1

Related Questions