I just read few posts about "how to test named scopes". I'm asking - why should I use FactoryGirl and create tons of unnecessary objects just to test `order(:position).first`?!
How not to do this
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
require 'spec_helper' | |
describe Post do | |
context '#recent' do | |
it 'should return most recent post' do | |
_, post = [Factory(:post), Factory(:post)] | |
Post.recent.should == post | |
end | |
end | |
context '#for_user' do | |
it 'should return only posts created by given user' do | |
post1, post2, _ = [Factory(:post, user_id: 1), Factory(:post, user_id: 1), Factory(:post, user_id: 2)] | |
Post.for_user(1).should == [post1, post2] | |
end | |
end | |
end |
How to do this
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
require 'spec_helper' | |
describe Post do | |
context '#recent' do | |
it 'should return most recent post' do | |
post = mock_model Post | |
Post.should_receive(:order).with('created_at DESC').and_return post | |
post.should_receive(:first).and_return post | |
Post.recent | |
end | |
end | |
context '#for_user' do | |
it 'should return only posts created by given user' do | |
post = mock_model Post | |
Post.should_receive(:where).with(user_id: 1) | |
Post.for_user 1 | |
end | |
end | |
end |
Brak komentarzy:
Prześlij komentarz