Skip to content

Commit a6a2df2

Browse files
author
Sam Phippen
committed
Prevent users from mutating the extra_params hash
Instead they should always use the `extra_params=` writer so that the path info gets updated.
1 parent 6399326 commit a6a2df2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/rspec/rails/view_spec_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def extra_params=(hash)
3939
# # => { :id => 4 }
4040
def extra_params
4141
@extra_params ||= {}
42+
@extra_params.dup.freeze
4243
end
4344
end
4445
end

spec/rspec/rails/view_spec_methods_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ class ::VCSampleClass; end
1616
expect(VCSampleClass.instance_methods.map(&:to_sym)).to(include(:extra_params=))
1717
expect(VCSampleClass.instance_methods.map(&:to_sym)).to(include(:extra_params))
1818
end
19+
20+
describe "the added #extra_params reader" do
21+
it "raises an error when a user tries to mutate it" do
22+
ViewSpecMethods.add_to(VCSampleClass)
23+
24+
expect {
25+
VCSampleClass.new.extra_params[:id] = 4
26+
}.to raise_error(RuntimeError)
27+
end
28+
end
1929
end
2030

2131
describe "when accessors are already defined" do

0 commit comments

Comments
 (0)