我已经为使用 Jasmine 构建的 Node.js 驱动的 API 编写了一些单元测试。
测试工作正常,但现在我想运行一组具有不同输入的异步测试,但我无法让它工作。
我的测试针对 API 运行一些请求,并在 URL 中使用“offset”和“limit”参数,并且 API 在响应中插入分页链接(第一个、上一个、下一个和最后一个)。
这是我的 Jasmine 测试:
describe('API with "limit" and "offset" parameters', function() {
var offset = 0;
var limit = 4;
var url = baseUrl + '?limit=' + limit + '&offset=' + offset;
beforeEach(function(done) {
request.get(url, function(err, res, resBody) {
response = res;
body = JSON.parse(resBody);
count = body.count;
done();
});
});
it('should respond with status code 200', function(done) {
expect(response.statusCode).toEqual(200);
done();
});
it('should have a response with [limit] results if count - offset >= limit, otherwise count - offset', function(done) {
if(count - offset >= limit) expect(body.rows.length).toEqual(limit);
else expect(body.rows.length).toEqual(count - offset);
done();
});
it('should have navigation links', function(done) {
expect(body.first).toBeDefined();
expect(body.prev).toBeDefined();
expect(body.next).toBeDefined();
expect(body.last).toBeDefined();
done();
});
it('should have "first" navigation link equal to url offset=0 and limit=limit', function(done) {
expect(body.first).toEqual(baseUrl + '?limit=' + limit + '&offset=0');
done();
});
it('should have "prev" navigation link equal to null if offset=0, otherwise offset = offset - limit', function(done) {
if(offset ===0) expect(body.prev).toBeNull();
else expect(body.prev).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset - limit));
done();
});
it('should have "next" navigation link equal to offset + limit, or null if offset+limit > count', function(done) {
if(offset + limit <= count) expect(body.next).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset + limit));
else expect(body.next).toBeNull();
done();
});
it('should have "last" navigation link equal to floor(count/limit)*limit', function(done) {
expect(body.last).toEqual(baseUrl + '?limit=' + limit + '&offset=' + Math.floor(count/limit) * limit);
done();
});
});
此测试运行良好,但现在我想重复运行此测试,在每次运行中将“偏移量”增加“限制”,直到到达最后一页(即偏移量+限制>=计数),但我不能让这个工作起来。
到目前为止我已经尝试过
我的第一个想法是将测试放入一个循环中,封装在 IIFE 中,但这不起作用,因为 for 循环中的“count”未定义(我猜它只能在“it”内部使用):
for(var i=offset; i<count; i+=limit) { // "count" is undefined here
(function(offset, count, limit) {
it('should have "first" navigation link equal to url offset=0 and limit=limit', function(done) {
expect(body.first).toEqual(baseUrl + '?limit=' + limit + '&offset=0');
done();
});
it('should have "prev" navigation link equal to null if offset=0, otherwise offset = offset - limit', function(done) {
if(offset ===0) expect(body.prev).toBeNull();
else expect(body.prev).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset - limit));
done();
});
it('should have "next" navigation link equal to offset + limit, or null if offset+limit > count', function(done) {
if(offset + limit <= count) expect(body.next).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset + limit));
else expect(body.next).toBeNull();
done();
});
it('should have "last" navigation link equal to floor(count/limit)*limit', function(done) {
expect(body.last).toEqual(baseUrl + '?limit=' + limit + '&offset=' + Math.floor(count/limit) * limit);
done();
});
})(offset, count, limit);
}
我的第二个想法是在“afterAll”内部进行递增,再次将测试包装在 IIFE 中。 看来 afterAll 确实再次运行 runTests() (我看到 console.log 出现),但测试本身没有再次运行:
(function runTests(offset, count, limit) {
console.log('running tests');
beforeEach(function(done) {
request.get(url, function(err, res, resBody) {
response = res;
body = JSON.parse(resBody);
count = body.count;
done();
});
});
it('should have "first" navigation link equal to url offset=0 and limit=limit', function(done) {
expect(body.first).toEqual(baseUrl + '?limit=' + limit + '&offset=0');
done();
});
it('should have "prev" navigation link equal to null if offset=0, otherwise offset = offset - limit', function(done) {
if(offset ===0) expect(body.prev).toBeNull();
else expect(body.prev).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset - limit));
done();
});
it('should have "next" navigation link equal to offset + limit, or null if offset+limit > count', function(done) {
console.log('next', offset, limit, count);
if(offset + limit <= count) expect(body.next).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset + limit));
else expect(body.next).toBeNull();
done();
});
it('should have "last" navigation link equal to floor(count/limit)*limit', function(done) {
expect(body.last).toEqual(baseUrl + '?limit=' + limit + '&offset=' + Math.floor(count/limit) * limit);
done();
});
afterAll(function() {
offset += limit;
if(offset < count) runTests(offset, count, limit);
});
})(offset, count, limit);
我在这里做错了什么?还有其他方法可以让它工作吗?
更新:这是我基于 Zlatko 的回答的完整工作代码:
describe('API with "limit" and "offset" parameters', function() {
var offset = 0;
var limit = 4;
var url = baseUrl + '?limit=' + limit + '&offset=' + offset;
function testNavLinks(limit, offset, url) {
describe('Check navigation links with offset = ' + offset, function() {
var response;
var count;
var body;
beforeEach(function(done) {
request.get(url, function(err, res, resBody) {
response = res;
body = JSON.parse(resBody);
count = body.count;
done();
});
});
it('should respond with status code 200', function(done) {
expect(response.statusCode).toEqual(200);
done();
});
it('should have a response with [limit] results if count - offset >= limit, otherwise count - offset', function(done) {
if(count - offset >= limit) expect(body.rows.length).toEqual(limit);
else expect(body.rows.length).toEqual(count - offset);
done();
});
it('should have navigation links', function(done) {
expect(body.first).toBeDefined();
expect(body.prev).toBeDefined();
expect(body.next).toBeDefined();
expect(body.last).toBeDefined();
done();
});
it('should have "first" navigation link equal to url offset=0 and limit=limit', function(done) {
expect(body.first).toEqual(baseUrl + '?limit=' + limit + '&offset=0');
done();
});
it('should have "prev" navigation link equal to null if offset=0, otherwise offset = offset - limit', function(done) {
if(offset === 0) expect(body.prev).toBeNull();
else expect(body.prev).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset - limit));
done();
});
it('should have "next" navigation link equal to offset + limit, or null if offset+limit > count', function(done) {
if(offset + limit <= count) expect(body.next).toEqual(baseUrl + '?limit=' + limit + '&offset=' + (offset + limit));
else expect(body.next).toBeNull();
done();
});
it('should have "last" navigation link equal to floor(count/limit)*limit', function(done) {
expect(body.last).toEqual(baseUrl + '?limit=' + limit + '&offset=' + Math.floor(count/limit) * limit);
done();
});
});
}
// 13 is the actual number of results
// I did not manage to get the actual number from the response, see my comment below
for(var i=0; i<13; i+=limit) {
url = baseUrl + '?limit=' + limit + '&offset=' + i;
testNavLinks(limit, i, url);
}
});
最佳答案
因此,您尝试多次重复测试,但根本不需要处理 IIFE。只需让您的测试成为一个函数,然后从循环中调用它即可:
describe('Loop an async set of tests.', function() {
var baseUrl = process.env.API_URL; // or whatever
function loopMyApi(offset, limit) {
// add a new suite
describe('Calling at offset: ' + offset , function() {
var response;
var myUrl = baseUrl + '?limit=' + limit + '&offset=' + offset;
beforeEach(function(done) {
request(myUrl, function(err, res) {
if (err) {throw err;}
response = res;
done();
});
});
it('should have length', function() {
// btw since response is already here, this test is now sync
// no need for `done`.
response.body.length.should.equal(limit);
});
it('should be ok', function() {
response.statusCode.should.be.lessThan(399);
});
});
}
var limit = 10;
// you can start like this, or just make a regular for
// loop as you have in your examples.
[0, 10, 20, 30].forEach(function(offset) {
// if you want to modify limit for some reason, do it here.
loopMyApi(offset, limit);
});
});
根据需要进行调整以适应您的场景。
关于javascript - 循环内的 Jasmine 异步测试未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280775/