Capybara have_content()没有按预期显示

时间:2017-01-29 05:56:24

标签: rspec capybara ruby-on-rails-5

好的,我的智慧结束了。我有一个功能规格,它没有通过。 deletion_posts_spec.rb没有找到文章“帖子被成功销毁”。我已经检查了页面,在我的localhost上,成功通知文本肯定存在。我尝试插入一个byebug,当我运行page.body时,在byebug中,我可以看到通知的代码,当我实际删除某些内容时,我可以看到通知。奇怪的是,我对creation_posts有另一个测试,成功通知确实显示并且测试通过了。

很奇怪,对吧?

我没有turbolinks,因为之前导致奇怪的错误。我也在控制器中取出了重定向,但仍然没有注意到。我最近捆绑更新,但仍然失败。我的ruby版本是2.3.1,RSpec是3.5.4。

如果您需要其他信息,请与我们联系。

我的错误

Deleting Posts admins can delete posts
     Failure/Error: expect(page).to have_content "Post was successfully destroyed."
       expected to find text "Post was successfully destroyed." in "Listing All Posts Content Author Actions Post Something"

更新!我在删除规范中更改了我的代码,但我仍然遇到了同样的失败。我评论了最初的内容供参考。

deleting_posts_spec.rb

RSpec.feature "Deleting Posts" do
   let(:administrator) {Administrator.create(name: "My Name", email: "my@example.com", password: "password" )}

   let(:post) {Post.create!(content: "The Content created", votes: 1, story: "My story", author: "A Name")}

     scenario "admins can delete posts" do
     login(administrator)

     click_link "Admin Page"

     #page.all("tr.the-actions").each do |tr|
       click_on "Delete" if page.has_selector?('a[href*="delete-link"]')
         #.to change(Post, :count).by(-1)
     #end

    expect(page.current_path).to eq(admin_posts_path)
    expect(page).to have_content "Post was successfully destroyed."
  end
end

管理员/ posts_controller.rb

def destroy                                                                                                                            
  if @post.destroy
    redirect_to admin_posts_path
    flash[:notice] = 'Post was successfully destroyed.'
  end
end

视图/管理/帖/ index.html.erb

<p id="notice"><%= notice %></p>                                                                                                         

 <script type="text/javascript">
   $(document).ready(function(){
     setTimeout(function(){
       $('#notice').remove();
     }, 2000);
   })
 </script>

 <center><h1>Listing All Posts</h1></center>

  ...
  stuff
  ...

  <% @posts.each do |post| %>
    <tr class="the-actions">
      <td><center><%= post.content %></center></td>
      <td><center><%= link_to post.author, post_path(post.id) %></center></td>
      <td class="actions-column">
        <%= link_to 'Show', post %> &middot;
        <%= link_to 'Edit', [:edit, :admin, post], class: "edit-link"  %> &middot;
        <%= link_to 'Delete', [:admin, post], method: 'delete', confirm: 'Are you sure you want to delete this post?', class: "delete-     link" %>                                                                                                                                 
      </td>
    </tr>
  <% end %>

1 个答案:

答案 0 :(得分:1)

我在上面添加了一条评论,其中有一些关于您的测试的评论,但我认为它不起作用的主要原因是当您转到该页面时,实际上并未创建Post对象。使用let时,首次引用时会延迟创建对象,但您的测试从不引用post,因此它永远不会创建。您可以使用let!代替

来解决此问题
let!(:post) {Post.create!(content: "The Content created", votes: 1, story: "My story", author: "A Name")}

将始终创建对象。如果您的测试中没有if page.has_selector?('a[href*="delete-link"]')代码,这很明显,因为Capybara会告诉您它无法找到“删除”代码。链路/按钮