Ruby on rails Rspec/Capybara检查页面整个内容的连接字符串是否为预期字符串

Ruby on rails Rspec/Capybara检查页面整个内容的连接字符串是否为预期字符串,ruby-on-rails,ruby,rspec,capybara,Ruby On Rails,Ruby,Rspec,Capybara,我正在测试在rails应用程序中编辑产品的过程。该过程在我的网站上按预期运行,所有其他类似的规范都通过了。在本例中,当我告诉Capybara在表单提交后页面上会出现一个字符串“Product details have Update”时,它正在查看另一个字符串,该字符串由页面上连接在一起的所有内容组成。请查看下面我从rspec收到的消息,如果您需要更多信息,请告诉我。正如您所看到的,我的字符串显示在页面上,但是rspec无法识别它 来自rspec的消息: 1) edit a product p

我正在测试在rails应用程序中编辑产品的过程。该过程在我的网站上按预期运行,所有其他类似的规范都通过了。在本例中,当我告诉Capybara在表单提交后页面上会出现一个字符串“Product details have Update”时,它正在查看另一个字符串,该字符串由页面上连接在一起的所有内容组成。请查看下面我从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
class ProductsController
观点:

    <div class='card'>
      <h3>Update <%= @product.name%></h3>
      <p><%= flash[:notice] %></p>
      <%= render 'form' %>
      <%= link_to 'Back to Product Page', product_path(@product) %>
    </div>

更新

问题出在您更新的操作中:

  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”更改为“updated”,您就可以开始了。

可能因为页面上显示的是“udpated”而不是“updated”,所以它找不到它?现在这是一个很好的转置定位+1.