Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

karate - unable to update variables in a called feature

I am trying to follow the examples in the demo: https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/callfeature I need to do a call from one feature to another, and pass a reference to update. The reference is for a JSON that is read from a file:

  Background: 
    * url url
    * header Authorization = token
    * def payload = read('event.json')
    * set payload.createdByUser = 'karate'

  Scenario: Call another feature with arg
    * call read('classpath:common/swap-json-elements.feature') payload
    * print payload

Inside my swap-json-elements.feature:

  Background: 
    * set new = payload.old
    * set payload.new= payload.old
    * set payload.old= new

This is not working. It is clear in the documentation that a shared scope is shared when we 'set' is used, while 'def' will create a new variable, and never update the shared one.

What am I missing ?

question from:https://stackoverflow.com/questions/65911300/can-i-override-path-in-feature-file-for-a-test-called-from-another-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you pass an argument, it is passed by value. When you call with "shared scope" you typically don't need to pass arguments. Because all variables are visible anyway. Try a simpler example, and please watch white-space around the = sign.

main.feature:

Feature:

Background:
* def json = { foo: 'bar' }
* call read('called.feature')

Scenario:
* match json == { foo: 'baz' }

called.feature

Feature:

Scenario:
* set json.foo = 'baz'
* match json == { foo: 'baz' }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...