繁体   English   中英

testcafe requestLogger 只记录夹具中的第一个测试

[英]testcafe requestLogger only logs the first test in the fixture

所以我在 testcafe 方面做得越来越好,我想学习的一项功能是它的 RequestLogger。

所以我创建了它的一个实例

    import { RequestLogger } from 'testcafe';

const logger = RequestLogger(/some reg exp/, {
    logRequestHeaders: true,
    logRequestBody: true
});

export default logger;

然后尝试在示例测试夹具上使用它:

fixture `REQUEST LOGGER TEST`
.requestHooks(logger);

test('should contain 204 as a status code 1', async t => {
    await t.useRole(role);
    await model.doSmth(t);
    await model.doSmthElse(t);

    console.log(logger.requests);

    await t
        .expect(logger.requests[0].response.statusCode)
        .eql(204);

    await t
        .expect(logger.requests[1].response.statusCode)
        .eql(200);
});

虽然第一个测试工作得很好,但第二个,即使它是相同的,一旦我尝试 console.log(logger.requests) 将输出一个空数组

知道如何去做吗?

我遇到了同样的问题,因为在对 Logger.request 数组进行断言之前,您必须等待 Testcafe 的智能断言查询机制。

文档告诉我们,使用 count 或 contains 谓词使 Testcafe 使用智能断言查询机制

如果您的请求返回 200 statusCode ,Marion 的建议可能会完成这项工作:

await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();

我发现这样做更简单,它不依赖于返回的 http 状态

await t.expect(logger.count(() => true)).eql(1);

在执行断言之前,将 eql(1) 替换为您希望记录的请求数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM