2

我需要获取当前确切的集合变量值。

在邮递员请求的预请求脚本中,我设置了2 个集合变量,如下所示

pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}");
pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVariable}}");

然后我在发布请求正文中使用这两个集合变量来设置特定数据,如下所示

 { 
    "firstKey": {{firstCollectionVariable}},
    "secondKey" : {{secondCollectionVariable}},
 }

firstKey 和 secondKey 按预期设置

firstKey => "string_c6631d2c-2427-4903-b604-8120662a5e0e"

secondKey => "second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e"

问题是当我尝试使用检查响应时

pm.expect(pm.response.secondKey).to.eql(pm.collectionVariables.get("secondCollectionVariable"));

我得到断言错误

AssertionError:预期“second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e”深度等于“second_variable_{{firstCollectionVariable}}”

如何获得集合变量的当前确切值?

4

2 回答 2

1

也许使用to.be.deep可以解决您的问题,请尝试以下操作:

pm.expect(pm.response.secondKey).to.be.deep.equals(pm.collectionVariables.get("secondCollectionVariable"));

或者

pm.expect(pm.response.secondKey).to.be.deep.equal(pm.collectionVariables.get("secondCollectionVariable"));

另一个不错的方法是使用单引号来获取您的 collectionVariable 之类的'secondCollectionVariable'

我希望这对你有用,最好的问候!

于 2020-07-29T01:17:30.287 回答
0

将变量转换为字符串也可以解决您的问题。

于 2020-07-31T07:29:43.680 回答