4

在 Jasmine 测试中每个“它”有多个匹配器是否正确,否则它们会相互干扰?

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

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

这可能/合理吗?

4

2 回答 2

6

您可以根据需要在每个测试中拥有尽可能多的断言。但是当这个测试失败时,有太多内容会让人难以阅读。因为您必须扫描数十行才能找出问题所在,而不是仅仅看到'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);
}));
于 2013-05-09T06:04:20.793 回答
3

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

像您一样在每个 it 块中测试一个行为,并使用 Andreas Köberles 的建议来使用辅助函数。

于 2013-05-13T13:12:38.357 回答