I was working on cucumber testing and met the issue that I don't know how to get the world object "this" outside of cucumber architecture
For example,
function verifyFunction(){
expect(this.a).toEqual(1);
}
Given(
/^bla bla bla$/, function () {
this.a = 1
}
);
Then(
/^bla bla bla$/, function () {
verifyFunction()
}
);
When running the example test like above, it will show the error Cannot read property 'a' of undefined.
Any idea about how to solve this issue?
Thanks a lot!
try using ES6 syntax :
const verifyFunction = () =>{
expect(this.a).toEqual(1);
}
Caveat: I only use Cucumber with ruby.
Generally when using functions from step refs you should pass things into the function and retrieve things from the function. In ruby you would share between steps using a global. So
module StepHelpers
def get_foo
return 'foo'
end
def bar(x)
puts x
end
end
World StepHelpers
Given 'foo' do
@foo = get_foo
end
When 'bar with foo' do
# We can access @foo because its defined as a global in a step
bar(@foo)
end
Try translating the above into your js cukes
n.b. the module stuff is to prevent the methods getting into your applications global namespace.