水豚 have_content() 未按预期显示
Capybara have_content() not showing as expected
好吧,我已经无计可施了。我有一个功能规范,但没有通过。
deleting_posts_spec.rb 没有找到文本 "Post was successfully destroyed." 我已经检查了该页面,在我的本地主机上肯定有成功通知文本。我尝试插入一个 byebug,当我 运行 page.body 时,在 byebug 中,我可以看到通知的代码,当我实际删除某些内容时,我可以看到通知。奇怪的是,我对 creating_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
admin/posts_controller.rb
def destroy
if @post.destroy
redirect_to admin_posts_path
flash[:notice] = 'Post was successfully destroyed.'
end
end
views/admin/posts/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 %> ·
<%= link_to 'Edit', [:edit, :admin, post], class: "edit-link" %> ·
<%= link_to 'Delete', [:admin, post], method: 'delete', confirm: 'Are you sure you want to delete this post?', class: "delete- link" %>
</td>
</tr>
<% end %>
我在上面添加了一条评论,其中包含一些关于您的测试的评论,但我认为它不起作用的主要原因是 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 会告诉您它找不到 'Delete' link/button
好吧,我已经无计可施了。我有一个功能规范,但没有通过。 deleting_posts_spec.rb 没有找到文本 "Post was successfully destroyed." 我已经检查了该页面,在我的本地主机上肯定有成功通知文本。我尝试插入一个 byebug,当我 运行 page.body 时,在 byebug 中,我可以看到通知的代码,当我实际删除某些内容时,我可以看到通知。奇怪的是,我对 creating_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
admin/posts_controller.rb
def destroy
if @post.destroy
redirect_to admin_posts_path
flash[:notice] = 'Post was successfully destroyed.'
end
end
views/admin/posts/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 %> ·
<%= link_to 'Edit', [:edit, :admin, post], class: "edit-link" %> ·
<%= link_to 'Delete', [:admin, post], method: 'delete', confirm: 'Are you sure you want to delete this post?', class: "delete- link" %>
</td>
</tr>
<% end %>
我在上面添加了一条评论,其中包含一些关于您的测试的评论,但我认为它不起作用的主要原因是 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 会告诉您它找不到 'Delete' link/button