Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Fix block support in example.duplicate_with #2298

Merged
merged 1 commit into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/rspec/core/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ def duplicate_with(metadata_overrides={})

# don't clone the example group because the new example
# must belong to the same example group (not a clone).
#
# block is nil in new_metadata so we have to get it from metadata.
Example.new(example_group, description.clone,
new_metadata, new_metadata[:block])
new_metadata, metadata[:block])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to do metadata[:block].clone, as thats what new_metadata is, but somehow it's missing the block from its clone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's missing because of the code on lines 135-137 that deletes the reserved keys.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, I think it still makes sense to clone it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, blocks are immutable, so I'm not sure what cloning it does for us other than taking more memory and time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, LGTM then

end

# @private
Expand Down
6 changes: 5 additions & 1 deletion spec/rspec/core/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ def metadata_hash(*args)

describe '#duplicate_with' do
it 'successfully duplicates an example' do
example = example_group.example { raise 'first' }
error_string = 'first'
example = example_group.example { raise error_string }
example2 = example.duplicate_with({ :custom_key => :custom_value })

# ensure metadata is unique for each example
expect(example.metadata.object_id).to_not eq(example2.metadata.object_id)
expect(example.metadata[:custom_key]).to eq(nil)
expect(&example.metadata[:block]).to raise_error(error_string)

expect(example2.metadata[:custom_key]).to eq(:custom_value)
expect(&example2.metadata[:block]).to raise_error(error_string)

# cloned examples must have unique ids
expect(example.id).to_not eq(example2.id)
Expand Down