2017-10-04 85 views
1

我在爲獲得檔案Rspec的 - 檔案陣列進行匹配測試陣列

def self.archives 
    Post.unscoped.select("YEAR(created_at) AS year, MONTHNAME(created_at) AS month, COUNT(id) AS total") 
     .group("year, month, MONTH(created_at)") 
     .order("year DESC, MONTH(created_at) DESC") 
end 

這是測試我有我的方法寫

context '.archives' do 

    first = FactoryGirl.create(:post, published_at: Time.zone.now) 
    second = FactoryGirl.create(:post, published_at: 1.month.ago) 

    it 'returns articles archived' do 
    archives = Post.archives() 

    expect(
     [{ 
     year: first.published_at.strftime("%Y"), 
     month: first.published_at.strftime("%B"), 
     published: 1 
     }, 
     { 
     year: second.published_at.strftime("%Y"), 
     month: second.published_at.strftime("%B"), 
     published: 1 
     }] 
    ).to match_array(archives) 
    end 
end 

但是在我的崗位模型該類方法我得到以下錯誤

expected collection contained: [#<Post id: nil>, #<Post id: nil>] 
actual collection contained: [{:year=>"2017", :month=>"October", :published=>1}, {:year=>"2017", :month=>"September", :total=>1}] 
the missing elements were:  [#<Post id: nil>, #<Post id: nil>] 
the extra elements were:  [{:year=>"2017", :month=>"October", :total=>1}, {:year=>"2017", :month=>"September", :total=>1}] 

所以,雖然我創建了2個工廠,但archives數組是空。我究竟做錯了什麼?

回答

1

Rspec的標準是使用let語法的上下文中定義的變量或描述塊。測試應該是這個樣子:

describe '.archives' do 
    let!(:first) { FactoryGirl.create(:post, published_at: Time.zone.now) } 
    let!(:second) { FactoryGirl.create(:post, published_at: 1.month.ago) } 

    it 'returns year, month, and total for articles archived' do 
    actual_attributes = Post.archives.map { |post| [post.year, post.month, post.total] } 
    expected_total = 1 # I don't know why the query is returning 1 for total, but including this for completeness 
    expected_attributes = [first, second].map { |post| [post.created_at.year, post.created_at.strftime("%B"), expected_total] } 

    expect(actual_attributes).to match_array(expected_attributes) 
    end 
end 

這裏的問題是,你比較,只有少數屬性(您的SQL查詢的結果)具有完全形成的記錄(通過測試創建)拉記錄。該測試從兩個組中拉取適用的屬性並進行比較。

+0

這似乎不起作用。我收到了幾乎相同的錯誤 – Lykos

+0

'預期集合包含:[#<發佈ID:無>,#<發佈ID:無>]實際集合包含:[#<發佈ID:1,user_id:4,標題:「這是帖子標題「,摘錄:」這是帖子正文「,created_at:」2017-10-04 21:25:31「,updated_at:」2017-10-04 21:25:31「>]' – Lykos

+0

我已經調整了一下。請再試一次。 – moveson

1

實際數組不是空的,它是兩個Post實例的id未設置的數組(因爲Select.archives方法不包含id字段)。 您可以比較預期的哈希值不archives,但水木清華這樣的:

actual = Post.archives().map do |post| 
    { year: post["year"].to_s, month: post["month"], published: post["total"] } 
end 

expected = [{ 
    year: first.published_at.strftime("%Y").to_s, 
    month: first.published_at.strftime("%B"), 
    published: 1 
}, 
{ 
    year: second.published_at.strftime("%Y").to_s, 
    month: second.published_at.strftime("%B"), 
    published: 1 
}] 

expect(actual).to match_array(expected) 
+0

不知道我明白了,你的意思是這應該代替我的「期望」? – Lykos

+0

澄清了答案。 – hedgesky

+0

'預期集合包含:[{:year =>「2017」,:month =>「October」,:published => 1},{:year =>「2017」,:month =>「September」,:published => 1}]包含的實際集合:[]缺少的元素是:[{:year =>「2017」,:month =>「October」,:published => 1},{:year =>「2017」, :month =>「September」,:published => 1}]' – Lykos