当我收到此错误时,我真的不明白该怎么做:
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
请注意,当我点击删除微博(链接“supprimer”)时,微博仍然存在。我给你我的代码,以便查看我是否有错误:
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
端
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
<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>
<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>
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
所以,我尝试不同的东西并在网上搜索,但没有找到任何帮助我将测试传递给绿色。
我希望你理解我的问题并抱歉我的英语不好
由于
编辑:我在运行spec / requests / micropost_pages_spec.rb时发现了我的问题。请看下面
我通过以下方式编辑microposts_controller.rb:
def correct_user
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
对于_micropost.html.erb和_feed_item.html.erb:
<%= link_to "supprimer", micropost, method: :delete,
confirm: "Etes vous sûr?",
title: micropost.content %>
答案 0 :(得分:0)
这可能是因为在继续之前有一条弹出消息要接受。这是由, data: { confirm: "Etes vous sûr?" }
引起的。尝试删除它,看它是否有效。
如果有效,请阅读代码,并按照this post中的说法尝试:
page.driver.browser.switch_to.alert.accept
或者,取消:
page.driver.browser.switch_to.alert.dismiss
喜欢这个
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