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' }
        ]);
    });
Umesh_IoT
  • 59
  • 1
  • 11

2 Answers2

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

Community
  • 1
  • 1
AdityaReddy
  • 3,625
  • 12
  • 25
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
    }); 
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This is exactly what I was looking for. HomePage.getStatusGrid() returns a promise so I used the 2nd approach. Thanks alot! – Umesh_IoT Oct 03 '16 at 06:56