我正在玩空手道 v1.4.0
想要定义一个用于模拟创建的函数和变量(
pathToMy.js
总是返回true
):
@setup
Scenario:
* def data = read('data.json')
* def myFunc = read('pathToMy.js')
Scenario: pathMatches('/my_url') && methodIs('post') && myFunc()
* def responseHeaders = { 'Content-Type': 'application/json' }
* def response = data
* def responseStatus = 200
当我在测试运行时请求
/my_url
时,它不会返回任何内容:此外,无论是否有 @setup
的另一个场景都不允许捕获模拟 URL:
Scenario:
* def data= read('data.json')
# Note that not connected to the above scenario at all
Scenario: pathMatches('/my_url') && methodIs('post')
* def responseHeaders = { 'Content-Type': 'application/json' }
* def response = {"my":"data"}
* def responseStatus = 200
另外,当不使用附加场景,而是尝试调用方法进行匹配时,它不起作用:
Scenario: pathMatches('/my_url') && methodIs('post') && call read('pathToMy.js') # call diectly
* def responseHeaders = { 'Content-Type': 'application/json' }
* def response = {"my":"data"}
* def responseStatus = 200
但是当我在没有额外场景和方法调用的情况下做同样的事情时,它就可以工作了:
Scenario: pathMatches('/my_url') && methodIs('post')
* def responseHeaders = { 'Content-Type': 'application/json' }
* def response = read('data.json') # read data from a file
* def responseStatus = 200
附注对于我的用例来说,一个理想的解决方案是可以拥有
Scenario Outline
,但似乎也不支持它(因为我使用 @setup
来提供外部 json 文件)
@setup
钩子仅适用于测试,不适用于模拟。由于模拟的 Background
“仅”运行一次,因此您只需设置一个变量,根据需要调用 JS 函数,无需其他操作。如果您确实需要特定的东西,请考虑贡献代码。