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?
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);
}));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With