Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine: one matcher per "it" or multiple?

Is it right to have multiple matchers per one "it" in Jasmine tests or they will interfere with each other?

I want to consolidate these tests into one:

var mapper = ......... ;

it('should be reviewed if not admin and language not set', inject(function() {
    scope.globals.isAdmin = false;
    scope.globals.language = '';
    mapper.updatedOn.setYear(2013);
    expect(scope.isReviewed(mapper)).toBe(true);
}));

it('should disregard mapper date if not admin and language not set', inject(function() {
    scope.globals.isAdmin = false;
    scope.globals.language = '';
    mapper.updatedOn.setYear(2015);
    expect(scope.isReviewed(mapper)).toBe(true);
}));

it('should be reviewed if admin and mapper is older', inject(function() {
    scope.globals.isAdmin = true;
    scope.globals.language = '';
    mapper.updatedOn.setYear(2013);
    expect(scope.isReviewed(mapper)).toBe(true);
}));

it('should be not reviewed if admin and mapper is newer', inject(function() {
    scope.globals.isAdmin = true;
    scope.globals.language = '';
    mapper.updatedOn.setYear(2015);
    expect(scope.isReviewed(mapper)).toBe(false);
}));

it('should be reviewed if not admin, language is set and mapper is older', inject(function() {
    scope.globals.isAdmin = false;
    scope.globals.language = 'de';
    mapper.updatedOn.setYear(2013);
    expect(scope.isReviewed(mapper)).toBe(true);
}));

it('should be not reviewed if not admin, language is set and mapper is newer', inject(function() {
    scope.globals.isAdmin = false;
    scope.globals.language = 'de';
    mapper.updatedOn.setYear(2015);
    expect(scope.isReviewed(mapper)).toBe(false);
}));

Is this possible/reasonable?

like image 257
Paul Avatar asked Jan 13 '23 10:01

Paul


2 Answers

You can have as much asserts per tests as you want. But having to much makes it hard to read when this tests fails. Cause you have to scan tens of lines to figure out whats wrong, instead of just see that 'should be not reviewed if not admin, language is set and mapper is newer' fails.

Btw. in your case you could write a helper function so you dont have that much boilerplate:

var admin = true;
var notAdmin = false

init(isAdmin, language, year) {
    scope.globals.isAdmin = isAdmin;
    scope.globals.language = language;
    mapper.updatedOn.setYear(year);
}

it('should be not reviewed if not admin, language is set and mapper is newer', inject(function() {
    init(admin, 'de', 2015)
    expect(scope.isReviewed(mapper)).toBe(false);
}));
like image 101
Andreas Köberle Avatar answered Jan 17 '23 20:01

Andreas Köberle


I would say, it is not about the 'amount of expects' per it. But of the amount of 'amount of behaviours' per it. If the testing of one behaviour needs more then one expect, use more. But don't test several aspects which don't belong together in one test case.

Test one behaviour in each it block like you did and use Andreas Köberles suggestion to use a helper function.

like image 44
Frederick Roth Avatar answered Jan 17 '23 20:01

Frederick Roth