Skip to content

Commit

Permalink
Fix frozen string literal warning in InstanceMethod
Browse files Browse the repository at this point in the history
I've mostly fixed this by using `Object.new` instead of instances of
`String` to act as partial mocks.

I've also had to disable one test which is specifically stubbing a
method on an instance of `String` when the Ruby version is v3.4 or
higher. This is OK, because we already have behaviour in place to
prevent stubbing of a method on a frozen object [1,2], so the behaviour
under test would be moot at that point.

Previously I was seeing a lot of the following warnings when running
the tests under Ruby v3.4:

    lib/mocha/instance_method.rb:16: warning: literal string will be frozen in the future

[1]: https://github.com/freerange/mocha/blob/5c7d14cb7d779bd22da3dbfe9d16ed1091ace7a1/lib/mocha/object_methods.rb#L75-L77
[2]: https://github.com/freerange/mocha/blob/5c7d14cb7d779bd22da3dbfe9d16ed1091ace7a1/lib/mocha/object_methods.rb#L121-L123
  • Loading branch information
floehopper committed Jul 30, 2024
1 parent 55dd0fc commit ce2ae26
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
10 changes: 6 additions & 4 deletions test/acceptance/failure_messages_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ def test_should_display_mock_address_when_expectation_was_on_unnamed_mock
assert_match Regexp.new("#<Mock:#{OBJECT_ADDRESS_PATTERN}>"), test_result.failures[0].message
end

def test_should_display_string_when_expectation_was_on_string
test_result = run_as_test do
'Foo'.expects(:bar)
unless Mocha::RUBY_V34_PLUS
def test_should_display_string_when_expectation_was_on_string
test_result = run_as_test do
'Foo'.expects(:bar)
end
assert_match Regexp.new(%("Foo")), test_result.failures[0].message
end
assert_match Regexp.new(%("Foo")), test_result.failures[0].message
end

def test_should_display_that_block_was_expected
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/partial_mocks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def teardown

def test_should_pass_if_all_expectations_are_satisfied
test_result = run_as_test do
partial_mock_one = 'partial_mock_one'
partial_mock_two = 'partial_mock_two'
partial_mock_one = Object.new
partial_mock_two = Object.new

partial_mock_one.expects(:first)
partial_mock_one.expects(:second)
Expand All @@ -29,8 +29,8 @@ def test_should_pass_if_all_expectations_are_satisfied

def test_should_fail_if_all_expectations_are_not_satisfied
test_result = run_as_test do
partial_mock_one = 'partial_mock_one'
partial_mock_two = 'partial_mock_two'
partial_mock_one = Object.new
partial_mock_two = Object.new

partial_mock_one.expects(:first)
partial_mock_one.expects(:second)
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/sequence_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_should_allow_invocations_in_sequence_even_if_expected_on_different_mock

def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new

sequence('one') do
partial_mock_one.expects(:first)
Expand All @@ -93,8 +93,8 @@ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expecte

def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new

sequence('one') do
partial_mock_one.expects(:first)
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/sequence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def test_should_allow_invocations_in_sequence_even_if_expected_on_different_mock

def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new
sequence = sequence('one')

partial_mock_one.expects(:first).in_sequence(sequence)
Expand All @@ -88,8 +88,8 @@ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expecte

def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new
sequence = sequence('one')

partial_mock_one.expects(:first).in_sequence(sequence)
Expand Down

0 comments on commit ce2ae26

Please sign in to comment.