0

I finally got around to TTD! Testing is great however I have a problem. I am testing a function that makes a HTTP call and I receive a large JSON object that has a property called result. result is an object array, it kinda looks like this:

{
    itemsPerPage: 2,
    numPages:1,
    offset:0,
    actualPage:1,
    firstPostingOnPage:1,
    lastPostingOnPage:2,
    totalNumberOfJobs:2,
    result: [
        {
            jobPostingId:1,
            postingVersionId:1,
            title: 'first job',
            applyOnlineLink: 'some link',
            config: null,
            jobStartDate: 1234567890,
            postingVersionStartDate:1234567890,
            postingVersionEndDate:1234567890
        }
    ]
};

result can have any length but never zero. I want to test the structure of the objects in result. In my app I have the following test:

it('The object array should have an object that contains keys "jobPostingId, postingVersionId, title" ', () => {

                    let result:Observable<any> = sut.getJobs();

                    return result.subscribe((content:any) => {

                        assertThat(content, hasProperty('result',

                            containsInAnyOrder({
                                'jobPostingId': is(number()),
                                'postingVersionId': is(number()),
                                'title': is(string()),
                                'applyOnlineLink': is(string()),
                                'config': is(falsy()),
                                'jobStartDate': is(number()),
                                'postingVersionStartDate': is(number()),
                                'postingVersionEndDate': is(number())
                            })
                        ));
                    });
                });

This fails as I get the error

Expected: an object with {result: [{"jobPostingId":{},"postingVersionId":{},"title":{},"applyOnlineLink":{},"config":{},"jobStartDate":{},"postingVersionStartDate":{},"postingVersionEndDate":{}}] in any order}
[2]          but: result no item in [{"jobPostingId":1,"postingVersionId":1,"title":"first job","applyOnlineLink":"some link","config":null,"jobStartDate":1234567890,"postingVersionStartDate":1234567890,"postingVersionEndDate":1234567890}, {"jobPostingId":2,"postingVersionId":1,"title":"second job","applyOnlineLink":"some link","config":null,"jobStartDate":1234567890,"postingVersionStartDate":1234567890,"postingVersionEndDate":1234567890}] matches: {"jobPostingId":{},"postingVersionId":{},"title":{},"applyOnlineLink":{},"config":{},"jobStartDate":{},"postingVersionStartDate":{},"postingVersionEndDate":{}}

I'm obviously doing something wrong, why does it expect an object after each property in the object from the object array.

Thanks in advance, sorry if I have asked a silly question.

1 Answer 1

1

I was using the wrong matchers, etc... this worked...

it('The object array should have an object that contains keys "jobPostingId, postingVersionId, title", etc.... ', () => {

                    let result:Observable<any> = sut.getJobs();

                    return result.subscribe((content:any) => {

                        assertThat(content, hasProperty('result',
                            contains(
                                hasProperties({
                                    'jobPostingId': is(number()),
                                    'postingVersionId': is(number()),
                                    'title': is(string()),
                                    'applyOnlineLink': is(string()),
                                    'config': is(null),
                                    'jobStartDate': is(number()),
                                    'postingVersionStartDate': is(number()),
                                    'postingVersionEndDate': is(number())
                                }),
                                hasProperties({
                                    'jobPostingId': is(number()),
                                    'postingVersionId': is(number()),
                                    'title': is(string()),
                                    'applyOnlineLink': is(string()),
                                    'config': is(null),
                                    'jobStartDate': is(number()),
                                    'postingVersionStartDate': is(number()),
                                    'postingVersionEndDate': is(number())
                                })
                            )
                        ));
                    });
                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.