我在爲獲得檔案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
數組是空。我究竟做錯了什麼?
這似乎不起作用。我收到了幾乎相同的錯誤 – Lykos
'預期集合包含:[#<發佈ID:無>,#<發佈ID:無>]實際集合包含:[#<發佈ID:1,user_id:4,標題:「這是帖子標題「,摘錄:」這是帖子正文「,created_at:」2017-10-04 21:25:31「,updated_at:」2017-10-04 21:25:31「>]' – Lykos
我已經調整了一下。請再試一次。 – moveson