Sha256: 07e0512c60efdd87238b3a7b38e3f1c63474c54abb0a770ad3c4e168d28c0ed4

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module ClassAction
  module RSpec

    class RespondWithMatcher

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

      def on(condition)
        @condition = condition
        self
      end

      def matches?(action)
        @action = action
        @actual = action.class._responses[@condition].try(:to_sym)
        @actual == @expected
      end

      def description
        if @condition
          "respond with method :#{@expected} on :#{@condition}"
        else
          "respond with method :#{@expected}"
        end
      end

      def failure_message
        suffix = if @actual
          ", but it responds with :#{@actual}"
        else
          ", but it has no response method"
        end

        if @condition
          "expected action of class #{@action.class} to respond with :#{@expected} on :#{@condition}#{suffix}"
        else
          "expected action of class #{@action.class} to respond with :#{@expected}#{suffix}"
        end
      end

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

    end

  end
end

RSpec::Matchers.module_eval do
  def respond_with(expected)
    ClassAction::RSpec::RespondWithMatcher.new(expected)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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