rstormsf
rstormsf

Reputation: 547

How to call a dynamic Rspec matcher should or should_not

def some_method condition
  actual = [1,2,3]
  expected = include(1)
  matcher = lambda {|condition|
            if condition == "YES"
              return RSpec::Matcher.should
            else
              return RSpec::Matcher.should_not
            end}
  actual.matcher.call(condition) expected
end

How to dynamically make a should or should_not matcher based on condition parsed into the method?

Upvotes: 0

Views: 367

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

I think what you meant to do is:

def some_method condition
  actual = [1,2,3]
  expected = include(1)
  matcher = lambda {|condition|
            if condition == "YES"
              :should
            else
              :should_not
            end}
  actual.send(&matcher.call(condition), expected)
end

This will either call the method should or should_not on the actual result.

On a side note, I'm having quite a difficulty seeing when this would be a good idea... Unit tests should be deterministic and readable, and this idea makes them less of both...

Upvotes: 1

Related Questions