Ruby on rails rails rspec capybara测试capybara::ElementNotFound:无法找到字段“;标题「;

Ruby on rails rails rspec capybara测试capybara::ElementNotFound:无法找到字段“;标题「;,ruby-on-rails,testing,capybara,Ruby On Rails,Testing,Capybara,不知道为什么会这样另一个通过了 require 'spec_helper' feature "Editing tickets" do let!(:project) {FactoryGirl.create(:project)} let!(:ticket) {FactoryGirl.create(:ticket, project: project)} before do visit '/' click_link project.name

不知道为什么会这样另一个通过了

require 'spec_helper'

feature "Editing tickets" do
    let!(:project) {FactoryGirl.create(:project)}
    let!(:ticket) {FactoryGirl.create(:ticket, project: project)}

    before do
        visit '/'
        click_link project.name
        click_link ticket.title
        click_link "Edit Ticket"
    end

    scenario "Updating a ticket" do
        fill_in "Title", with: "Make it really shiny!"
        click_button "Update Ticket"

        expect(page).to have_content "Ticket has been updated."

        within("#ticket h2") do
            expect(page).to have_content("Make it really shiny!")
        end

        expect(page).to_not have_content ticket.title
    end

    scenario "Updating a ticket with invalid information" do
        fill_in "Title", with: ""
        click_button "Update Ticket"

        expect(page).to have_content("Ticket has not been updated.")
    end
end
_form.html.erb

<%= form_for [@project, @ticket] do |f| %>
<% if @ticket.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@ticket.errors.count, "error") %>
            prohibited this ticket from being saved.</h2>

            <ul>
                <% @ticket.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
        <% end %>

    <p>
        <%= f.label :title , "Title" %><br />
        <%= f.text_field :title %>
    </p>
    <p>
    <%= f.label :description, "Description" %><br />
    <%= f.text_area :description %>
    </p>
    <%= f.submit %>
    <% end %>

禁止保存这张票。


售票员

class TicketsController < ApplicationController
    before_action :set_project
    before_action :set_ticket, only: [:show, :edit, :update, :destroy]

    def new
        @ticket = @project.tickets.build
    end

    def create
        @ticket = @project.tickets.build(ticket_params)
        if @ticket.save
            flash[:notice] = "Ticket has been created."
            redirect_to [@project, @ticket]
        else
            flash[:alert] = "Ticket has not been created."
            render 'new'
        end
    end

    private

    def ticket_params
        params.require(:ticket).permit(:title, :description)
    end

    private

    def set_project
        @project = Project.find(params[:project_id])
    end

    private

    def set_ticket
        @ticket = @project.tickets.find(params[:id])
    end
end
class ticketcontroller
从项目视图的

<% title(@project.name, "Projects") %>
<h2><%= @project.name %></h2>
<%= link_to "Edit Project", edit_project_path(@project) %>
<%= link_to "Delete Project", project_path(@project),
            method: :delete,
            data: {confirm:
                    "Are you sure you want to delete this project?"
                    } %>
<%= link_to "New Ticket", new_project_ticket_path(@project) %>

<ul id="tickets">
    <% @project.tickets.each do |ticket| %>
    <li>
        #<%= ticket.id %> -
        <%= link_to ticket.title, [@project, ticket] %>
    </li>
    <% end %>
</ul>

  • # -
Tickets show.html.erb

<div id="ticket">
    <h2><%= @ticket.title %></h2>
    <%= link_to "Edit Ticket", [:edit, @project, @ticket] %>
    <%= simple_format(@ticket.description) %>
</div>

错误是

1) 编辑票证更新票证 失败/错误:在“标题”中填写:“使其真正闪亮!” Capybara::ElementNotFound: 找不到字段“标题” #./spec/features/editing_tickets_spec.rb:16:in'block(2层)in'

2) 编辑票证使用无效信息更新票证 失败/错误:在“标题”中填写:“ Capybara::ElementNotFound: 找不到字段“标题” #./spec/features/editing_tickets_spec.rb:29:in'block(2层)in'


提交编辑表单后,用户不会重定向到
show.html.erb
页面。这就是断言失败的原因

添加一个编辑操作,成功后可呈现您的显示页面

render :show

使用2个空格缩进您的Ruby代码,而不是4个空格(或制表符)。抱歉,这是我的错误,我忘了从project的视图中添加一个包含#ticket的附加页面。没有(“ticket have not update.”没有显示,因为我没有添加更新按钮功能。当您更新ticket时,你确定要进入“展示”页面吗?我看不到控制器中的逻辑。嗯,这些漫长的编码时间对我影响很大。我从票证的视图文件中添加了显示html,我想这应该有助于您更好地理解错误。。