I don't really understand what to do when I get this error :
Failures:
1) MicropostPages micropost destruction as correct user should delete a micropost
Failure/Error: expect { click_link "supprimer" }.to change(Micropost, :count).by(-1)
count should have been changed by -1, but was changed by 0
# ./spec/requests/micropost_pages_spec.rb:41:in `block (4 levels) in <top (required)>'
Finished in 4.12 seconds
92 examples, 1 failure
Failed examples:
rspec ./spec/requests/micropost_pages_spec.rb:40 # MicropostPages micropost destruction as correct user should delete a micropost
Notice that when I click on delete micropost (link "supprimer") the micropost is still present. I give you my code, in order to see if I have an error :
microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost cree !"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
redirect_to root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(params[:id])
rescue
redirect_to root_url
end
end
micropost_pages_spec.rb
describe "MicropostPages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "micropost creation" do
[...]
end
describe "micropost destruction" do
before { FactoryGirl.create(:micropost, user: user) }
describe "as correct user" do
before { visit root_path }
it "should delete a micropost" do
expect { click_link "supprimer" }.to change(Micropost, :count).by(-1)
end
end
end
end
_micropost.html.erb
<li>
<span class="content"> <%= micropost.content %> </span>
<span class="timestamp">
Posté il y a <%= time_ago_in_words(micropost.created_at) %>.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "supprimer", micropost, method: :delete, data: { confirm: "Etes vous sûr?"}, title: micropost.content %>
<% end %>
</li>
_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content">
<%= feed_item.content %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "supprimer", feed_item, method: :delete, data: { confirm: "Etes vous sûr?" },title: feed_item.content %>
<% end %>
</li>
_feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
So, I try differents things and search on the net but find nothing to help me to pass the test to green.
I hope you understand my problem and sorry for my bad english
Thanks
Edit : I found my problem when running spec/requests/micropost_pages_spec.rb. Just see below
I edit microposts_controller.rb by :
def correct_user
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
And for _micropost.html.erb and _feed_item.html.erb :
<%= link_to "supprimer", micropost, method: :delete,
confirm: "Etes vous sûr?",
title: micropost.content %>