Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created September 25, 2008 19:55
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 agibralter/12923 to your computer and use it in GitHub Desktop.
Save agibralter/12923 to your computer and use it in GitHub Desktop.
add named_scopes to mock_models
def scopify(model_class, *scopes)
mocks = [model_class]
scopes.each do |scope|
mocks << mock(scope.to_s)
end
mocks.each do |mock|
mocks.delete_if { |m| m == mock || m == model_class }.each do |m|
mock.stub!(m.instance_variable_get(:@name)).and_return(model_class)
end
end
end
# so that this spec:
@post = mock_model(Post)
scopify(Post, :published, :tagged_with, :by_date)
Post.stub!(:paginate).and_return([@post])
# works with:
@posts = Post.published.tagged_with("commentary", :on => :category).by_date.paginate(:page => params[:page], :per_page => 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment