Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same mutable string object is repeatedly passed to steps #453

Closed
jamespharaoh opened this issue May 13, 2013 · 3 comments
Closed

Same mutable string object is repeatedly passed to steps #453

jamespharaoh opened this issue May 13, 2013 · 3 comments

Comments

@jamespharaoh
Copy link

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
Copy link
Contributor

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

@chrismdp chrismdp modified the milestone: 2.0 Mar 6, 2014
@mattwynne
Copy link
Member

@tooky this is fixed by #760 right?

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants