Skip to content

Instantly share code, notes, and snippets.

@jemmyw
Created September 7, 2009 00:30
Show Gist options
  • Save jemmyw/182070 to your computer and use it in GitHub Desktop.
Save jemmyw/182070 to your computer and use it in GitHub Desktop.
class Class
def should_receive_squirrel_find_with(expected)
FindWithSquirrel.new(self, expected)
end
end
class FindWithSquirrel
include Spec::Matchers
def initialize(klass, expected)
@klass = klass
@expected = expected
$rspec_mocks.add(self) unless $rspec_mocks.nil?
extend
end
def and_return(value)
@return = value
@klass.instance_variable_set('@find_with_return', @return)
end
def extend
@klass.class_eval %Q{
def self.find_with_finds
@find_with_finds ||= []
end
def self.find_with_find_with(*args, &blk)
find_with_finds << find_without_find_with(:query, &blk).to_find_conditions
if @find_with_return
@find_with_return
else
find_without_find_with(*args, &blk)
end
end
class << self
alias_method :find_without_find_with, :find
alias_method :find, :find_with_find_with
end
}
end
def unextend
@klass.class_eval %Q{
class << self
alias_method :find, :find_without_find_with
end
}
end
alias_method :rspec_reset, :unextend
def finds
@klass.find_with_finds
end
def verify
finds.should include(@expected)
end
alias_method :rspec_verify, :verify
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment