diff --git a/.rubocop.yml b/.rubocop.yml index 6498e4f..2f39665 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,7 +14,7 @@ AllCops: - bin/* - gemfiles/* - benchmarks/* - - spec/**/* + - spec/dummy/**/* Gemspec/RequireMFA: Enabled: false @@ -35,6 +35,9 @@ Style/StringLiterals: Style/ArgumentsForwarding: Enabled: false +Style/BlockDelimiters: + AllowedPatterns: ['expect'] + ########## # LAYOUT # ########## @@ -66,3 +69,25 @@ Layout/IndentationConsistency: Naming/BlockForwarding: Enabled: false + +######### +# RSPEC # +######### + +RSpec/MultipleExpectations: + Max: 5 + +RSpec/NestedGroups: + Max: 4 + +RSpec/ExampleLength: + Enabled: false + +RSpec/VerifiedDoubles: + Enabled: false + +RSpec/MetadataStyle: + EnforcedStyle: hash + +FactoryBot/SyntaxMethods: + Enabled: false diff --git a/spec/config_capybara.rb b/spec/config_capybara.rb index 0fde836..c58710b 100644 --- a/spec/config_capybara.rb +++ b/spec/config_capybara.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def register_driver(driver_name, args = []) opts = { js_errors: true, headless: true, window_size: [1920, 1200], browser_options: {} } args.each do |arg| diff --git a/spec/config_rspec.rb b/spec/config_rspec.rb index c461e63..da6e099 100644 --- a/spec/config_rspec.rb +++ b/spec/config_rspec.rb @@ -1,9 +1,12 @@ +# frozen_string_literal: true + # Configure RSpec RSpec.configure do |config| config.include Capybara::DSL + config.include HaveTextMatcher # Use DB agnostic schema by default - load Rails.root.join('db', 'schema.rb').to_s + load Rails.root.join("db", "schema.rb").to_s config.order = :random Kernel.srand config.seed @@ -19,7 +22,7 @@ config.use_transactional_fixtures = true # run retry only on features - config.around :each, :js do |ex| + config.around :each, js: true do |ex| ex.run_with_retry retry: 3 end @@ -31,11 +34,11 @@ DatabaseCleaner.strategy = :transaction end - config.before(:each) do + config.before do DatabaseCleaner.start end - config.after(:each) do + config.after do DatabaseCleaner.clean end end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index ee0f58e..f24c73c 100644 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -1,4 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true + +require "spec_helper" RSpec.describe Draper::Decorator do @@ -115,10 +117,10 @@ describe "#attributes" do it "returns only the object's attributes that are implemented by the decorator" do - decorator = described_class.new(double(attributes: {foo: "bar", baz: "qux"})) + decorator = described_class.new(double(attributes: { foo: "bar", baz: "qux" })) allow(decorator).to receive(:foo) - expect(decorator.attributes).to eq({foo: "bar"}) + expect(decorator.attributes).to eq({ foo: "bar" }) end end diff --git a/spec/draper/draper_spec.rb b/spec/draper/draper_spec.rb index 41ef70c..d32b31d 100644 --- a/spec/draper/draper_spec.rb +++ b/spec/draper/draper_spec.rb @@ -1,4 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true + +require "spec_helper" RSpec.describe Draper do describe ".decorate" do diff --git a/spec/draper/view_context/build_strategy_spec.rb b/spec/draper/view_context/build_strategy_spec.rb index 9c02e58..3819d3d 100644 --- a/spec/draper/view_context/build_strategy_spec.rb +++ b/spec/draper/view_context/build_strategy_spec.rb @@ -1,4 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true + +require "spec_helper" RSpec.describe "ViewContext" do def fake_view_context @@ -15,7 +17,7 @@ def fake_controller(view_context = fake_view_context) it "returns the controller's view context" do view_context = fake_view_context allow(Draper::ViewContext).to receive_messages controller: fake_controller(view_context) - strategy = Draper::ViewContext::BuildStrategy::Full.new + strategy = described_class.new expect(strategy.call).to be view_context end @@ -24,7 +26,7 @@ def fake_controller(view_context = fake_view_context) context "when a current controller is not set" do it "uses ApplicationController" do expect(Draper::ViewContext.controller).to be_nil - view_context = Draper::ViewContext::BuildStrategy::Full.new.call + view_context = described_class.new.call expect(view_context.controller).to eq Draper::ViewContext.controller expect(view_context.controller).to be_an ApplicationController end @@ -33,7 +35,7 @@ def fake_controller(view_context = fake_view_context) it "adds a request if one is not defined" do controller = Class.new(ActionController::Base).new allow(Draper::ViewContext).to receive_messages controller: controller - strategy = Draper::ViewContext::BuildStrategy::Full.new + strategy = described_class.new expect(controller.request).to be_nil strategy.call @@ -48,14 +50,14 @@ def fake_controller(view_context = fake_view_context) it "compatible with rails 5.1 change on ActionController::TestRequest.create method" do ActionController::TestRequest.class_eval do if ActionController::TestRequest.method(:create).parameters.first == [] - def create controller_class + def create(_controller_class) create end end end controller = Class.new(ActionController::Base).new allow(Draper::ViewContext).to receive_messages controller: controller - strategy = Draper::ViewContext::BuildStrategy::Full.new + strategy = described_class.new expect(controller.request).to be_nil strategy.call @@ -64,7 +66,7 @@ def create controller_class it "adds methods to the view context from the constructor block" do allow(Draper::ViewContext).to receive(:controller).and_return(fake_controller) - strategy = Draper::ViewContext::BuildStrategy::Full.new do + strategy = described_class.new do def a_helper_method; end end @@ -77,7 +79,7 @@ def a_helper_method; end helpers = Module.new do def a_helper_method; end end - strategy = Draper::ViewContext::BuildStrategy::Full.new do + strategy = described_class.new do include helpers end @@ -89,7 +91,7 @@ def a_helper_method; end describe Draper::ViewContext::BuildStrategy::Fast do describe "#call" do it "returns an instance of a subclass of ActionView::Base" do - strategy = Draper::ViewContext::BuildStrategy::Fast.new + strategy = described_class.new returned = strategy.call @@ -98,19 +100,19 @@ def a_helper_method; end end it "returns different instances each time" do - strategy = Draper::ViewContext::BuildStrategy::Fast.new + strategy = described_class.new expect(strategy.call).not_to be strategy.call end it "returns the same subclass each time" do - strategy = Draper::ViewContext::BuildStrategy::Fast.new + strategy = described_class.new - expect(strategy.call.class).to be strategy.call.class + expect(strategy.call.class).to be strategy.call.class # rubocop:disable RSpec/IdenticalEqualityAssertion end it "adds methods to the view context from the constructor block" do - strategy = Draper::ViewContext::BuildStrategy::Fast.new do + strategy = described_class.new do def a_helper_method; end end @@ -121,7 +123,7 @@ def a_helper_method; end helpers = Module.new do def a_helper_method; end end - strategy = Draper::ViewContext::BuildStrategy::Fast.new do + strategy = described_class.new do include helpers end diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index 03661f7..4bd058e 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -1,4 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true + +require "spec_helper" RSpec.describe Draper::ViewContext do @@ -6,23 +8,23 @@ it "returns the stored view context from RequestStore" do allow(RequestStore).to receive_messages store: { current_view_context: :stored_view_context } - expect(Draper::ViewContext.current).to be :stored_view_context + expect(described_class.current).to be :stored_view_context end context "when no view context is stored" do it "builds a view context" do allow(RequestStore).to receive_messages store: {} - allow(Draper::ViewContext).to receive_messages build_strategy: -> { :new_view_context } + allow(described_class).to receive_messages build_strategy: -> { :new_view_context } - expect(Draper::ViewContext.current).to be :new_view_context + expect(described_class.current).to be :new_view_context end it "stores the built view context" do store = {} allow(RequestStore).to receive_messages store: store - allow(Draper::ViewContext).to receive_messages build_strategy: -> { :new_view_context } + allow(described_class).to receive_messages build_strategy: -> { :new_view_context } - Draper::ViewContext.current + described_class.current expect(store[:current_view_context]).to be :new_view_context end end @@ -33,7 +35,7 @@ store = {} allow(RequestStore).to receive_messages store: store - Draper::ViewContext.current = :stored_view_context + described_class.current = :stored_view_context expect(store[:current_view_context]).to be :stored_view_context end end @@ -42,7 +44,7 @@ it "returns the stored controller from RequestStore" do allow(RequestStore).to receive_messages store: { current_controller: :stored_controller } - expect(Draper::ViewContext.controller).to be :stored_controller + expect(described_class.controller).to be :stored_controller end end @@ -51,7 +53,7 @@ store = {} allow(RequestStore).to receive_messages store: store - Draper::ViewContext.controller = :stored_controller + described_class.controller = :stored_controller expect(store[:current_controller]).to be :stored_controller end @@ -63,7 +65,7 @@ allow(RequestStore).to receive_messages store: store - Draper::ViewContext.controller = :other_stored_controller + described_class.controller = :other_stored_controller expect(store).to include(current_controller: :other_stored_controller) expect(store).not_to include(:current_view_context) @@ -77,7 +79,7 @@ allow(RequestStore).to receive_messages store: store - Draper::ViewContext.controller = :stored_controller + described_class.controller = :stored_controller expect(store).to include(current_controller: :stored_controller) expect(store).to include(current_view_context: :stored_view_context) @@ -86,25 +88,25 @@ describe ".build" do it "returns a new view context using the build strategy" do - allow(Draper::ViewContext).to receive_messages build_strategy: -> { :new_view_context } + allow(described_class).to receive_messages build_strategy: -> { :new_view_context } - expect(Draper::ViewContext.build).to be :new_view_context + expect(described_class.build).to be :new_view_context end end describe ".build!" do it "returns a helper proxy for the new view context" do - allow(Draper::ViewContext).to receive_messages build_strategy: -> { :new_view_context } + allow(described_class).to receive_messages build_strategy: -> { :new_view_context } - expect(Draper::ViewContext.build!).to be :new_view_context + expect(described_class.build!).to be :new_view_context end it "stores the helper proxy" do store = {} allow(RequestStore).to receive_messages store: store - allow(Draper::ViewContext).to receive_messages build_strategy: -> { :new_view_context } + allow(described_class).to receive_messages build_strategy: -> { :new_view_context } - Draper::ViewContext.build! + described_class.build! expect(store[:current_view_context]).to be :new_view_context end end @@ -114,7 +116,7 @@ store = { current_controller: :stored_controller, current_view_context: :stored_view_context } allow(RequestStore).to receive_messages store: store - Draper::ViewContext.clear! + described_class.clear! expect(store).not_to have_key :current_controller expect(store).not_to have_key :current_view_context end @@ -122,11 +124,11 @@ describe ".build_strategy" do it "defaults to full" do - expect(Draper::ViewContext.build_strategy).to be_a Draper::ViewContext::BuildStrategy::Full + expect(described_class.build_strategy).to be_a Draper::ViewContext::BuildStrategy::Full end it "memoizes" do - expect(Draper::ViewContext.build_strategy).to be Draper::ViewContext.build_strategy + expect(described_class.build_strategy).to be described_class.build_strategy # rubocop:disable RSpec/IdenticalEqualityAssertion end end end diff --git a/spec/features/draper/draper_spec.rb b/spec/features/draper/draper_spec.rb index c02c728..ec8d3ed 100644 --- a/spec/features/draper/draper_spec.rb +++ b/spec/features/draper/draper_spec.rb @@ -1,6 +1,8 @@ -require 'spec_helper' +# frozen_string_literal: true -RSpec.feature 'Draper', js: true do +require "spec_helper" + +RSpec.feature "Draper", js: true do spec_types = { view: ["/decorated_posts/1", "DecoratorController"], mailer: ["/decorated_posts/1/mail", "PostMailer"] @@ -35,8 +37,8 @@ spec_types.each do |type, (path, controller)| describe "in a #{type}" do - before(:each) do - FactoryBot.create(:comment, content: 'foo') + before do + FactoryBot.create(:comment, content: "foo") visit path end @@ -61,15 +63,15 @@ expect(page).to have_text("Hello, world!").in("#hello_world") end - it "can use user-defined helpers from the controller" do + it "can use user-defined helpers from the controller A" do expect(page).to have_text("Goodnight, moon!").in("#goodnight_moon") end - it "can use user-defined helpers from the controller" do + it "can use user-defined helpers from the controller B" do expect(page).to have_text("foo").in("#comments_content") end - it "can use user-defined helpers from the controller" do + it "can use user-defined helpers from the controller C" do expect(page).to have_text("Today").in("#comments_date") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b882571..46e4cc4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,33 +1,33 @@ # frozen_string_literal: true -require 'simplecov' +require "simplecov" # Start SimpleCov SimpleCov.start do - add_filter 'spec/' + add_filter "spec/" end # Load Rails dummy app -ENV['RAILS_ENV'] = 'test' -require File.expand_path('dummy/config/environment.rb', __dir__) +ENV["RAILS_ENV"] = "test" +require File.expand_path("dummy/config/environment.rb", __dir__) # Load test gems -require 'rspec/rails' -require 'capybara/rspec' -require 'capybara/cuprite' -require 'database_cleaner' -require 'factory_bot' -require 'faker' -require 'rspec/retry' - -# Load our own config -require_relative 'config_capybara' -require_relative 'config_rspec' +require "rspec/rails" +require "capybara/rspec" +require "capybara/cuprite" +require "database_cleaner" +require "factory_bot" +require "faker" +require "rspec/retry" # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[File.expand_path('support/**/*.rb', __dir__)].each { |f| require f } +Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require f } + +# Load our own config +require_relative "config_capybara" +require_relative "config_rspec" -class Model; end +class Model; end # rubocop:disable Lint/EmptyClass class Product < Model; end class ProductDecorator < Draper::Decorator; end diff --git a/spec/support/matchers/have_text.rb b/spec/support/matchers/have_text.rb index fcaa89b..cc4849f 100644 --- a/spec/support/matchers/have_text.rb +++ b/spec/support/matchers/have_text.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module HaveTextMatcher def have_text(text) HaveText.new(text) @@ -29,20 +31,17 @@ def failure_message_when_negated private - def within - if @css && @subject.has_css?(@css) - "within\n#{@subject.find(@css).native}" - else - inside + def within + if @css && @subject.has_css?(@css) + "within\n#{@subject.find(@css).native}" + else + inside + end end - end - def inside - @css ? "inside #{@css.inspect}" : "anywhere" - end - end -end + def inside + @css ? "inside #{@css.inspect}" : "anywhere" + end -RSpec.configure do |config| - config.include HaveTextMatcher + end end