我正在测试在rails应用中编辑产品的过程。该过程在我的网站上按预期工作,所有其他类似的规格都在通过。在这种情况下,当我告诉Capybara期望一个字符串"产品细节已经更新"在提交表单后的页面上,它查找另一个字符串,该字符串由连接在一起的页面上的所有内容组成。请参阅下面我从rspec获得的消息,如果您需要更多信息,请告诉我们。正如您所看到的,我的字符串显示在页面上,但rspec无法将其选中。
来自rspec的消息:
1) edit a product process edits a product
Failure/Error: expect(page).to have_content "Product details have
been updated."
expected to find text "Product details have been updated." in
"Welcome to Mario's Specialty Food Products See all products Return
to Home Update Hot Dog Product details have been udpated. Name Cost
Country Back to Product Page"
失败的规范:
require 'rails_helper'
describe "edit a product process" do
it "edits a product " do
product = Product.create(:name => "Hot Dog", :cost => 10, :country => "United States")
visit products_path
click_link 'See all products'
click_link product.name
click_on "Edit Product Details"
fill_in "Cost", :with => 7
click_on "submit"
expect(page).to have_content "Product details have been updated."
end
end
控制器:
class ProductsController < ApplicationController
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
flash[:notice] = "Product details have been udpated."
render :edit
else
render :edit
end
end
观点:
<div class='card'>
<h3>Update <%= @product.name%></h3>
<p><%= flash[:notice] %></p>
<%= render 'form' %>
<%= link_to 'Back to Product Page', product_path(@product) %>
</div>
答案 0 :(得分:2)
问题在于您的#updated操作:
def update
@product = Product.find(params[:id])
if @product.update(product_params)
flash[:notice] = "Product details have been udpated."
render :edit
else
render :edit
end
end
将您的Flash通知从“udpated”更改为“已更新”,您应该好好去。