0

Suppose the following JSON is expected as part of a Frisby test:

{
    array: ["1", "2", "3"]
}

The array of strings may return in any order, say ["3", "1", "2"].

How can I expect the above defined array without expecting an order?

I have tried

.expectJSON('array.?', "1")
.expectJSON('array.?', "2")
.expectJSON('array.?', "3")

but this is not valid syntax and the following error occurs:

 TypeError: Expected valid JavaScript object to be given, got undefined

4 Answers4

2

I went into the same problem, and finally figure this out. Using .afterJSON(), you will be able to use Jasmine's expect-syntax to do all the validation you want on the JSON object.

For your example, the script will look like this:

    .afterJSON(function(json){
        expect(json.array).toContain('1');
        expect(json.array).toContain('2');
        expect(json.array).toContain('3');
    })
Joe H
  • 1,447
  • 2
  • 11
  • 8
1

Did you figure it out? I think I'm a little late but, I'm pretty sure it's because .expectJSON is expecting a JSON object.

This .expectJSON('array.?', "1")

Becomes .expectJSON('?', { array: (not sure how to do this part) })

But you should be able to understand why the test is failing now... Sorry not great a frisby yet ;p Came here looking for help myself.

minijag
  • 59
  • 5
1

Read the response as text instead of object and look for the desired array items in the text. Ie. .expectBodyContains ('1') .expectBodyContains ('2') .expectBodyContains ('3')

Now you have to be careful in you have multiple arrays with the same possible values, but using JSON.stringify and simple string manipulation, you should be able to isolate the desired array.

0

To test for some objects in an array:

.expectJSON('array.?', '1')
.expectJSON('array.?', '2')
.expectJSON('array.?', '3')

According to the docs.

Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48