Jasmine:每个“它”或多个匹配器一个?

时间:2013-05-07 15:47:49

标签: unit-testing jasmine matcher

在Jasmine测试中每个“it”有多个匹配器是对的,还是会相互干扰?

我想将这些测试合并为一个:

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);
}));

这可能/合理吗?

2 个答案:

答案 0 :(得分:5)

您可以根据需要为每个测试提供尽可能多的断言。但是,当测试失败时,不得不阅读很多内容。因为你必须扫描数十行来弄清楚什么是错误的,而不是仅仅看到'should be not reviewed if not admin, language is set and mapper is newer'失败。

顺便说一下。在你的情况下,你可以写一个辅助函数,所以你没有那么多的样板:

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);
}));

答案 1 :(得分:2)

我想说,这不是关于每个人的“期望量”。但是每个行为的“行为量”的数量。如果对一种行为的测试需要更多的预期,请使用更多。但是,不要在一个测试用例中测试不属于一起的几个方面。

像你一样测试每个块中的一个行为,并使用AndreasKöberles的建议来使用辅助函数。