Skip to content

Instantly share code, notes, and snippets.

@BenEddy
Created August 27, 2013 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenEddy/6348323 to your computer and use it in GitHub Desktop.
Save BenEddy/6348323 to your computer and use it in GitHub Desktop.
class MockActiveRelation < Hash
attr_reader :mocked_scopes
def initialize(*mocked_scopes)
@mocked_scopes = mocked_scopes
super()
end
def scoped?
any?
end
def scoped_by?(scope, *args)
fetch(scope, []).include?(args)
end
private
def mocks_scope?(scope)
mocked_scopes.include?(scope)
end
def record_invocation(scope, *args)
clone.tap do |clone|
clone[scope] = fetch(scope, []).push(args)
end
end
def method_missing(scope, *args)
mocks_scope?(scope) ? record_invocation(scope, *args) : super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment