Sha256: dd5e045958d2f937062144431203248d3fd8348f864fe04b59dd45ee9e1dac2c

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

module Rails
  module AddOns
    module Shoulda
      module Matchers
        # Example:
        #
        #    RSpec.describe '/posts', type: :feature do
        #      let(:resource)       { create(:post) }
        #     
        #       it { expect(subject).to implement_show_action(self).for(resource) }
        #    end
        #
        def implement_show_action(spec)
          ImplementShowActionMatcher.new(spec)
        end

        class ImplementShowActionMatcher
          include RSpec::Matchers

          def initialize(spec)
            @spec = spec
          end

          # Resource that will be updated
          def for(resource)
            @resource = resource
            self
          end

          def id
            @resource.to_param
          end

          def matches?(base_path)
            @base_path     = base_path
            @expected_path = "#{@base_path}/#{id}"

            @spec.visit(@expected_path)
            
            has_correct_status_code && has_correct_current_path
          end

          def has_correct_status_code
            if @spec.status_code == 200
              true
            else
              @error = "Wrong status code [#{@spec.status_code}] instead of [200]"
              false
            end
          end

          def has_correct_current_path
            if @spec.current_path == @expected_path
              true
            else
              @error = "Wrong current path [#{@spec.current_path}] instead of [#{@expected_path}]"
              false
            end
          end

          def failure_message
            "Should expose show action on #{@base_path}. Error: #{@error}"
          end

          def failure_message_when_negated
            "Should not expose show action on #{@base_path}. Error: #{@error}"
          end

          alias negative_failure_message failure_message_when_negated

          def description
            "expose show action on #{@base_path}"
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-add_ons-2.2.1 lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb
rails-add_ons-2.2.0 lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb
rails-add_ons-2.1.1 lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb
rails-add_ons-2.1.0 lib/rails/add_ons/shoulda/matchers/implement_show_action_matcher.rb