Rspec/Capybara 检查页面全部内容的连接字符串,是否为预期字符串

Rspec/Capybara checking concatenated string of entire content of page, for expected string

我正在测试在 rails 应用程序中编辑产品的过程。该过程在我的网站上按预期工作,所有其他类似规范均通过。在这种情况下,当我告诉 Capybara 在提交表单后期待页面上的字符串 "Product details have been updated" 时,它正在寻找另一个字符串,该字符串由页面上的所有内容连接在一起组成。请查看下面我从 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>

问题出在您的#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

将您的即时通知从 "udpated" 更改为 "updated",您应该可以开始了。