module Quacky::Expectations

Public Instance Methods

expect(method_name, return_value, with=[]) click to toggle source
# File lib/quacky/minitest_setup.rb, line 19
def expect method_name, return_value, with=[]
  should_receive(method_name).and_return(return_value).tap do |expectation|
    expectation.with(*with) unless with.empty?
  end
end
quacky_stub(method_name, &block) click to toggle source
# File lib/quacky/expectations.rb, line 7
def quacky_stub method_name, &block
  setup_expectation method_name, &block
end
Also aliased as: stub
should_receive(method_name) click to toggle source
# File lib/quacky/expectations.rb, line 11
def should_receive method_name
  quacky_stub(method_name).tap do |expectation|
    Quacky.expectations << expectation
  end
end
stub(method_name, return_value, with=[]) click to toggle source
# File lib/quacky/minitest_setup.rb, line 25
def stub method_name, return_value, with=[]
  quacky_stub(method_name).and_return(return_value).tap do |expectation|
    expectation.with(*with) unless with.empty?
  end
end

Private Instance Methods

parse_method_name(method_name) click to toggle source
# File lib/quacky/expectations.rb, line 43
def parse_method_name method_name
  method_name = method_name.to_s
  eol_matcher = /([\!\?])$/
  method_name_postpend = method_name.to_s.match(eol_matcher) ? $1 : ""
  method_name_minus_postpend = method_name.to_s.gsub eol_matcher, ""
  [method_name_minus_postpend, method_name_postpend]
end
quacky_expectations() click to toggle source
# File lib/quacky/expectations.rb, line 19
def quacky_expectations
  @expectations ||= {}
end
setup_expectation(method_name, &block) click to toggle source
# File lib/quacky/expectations.rb, line 23
    def setup_expectation method_name, &block
      method_name = method_name.to_sym
      raise Quacky::NoMethodError, "#{inspect} does not define `#{method_name}'" unless respond_to?(method_name)

      quacky_expectations[method_name] = Stub.new(public_method(method_name), &block)
      sanitized_name, postpend = parse_method_name method_name

      eval <<-EVAL
        class << self
          define_method("#{sanitized_name}_with_expectation#{postpend}") do |*args|
            quacky_expectations[:#{method_name}].call *args
          end

          alias_method_chain :#{method_name}, :expectation
        end
      EVAL

      quacky_expectations[method_name]
    end