0

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 %>
  • What is the value of `params` in the controller, on the `destroy` action? – Dougui May 07 '14 at 19:30
  • You're allowed to post your own answer to your own problem, and to accept it afterwards. That's actually the recommended behaviour. So you should do that instead of posting the answer at the top of the post. :) – frandroid Jan 02 '15 at 22:02

1 Answers1

0

This is probably because there is a popup message to accept before to continue. This is due to this , data: { confirm: "Etes vous sûr?" }. Try to remove it and see if it works.

If it works, readd the code and, as said in this post try this :

page.driver.browser.switch_to.alert.accept

Alternately, to cancel:

page.driver.browser.switch_to.alert.dismiss

Like this

it "should delete a micropost", js: true do
  expect do
    click_link "supprimer"
    page.driver.browser.switch_to.alert.accept
  end.to change(Micropost, :count).by(-1)
end
Community
  • 1
  • 1
Dougui
  • 7,142
  • 7
  • 52
  • 87
  • Thank you for your help but I don't understand where to put this code ? –  May 07 '14 at 19:42
  • Thanks for your edit. That's tell me : Failure/Error: page.driver.browser.switch_to.alert.accept NoMethodError: undefined method `switch_to' for # # ./spec/requests/micropost_pages_spec.rb:43:in `block (5 levels) in '. Notice that when I click on delete micropost (link "supprimer") the micropost is still present. –  May 07 '14 at 20:59
  • I added reasons of the bug in my answer. I also added `js: true` after the `it`. Try it and let me know if it works. – Dougui May 07 '14 at 21:12
  • It did not work so I tried something else and now that's okay. I can delete a micropost, and the test is green. I edit my question with the correct code for other people. Thanks again for your help Dougui –  May 07 '14 at 21:38
  • And an anwser and approve it. Bonne soirée! – Dougui May 07 '14 at 21:56