单元测试库和API - 如何不重复代码?

时间:2016-11-11 18:15:09

标签: javascript node.js unit-testing

目前我们正在努力构建我们的代码,以免它重复出现。

样本测试:

describe('#getSentence()', function() {
    it('should start with an uppercase character', function() {
        let fChar = ipsumGenerator.getSentence().slice(0, 1);

        fChar.should.be.equal(
            fChar.toUpperCase()
        );
    });
});

目前,我们只是省略了如下所述的测试,只验证是否返回了有效的JSON对象。我们这样做是因为当规范发生变化时,我们不得不调整2个测试,但这确保我们只能手动验证API。

describe('GET /api/ipsum/lorem/sentence/yes', function() {
    it('should return a valid json object', function(whenDone) {
        request(expressApp).get(uriPath)
            .set('Accept', 'application/json')
            .expect('Content-Type', /json/)
            .expect(200)
            .end(function(withError, res) {
                if (withError) return whenDone(withError);

                //
                // Repeating Code:
                let fChar = res.body.s.slice(0, 1);

                fChar.should.be.equal(
                    fChar.toUpperCase()
                );
                //
                // End of Repeating Code

                whenDone();
            );
        });
    });
});

是否有“正确”的方法来省略重复代码?

0 个答案:

没有答案