私はレールアプリで製品を編集するプロセスをテストしています。このプロセスは私のサイトでは期待通りに動作していますが、他のすべての同様の仕様が合格しています。この例では、フォームが提出された後に、「製品の詳細が更新されました」という文字列がCapybaraに送信されると、ページ上のすべてのコンテンツで構成される別の文字列を参照しています。下記のrspecから私が得ているメッセージを見て、それ以上の情報が必要な場合はお知らせください。ご覧のとおり、私の文字列はページに表示されていますが、rspecはそれを選択できません。Rspec/Capybaraは、予想される文字列のページ全体の内容を連結した文字列をチェックします。
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>
「更新済み」ではなく「udpated」と表示される可能性があります。 – moveson