|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe(RuboCop::Cop::Rails::AssertChangesSecondArgument, :config) do |
| 4 | + let(:message) do |
| 5 | + 'Use assert_changes to assert when an expression changes from and to specific values. ' \ |
| 6 | + 'Use assert_difference instead to assert when an expression changes by a specific delta. ' \ |
| 7 | + 'The second argument to assert_changes is the message emitted on assert failure.' |
| 8 | + end |
| 9 | + |
| 10 | + describe('offenses') do |
| 11 | + it 'adds offense when the second positional argument is an integer' do |
| 12 | + expect_offense(<<~RUBY) |
| 13 | + assert_changes @value, -1 do |
| 14 | + ^^^^^^^^^^^^^^ #{message} |
| 15 | + @value += 1 |
| 16 | + end |
| 17 | + RUBY |
| 18 | + end |
| 19 | + |
| 20 | + it 'adds offense when the second positional argument is a float' do |
| 21 | + expect_offense(<<~RUBY) |
| 22 | + assert_changes @value, -1.0 do |
| 23 | + ^^^^^^^^^^^^^^ #{message} |
| 24 | + @value += 1 |
| 25 | + end |
| 26 | + RUBY |
| 27 | + end |
| 28 | + |
| 29 | + it 'does not add offense when the second argument is a string' do |
| 30 | + expect_no_offenses(<<~RUBY) |
| 31 | + assert_changes @value, "Value should change" do |
| 32 | + @value += 1 |
| 33 | + end |
| 34 | + RUBY |
| 35 | + end |
| 36 | + |
| 37 | + it 'does not add offense when the second argument is an interpolated string' do |
| 38 | + expect_no_offenses(<<~RUBY) |
| 39 | + assert_changes @value, "\#{thing} should change" do |
| 40 | + @value += 1 |
| 41 | + end |
| 42 | + RUBY |
| 43 | + end |
| 44 | + |
| 45 | + it 'does not add offense when the second argument is a symbol' do |
| 46 | + expect_no_offenses(<<~RUBY) |
| 47 | + assert_changes @value, :should_change do |
| 48 | + @value += 1 |
| 49 | + end |
| 50 | + RUBY |
| 51 | + end |
| 52 | + |
| 53 | + it 'does not add offense when the second argument is an interpolated symbol' do |
| 54 | + expect_no_offenses(<<~RUBY) |
| 55 | + assert_changes @value, :"\#{thing}_should_change" do |
| 56 | + @value += 1 |
| 57 | + end |
| 58 | + RUBY |
| 59 | + end |
| 60 | + |
| 61 | + it 'does not add offense when the second argument is a variable' do |
| 62 | + expect_no_offenses(<<~RUBY) |
| 63 | + message = "Value should change" |
| 64 | + assert_changes @value, message do |
| 65 | + @value += 1 |
| 66 | + end |
| 67 | + RUBY |
| 68 | + end |
| 69 | + |
| 70 | + it 'does not add offense when there is only one argument' do |
| 71 | + expect_no_offenses(<<~RUBY) |
| 72 | + assert_changes @value do |
| 73 | + @value += 1 |
| 74 | + end |
| 75 | + RUBY |
| 76 | + end |
| 77 | + |
| 78 | + it 'does not add offense when there is only one positional argument' do |
| 79 | + expect_no_offenses(<<~RUBY) |
| 80 | + assert_changes @value, from: 0 do |
| 81 | + @value += 1 |
| 82 | + end |
| 83 | + RUBY |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + describe('autocorrect') do |
| 88 | + it 'autocorrects method from assert_changes to assert_difference' do |
| 89 | + source = <<-RUBY |
| 90 | + assert_changes @value, -1.0 do |
| 91 | + @value += 1 |
| 92 | + end |
| 93 | + RUBY |
| 94 | + |
| 95 | + corrected_source = <<-RUBY |
| 96 | + assert_difference @value, -1.0 do |
| 97 | + @value += 1 |
| 98 | + end |
| 99 | + RUBY |
| 100 | + |
| 101 | + expect(autocorrect_source(source)).to(eq(corrected_source)) |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments