Can I use RSpec algorithms?

I have these two matches which are identical from each other:

RSpec::Matchers.define :be_the_downcased_version_of do |actual|
    match do |actual|
        @actual = actual
        @expected = expected

        @actual == @expected.downcase
    end

    failure_message do |actual|
        "Expected #{@actual.inspect} to be the downcased version of #{expected.inspect}.\nIt was not: #{@actual.inspect}"
    end

    failure_message_when_negated do |actual|
        "Expected #{@actual.inspect} to not be the downcased version of #{expected.inspect}.\nIt was: #{@actual.inspect}"
    end

    description do
        "be the downcased version of #{expected.inspect}"
    end
end

RSpec::Matchers.define :return_the_downcased_version_of do |actual|
    match do |actual|
        @actual = actual
        @expected = expected

        @actual == @expected.downcase
    end

    failure_message do |actual|
        "Expected #{@actual.inspect} to be the downcased version of #{expected.inspect}.\nIt was not: #{@actual.inspect}"
    end

    failure_message_when_negated do |actual|
        "Expected #{@actual.inspect} to not be the downcased version of #{expected.inspect}.\nIt was: #{@actual.inspect}"
    end

    description do
        "be the downcased version of #{expected.inspect}"
    end
end

      

Can I pseudonize them, for example when smoothing a method with Ruby?

alias_method :foo_meth, :bar_meth

      

+3


source to share


1 answer


Yes, you can use an alias that is defined using the RSpec DSL interface. The most convenient way I know is to reopen the example context class in the block before

and the method alias there.

But clean up your assistant first.

  • The block passed to Matchers.define

    takes the expected value, not the actual value as an argument.
  • You don't need these instance variables; their values ​​are already available in block arguments.

In spec / support / be_the_downcased_version_of.rb:

RSpec::Matchers.define :be_the_downcased_version_of do |expected|
  match do |actual|
    actual == expected.downcase
  end

  failure_message_for_should do |actual|
    "Expected #{actual.inspect} to be the downcased version of #{expected.inspect}.\nIt was not: #{actual.inspect}"
  end

  failure_message_for_should_not do |actual|
    "Expected #{actual.inspect} to not be the downcased version of #{expected.inspect}.\nIt was: #{actual.inspect}"
  end

  description do
    "be the downcased version of #{expected.inspect}"
  end

end

      



For the alias of the method specified by this match, in spec / spec_helper.rb:

config.before :each do
  class << self
    alias_method :return_the_downcased_version_of, :be_the_downcased_version_of
  end
end

      

Here's an alternative approach. I wouldn't do that because it requires more boilerplate, but it uses less magic than the previous approach, so it can be informative. Just implement the non-DSL pairing and usually call the method normal. In spec / support / be_the_downcased_version_of.rb:

alias_method :return_the_downcased_version_of, :be_the_downcased_version_of

def be_the_downcased_version_of(actual)
  BeTheDowncasedVersionOf.new actual
end

class BeTheDowncasedVersionOf
  def initialize(expected)
    @expected = expected
  end

  def matches?(actual)
    @actual = actual
    @actual == @expected.downcase
  end

  def failure_message_for_should
    "Expected #{@actual.inspect} to be the downcased version of #{@expected.inspect}.\nIt was not: #{@actual.inspect}"
  end

  def failure_message_for_should_not
    "Expected #{@actual.inspect} to not be the downcased version of #{@expected.inspect}.\nIt was: #{@actual.inspect}"
  end

  def description
    "be the downcased version of #{@expected.inspect}"
  end

end

      

+2


source







All Articles