Sha256: daedad91d03d7e493ec63f38c81622ed22898181198708910342da171c8d7e5e

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module ClassAction
  module RSpec

    class RespondToFormatMatcher

      def initialize(format)
        @format = format.to_sym
      end

      def on(condition)
        @condition = condition
        self
      end

      def matches?(action, &block)
        @action = action

        if action.class._responders.key?([@format, @condition])

          if block
            # Response defined, we return true but we need to also execute the block,
            # as it might contain additional checks. First run the action's response
            # block, for this.
            respond_block = action.class._responders[ [@format, @condition] ]
            action.instance_exec &respond_block if respond_block
            action.send :copy_assigns_to_controller
            block.call
          end

          true
        else
          false
        end
      end

      def description
        if @condition
          "respond to format :#{@format} on :#{@condition}"
        else
          "respond to format :#{@format}"
        end
      end

      def failure_message
        if @condition
          "expected action of class #{@action.class} to respond to format :#{@format} on :#{@condition}"
        else
          "expected action of class #{@action.class} to respond to format :#{@format}"
        end
      end

      def failure_message_when_negated
        if @condition
          "expected action of class #{@action.class} not to respond to format :#{@format} on :#{@condition}"
        else
          "expected action of class #{@action.class} not to respond to format :#{@format}"
        end
      end

    end

  end
end

RSpec::Matchers.module_eval do
  def respond_to_format(format)
    ClassAction::RSpec::RespondToFormatMatcher.new(format)
  end
  def respond_to_any_format
    ClassAction::RSpec::RespondToFormatMatcher.new(:any)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
class-action-1.3.2 lib/class_action/rspec/respond_to_format_matcher.rb
class-action-1.3.1 lib/class_action/rspec/respond_to_format_matcher.rb