1

I have a test where i'm expecting an array of objects. The below expect condition works fine. But each day due to the system under test behavior we need to change the expected data object array. So my question is, how to expect a pattern for these type of object array without expecting the exact value? or is there any other good way to handle this?

    it('Verify the functionality of displaying correct data in Status grid table', function() {
        expect(HomePage.getStatusGrid()).toEqual([ 
            { make : 'Make1',   model : 'Model1',   totLoads : '17.24', washDays : 'Wednesday', timeDay : '10:00-11:00' },
            { make : 'Make1',   model : 'Model2',   totLoads : '15.58', washDays : 'Wednesday', timeDay : '16:00-17:00' },
            { make : 'Make1',   model : 'Model3',   totLoads : '17.17', washDays : 'Monday',    timeDay : '18:00-19:00' },
            { make : 'Make2',   model : 'Model4',   totLoads : '16.27', washDays : 'Monday',    timeDay : '19:00-20:00' },
            { make : 'Make2',   model : 'Model5',   totLoads : '16.19', washDays : 'Thursday',  timeDay : '19:00-20:00' },
            { make : 'Make2',   model : 'Model6',   totLoads : '15.01', washDays : 'Friday',    timeDay : '10:00-11:00' },
            { make : 'Make3',   model : 'Model7',   totLoads : '16.94', washDays : 'Tuesday',   timeDay : '11:00-12:00' },
            { make : 'Make3',   model : 'Model8',   totLoads : '15.72', washDays : 'Thursday',  timeDay : '10:00-11:00' },
            { make : 'Make3',   model : 'Model9',   totLoads : '15.90', washDays : 'Saturday',  timeDay : '16:00-17:00' }
        ]);
    });

2 Answers 2

1

Having a custom matcher may help to hide the complexity of the matching checks and achieve reusability.

Though, a straight-forward approach to loop over rows in a grid and apply the toMatch() matchers can be good enough:

var grid = [      
  { make : 'Make1',   model : 'Model1',   totLoads : '17.24', washDays : 'Wednesday', timeDay : '10:00-11:00' },
  { make : 'Invalid make',   model : 'Model1',   totLoads : '17.24', washDays : 'Wednesday', timeDay : '10:00-11:00' },    
];
grid.forEach(function (row) {
    expect(row.make).toMatch(/Make\d+/);
    expect(row.model).toMatch(/Model\d+/);
    // TODO: more checks
}); 

Note that if HomePage.getStatusGrid() returns a promise, you would need to explicitly resolve it:

HomePage.getStatusGrid().then(function (grid) {
    grid.forEach(function (row) {
        expect(row.make).toMatch(/Make\d+/);
        expect(row.model).toMatch(/Model\d+/);
        // TODO: more checks
    }); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. HomePage.getStatusGrid() returns a promise so I used the 2nd approach. Thanks alot!
1

I guess you can write a custom matcher in Jasmine to do the pattern check for you instead of complete data match

Some links to get you started on this.

jasmine 2.0 test with a custom matcher fails: undefined is not a function

http://jasmine.github.io/2.0/custom_matcher.html

1 Comment

I will check this. Thanks.

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.