You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在fixture.before/after钩子函数中访问ctx对象?

问题:能否在fixture.before或fixture.after钩子的函数中访问ctx对象?

尝试的代码片段

util.js

export function testCtxObject() {
    ctx.test = '123'
}

test.js

import { testCtxObject } from 'util';

fixture('Access ctx object in fixture hooks')
    .before(async (ctx) => {
        testCtxObject();
    });

...

运行报错信息

1) - Error in fixture.before hook -
    Cannot implicitly resolve the test run in the context of which the test controller
    action should be executed. Use test function's 't' argument instead.

解决方案

你当前的问题在于testCtxObject函数内直接引用的ctx未定义,且未将钩子中获取到的ctx传递给该函数。要在外部函数中访问fixture钩子的ctx对象,只需将ctx作为参数传入外部函数即可:

修改util.js:

export function testCtxObject(ctx) {
    ctx.test = '123'
}

修改test.js中的钩子调用:

fixture('Access ctx object in fixture hooks')
    .before(async (ctx) => {
        testCtxObject(ctx); // 传入ctx参数
    });

调整后即可在外部函数中正常操作fixture的ctx对象,不会再出现上下文解析错误。

内容的提问来源于stack exchange,提问作者user5792590

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:57:13