2013-05-01 81 views
1

我正在Rails中開發一個博客,當我試圖測試我添加到Post模型中的默認範圍以便讓帖子按降序排列時他們的創作日期。在使用FactoryGirl進行Rspec測試時,Rails模型中的Default_scope

郵編:

class Post < ActiveRecord::Base 
    attr_accessible :content, :name, :title 

    validates :title, presence: true,uniqueness: true 
    validates :name, presence: true 
    validates :content, presence: true 

    default_scope order: "posts.created_at DESC" 

end 

Rspec的代碼:

describe "Posts descending order of creation date" do 
    let(:older_post) do 
    FactoryGirl.create(:post, created_at: 1.day.ago) 
    end 
    let(:newer_post) do 
    FactoryGirl.create(:post, created_at: 1.hour.ago) 
    end 

    it "should have the 2 posts in desc order" do 
    Post.all.should == [newer_post, older_post] 

    end  
    end 

FactoryGirl定義

FactoryGirl.define do 
    factory :post do 
    sequence(:title) { |n| "A book #{n}" } 
    name "Johnny" 
    content "Lorem Ipsum" 
    end  
end 

輸出 ..... F的

失敗:在成品1.03秒 9實施例中,1次失敗

失敗實例

1) Post Posts descending order of creation date should have the 2 posts in desc order 
    Failure/Error: Post.all.should == [newer_post, older_post] 
     expected: [#<Post id: 1, name: "Johnny", title: "A book 1", content: "Lorem Ipsum", created_at: "2013-05-01 14:44:45", updated_at: "2013-05-01 15:44:45">, #<Post id: 2, name: "Johnny", title: "A book 2", content: "Lorem Ipsum", created_at: "2013-04-30 15:44:45", updated_at: "2013-05-01 15:44:45">] 
     got: [] (using ==) 
    Diff: 
    @@ -1,3 +1,2 @@ 
    -[#<Post id: 1, name: "Johnny", title: "A book 1", content: "Lorem Ipsum", created_at: "2013-05-01 14:44:45", updated_at: "2013-05-01 15:44:45">, 
    - #<Post id: 2, name: "Johnny", title: "A book 2", content: "Lorem Ipsum", created_at: "2013-04-30 15:44:45", updated_at: "2013-05-01 15:44:45">] 
    +[] 

# ./spec/models/post_spec.rb:54:in `block (3 levels) in <top (required)>' 

rspec ./spec/models/post_spec.rb:53 # Post Posts descending order of creation date should have the 2 posts in desc order 

我也想提一提,當我在滑軌控制檯鍵入Post.all,我以降序獲得記錄(所以我想他們)。

有人可以給我一個什麼問題的建議嗎?

+0

似乎是重複的:http://stackoverflow.com/questions/6853744/how-can-i-have-an-rspec-test-for-my-default-scope – 2013-05-01 15:55:48

回答

1

請知道,let在RSpec中被懶惰地評估。這常常會在涉及排序的情況下產生問題。 嘗試這兩個選擇:

describe "Posts descending order of creation date" do 
    let!(:older_post) do 
    FactoryGirl.create(:post, created_at: 1.day.ago) 
    end 
    let!(:newer_post) do 
    FactoryGirl.create(:post, created_at: 1.hour.ago) 
    end 

    it "should have the 2 posts in desc order" do 
    Post.all.should == [newer_post, older_post] 
    end  
    end 

注意,使用let!,而不是let
或者,使用before爲:

describe "Posts descending order of creation date" do 

    it "should have the 2 posts in desc order" do 
    @older_post = FactoryGirl.create(:post, created_at: 1.day.ago) 
    @newer_post = FactoryGirl.create(:post, created_at: 1.hour.ago) 

    Post.all.should == [@newer_post, @older_post] 
    end  
    end 

不要讓我知道,如果工程或沒有。 :)

+0

除非有一個原因在測試中共享這些對象,不要使用「let」。只需在'it'塊中使用局部變量即可。 – 2013-05-01 17:49:08

+0

謝謝!實際上,我打算在上一個例子中將對象放在'before'塊中。但不知何故,忘了它。 – kiddorails 2013-05-01 17:55:12

+0

周圍測試的背景很重要,但爲什麼還要用'before'?通過單一測試,這些變量只在本地有重要意義,應該只是當地人。 – 2013-05-01 17:56:31

相關問題