Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Same mutable string object is repeatedly passed to steps #453

Closed
jamespharaoh opened this Issue · 2 comments

4 participants

@jamespharaoh

Strings for the arguments passed to steps are mutable string objects. So if I change one then that change persists across all future calls to the step.

For example:

Given "A file \"(.+)\"" do |file_name, file_contents|
    file_contents.gsub! "${placeholder}", @some_value
    File.open file_name, "w" { |file_io| file_io.write file_contents }
end

The ${placeholder} will be replaced by the value of @some_value the first time this step is run, and it keeps the same value forever after, even if @some_value is different for future test.

The simple workaround for this is not to modify the string, for example:

file_contents = file_contents.gsub "${placeholder}", @some_value

...but I keep making this mistake, and cucumber could certainly be more clever about this, and arguably should be.

The simple solution is just for cucumber to freeze the strings it uses. This will make my call to gsub! throw an error.

A slightly nicer solution might be to clone the strings when passing them into the steps.

I have had a look at the cucumber code to try and write a test and fix for this but I can't really see where this ought to go, so am submitting an issue instead. If someone can give me some pointers into the code I'll happily create a pull request.

@aslakhellesoy

I vote for dup rather than freeze!. We shouldn't try to enforce programming practices.

@chrismdp chrismdp modified the milestone: 2.0
@mattwynne
Owner

@tooky this is fixed by #760 right?

@mattwynne mattwynne closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.