Created
September 25, 2008 19:55
-
-
Save agibralter/12923 to your computer and use it in GitHub Desktop.
add named_scopes to mock_models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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