Lint: Fix various offenses, mostly rspec

- Fix Lint/RescueWithoutErrorClass
  - And I use the term "fix" loosely
- Fix RSpec/PredicateMatcher
  - The failure message should actually be better this way too.
- RSpec/LetBeforeExamples
- RSpec/HookArgument
- Fix RSpec/ExpectInHook in widget_spec.rb
- Fix RSpec/MessageSpies
  - I like spies. I've never linted them before, but I like how they
    separate test setup from assertions.
This commit is contained in:
Jared Beck 2017-09-18 23:56:07 -04:00
parent b2bf3dd347
commit df4f3bba4f
8 changed files with 28 additions and 32 deletions

View File

@ -3,9 +3,6 @@ require: rubocop-rspec
# Remove these configuration records # Remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
Lint/RescueWithoutErrorClass:
Enabled: false
Metrics/AbcSize: Metrics/AbcSize:
Max: 22 # Goal: 15 Max: 22 # Goal: 15
@ -22,24 +19,21 @@ RSpec/BeforeAfterAll:
Enabled: false Enabled: false
RSpec/ExpectInHook: RSpec/ExpectInHook:
Enabled: false Exclude:
- spec/paper_trail/associations_spec.rb
- spec/models/version_spec.rb
RSpec/FilePath: RSpec/FilePath:
Enabled: false Enabled: false
RSpec/HookArgument:
Enabled: false
RSpec/InstanceVariable: RSpec/InstanceVariable:
Exclude: Exclude:
- spec/paper_trail/associations_spec.rb - spec/paper_trail/associations_spec.rb
- spec/paper_trail/model_spec.rb - spec/paper_trail/model_spec.rb
RSpec/LetBeforeExamples:
Enabled: false
RSpec/MessageSpies: RSpec/MessageSpies:
Enabled: false Exclude:
- spec/models/version_spec.rb
RSpec/NamedSubject: RSpec/NamedSubject:
Enabled: false Enabled: false
@ -49,8 +43,5 @@ RSpec/NestedGroups:
- spec/paper_trail/associations_spec.rb - spec/paper_trail/associations_spec.rb
- spec/paper_trail/model_spec.rb - spec/paper_trail/model_spec.rb
RSpec/PredicateMatcher:
Enabled: false
Security/YAMLLoad: Security/YAMLLoad:
Enabled: false Enabled: false

View File

@ -170,7 +170,7 @@ module PaperTrail
def next_version def next_version
subsequent_version = source_version.next subsequent_version = source_version.next
subsequent_version ? subsequent_version.reify : @record.class.find(@record.id) subsequent_version ? subsequent_version.reify : @record.class.find(@record.id)
rescue # TODO: Rescue something more specific rescue StandardError # TODO: Rescue something more specific
nil nil
end end

View File

@ -162,7 +162,7 @@ module PaperTrail
def primary_key_is_int? def primary_key_is_int?
@primary_key_is_int ||= columns_hash[primary_key].type == :integer @primary_key_is_int ||= columns_hash[primary_key].type == :integer
rescue rescue StandardError # TODO: Rescue something more specific
true true
end end
@ -307,7 +307,7 @@ module PaperTrail
else else
begin begin
PaperTrail.serializer.load(object_changes) PaperTrail.serializer.load(object_changes)
rescue # TODO: Rescue something specific rescue StandardError # TODO: Rescue something more specific
{} {}
end end
end end

View File

@ -1,10 +1,10 @@
require "spec_helper" require "spec_helper"
RSpec.describe Gadget, type: :model do RSpec.describe Gadget, type: :model do
it { is_expected.to be_versioned }
let(:gadget) { Gadget.create!(name: "Wrench", brand: "Acme") } let(:gadget) { Gadget.create!(name: "Wrench", brand: "Acme") }
it { is_expected.to be_versioned }
describe "updates", versioning: true do describe "updates", versioning: true do
it "generates a version for updates to `name` attribute" do it "generates a version for updates to `name` attribute" do
expect { gadget.update_attribute(:name, "Hammer") }.to(change { gadget.versions.size }.by(1)) expect { gadget.update_attribute(:name, "Hammer") }.to(change { gadget.versions.size }.by(1))

View File

@ -65,15 +65,16 @@ module PaperTrail
it "sets the invoke `paper_trail_originator`" do it "sets the invoke `paper_trail_originator`" do
allow(ActiveSupport::Deprecation).to receive(:warn) allow(ActiveSupport::Deprecation).to receive(:warn)
subject = PaperTrail::Version.new subject = PaperTrail::Version.new
expect(subject).to receive(:paper_trail_originator) allow(subject).to receive(:paper_trail_originator)
subject.originator subject.originator
expect(subject).to have_received(:paper_trail_originator)
end end
it "displays a deprecation warning" do it "displays a deprecation warning" do
expect(ActiveSupport::Deprecation).to receive(:warn). allow(ActiveSupport::Deprecation).to receive(:warn)
PaperTrail::Version.new.originator
expect(ActiveSupport::Deprecation).to have_received(:warn).
with(/Use paper_trail_originator instead of originator/) with(/Use paper_trail_originator instead of originator/)
subject = PaperTrail::Version.new
subject.originator
end end
end end

View File

@ -1,12 +1,12 @@
require "spec_helper" require "spec_helper"
RSpec.describe Widget, type: :model do RSpec.describe Widget, type: :model do
let(:widget) { Widget.create! name: "Bob", an_integer: 1 }
describe "`be_versioned` matcher" do describe "`be_versioned` matcher" do
it { is_expected.to be_versioned } it { is_expected.to be_versioned }
end end
let(:widget) { Widget.create! name: "Bob", an_integer: 1 }
describe "`have_a_version_with` matcher", versioning: true do describe "`have_a_version_with` matcher", versioning: true do
before do before do
widget.update_attributes!(name: "Leonard", an_integer: 1) widget.update_attributes!(name: "Leonard", an_integer: 1)
@ -145,8 +145,9 @@ RSpec.describe Widget, type: :model do
it "does not clobber the IdentityMap when reifying" do it "does not clobber the IdentityMap when reifying" do
widget.update_attributes name: "Henry", created_at: Time.now - 1.day widget.update_attributes name: "Henry", created_at: Time.now - 1.day
widget.update_attributes name: "Harry" widget.update_attributes name: "Harry"
expect(ActiveRecord::IdentityMap).to receive(:without).once allow(ActiveRecord::IdentityMap).to receive(:without)
widget.versions.last.reify widget.versions.last.reify
expect(ActiveRecord::IdentityMap).to have_receive(:without).once
end end
end end
end end
@ -249,10 +250,11 @@ RSpec.describe Widget, type: :model do
before do before do
PaperTrail.whodunnit = orig_name PaperTrail.whodunnit = orig_name
expect(widget.versions.last.whodunnit).to eq(orig_name) # persist `widget` widget # persist `widget` (call the `let`)
end end
it "modifies value of `PaperTrail.whodunnit` while executing the block" do it "modifies value of `PaperTrail.whodunnit` while executing the block" do
expect(widget.versions.last.whodunnit).to eq(orig_name)
widget.paper_trail.whodunnit(new_name) do widget.paper_trail.whodunnit(new_name) do
expect(PaperTrail.whodunnit).to eq(new_name) expect(PaperTrail.whodunnit).to eq(new_name)
widget.update_attributes(name: "Elizabeth") widget.update_attributes(name: "Elizabeth")
@ -261,6 +263,7 @@ RSpec.describe Widget, type: :model do
end end
it "reverts value of whodunnit to previous value after executing the block" do it "reverts value of whodunnit to previous value after executing the block" do
expect(widget.versions.last.whodunnit).to eq(orig_name)
widget.paper_trail.whodunnit(new_name) { |w| widget.paper_trail.whodunnit(new_name) { |w|
w.update_attributes(name: "Elizabeth") w.update_attributes(name: "Elizabeth")
} }
@ -268,6 +271,7 @@ RSpec.describe Widget, type: :model do
end end
it "reverts to previous value, even if error within block" do it "reverts to previous value, even if error within block" do
expect(widget.versions.last.whodunnit).to eq(orig_name)
expect { expect {
widget.paper_trail.whodunnit(new_name) { raise } widget.paper_trail.whodunnit(new_name) { raise }
}.to raise_error(RuntimeError) }.to raise_error(RuntimeError)

View File

@ -84,9 +84,9 @@ RSpec.describe(::PaperTrail, versioning: true) do
end end
it "have versions that are not live" do it "have versions that are not live" do
expect( @widget.versions.map(&:reify).compact.each do |v|
@widget.versions.map(&:reify).compact.all? { |w| !w.paper_trail.live? } expect(v.paper_trail).not_to be_live
).to(be_truthy) end
end end
it "have stored changes" do it "have stored changes" do

View File

@ -72,7 +72,7 @@ RSpec.configure do |config|
# Truncation is about three times slower than transaction rollback, so it'll # Truncation is about three times slower than transaction rollback, so it'll
# be nice when we can drop support for rails < 5. # be nice when we can drop support for rails < 5.
if active_record_gem_version < ::Gem::Version.new("5") if active_record_gem_version < ::Gem::Version.new("5")
config.before(:each) { DatabaseCleaner.start } config.before { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean } config.after { DatabaseCleaner.clean }
end end
end end