Skip to content

Instantly share code, notes, and snippets.

@pedrodelgallego
Created December 2, 2009 14:18
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 pedrodelgallego/247222 to your computer and use it in GitHub Desktop.
Save pedrodelgallego/247222 to your computer and use it in GitHub Desktop.
# Test
it 'finds the 5 most recent approved comments for the post' do
ret = [mock_model(Comment)]
comments = []
comments.should_receive(:find_recent).with(hash_including(:limit => 5)).and_return(ret)
post = mock_model(Post)
post.stub!(:approved_comments).and_return(comments)
CommentActivity.new(post).comments.should == ret
end
# $ spec spec/models/comment_activity_spec.rb -cf specdoc
CommentActivity#comments
- finds the 5 most recent approved comments for the post
- is memoized to avoid excess hits to the DB
Finished in 0.056793 seconds
2 examples, 0 failures
## A failing Test
The problem in my opinion is that the test is testing the implementation, not the behavior. but Im new to rails so I can missing some points
context "find recent comments" do
before :each do
@comments = []
(1..10).each do |n|
@comments << Comment.create(valid_comment_attributes(:created_at => Time.now - n * (60 * 60 * 24)))
end
end
it "should have the comment activity sorted by when they were created" do
comment_activity = CommentActivity.find_recent
comment_activity.first.post.should == @comments.first.post
end
it do
comment_activity = CommentActivity.find_recent
comment_activity.should have_exactly(5).posts
end
end
Error
ActiveRecord::StatementInvalid in Admin/dashboardController#show
PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...comments ON comments.post_id = posts.id ORDER BY comments.c...
^
: SELECT distinct posts.* FROM "posts" INNER JOIN comments ON comments.post_id = posts.id ORDER BY comments.created_at DESC LIMIT 5
Application Trace | Framework Trace | Full Trace
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `execute'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:1037:in `select_raw'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:1024:in `select'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:661:in `find_by_sql'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1548:in `find_every'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:615:in `find'
/home/pdelgallego/github/fix_enki/app/models/comment_activity.rb:18:in `find_recent'
/home/pdelgallego/github/fix_enki/app/controllers/admin/dashboard_controller.rb:4:in `show'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment