Je vais avoir cette méthode de classe sur mon modèle Post pour obtenir des archivesRspec - tableau archives pour correspondre ensemble testées
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
C'est le test que je l'ai écrit pour ma méthode
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
Cependant Je reçois l'erreur suivante
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}]
Ainsi, bien que j'ai créé 2 usines, le tableau est archives
vide. Qu'est-ce que je fais mal?
Cela ne semble pas fonctionner. Je reçois presque la même erreur – Lykos
'collection attendue contenue: [#, # ] collection réelle contenue: [# ]' –
Lykos
J'ai l'a un peu modifié. Veuillez réessayer. – moveson