【问题标题】:rails rspec capybara testing Capybara::ElementNotFound:Unable to find field "title"rails rspec capybara testing Capybara::ElementNotFound:Unable to find field "title"
【发布时间】:2015-12-20 20:18:12
【问题描述】:

不知道为什么要这样做,其他人通过了这件事

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

项目视图中的show.html.erb

<% 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>

门票 show.html.erb

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

错误是

1) 编辑工单 更新工单 失败/错误:填写“标题”,带有:“让它真正闪亮!” 水豚::ElementNotFound: 找不到字段“标题” # ./spec/features/editing_tickets_spec.rb:16:in `block (2 levels) in '

2) 编辑工单 使用无效信息更新工单 失败/错误:填写“标题”,带有:“” 水豚::ElementNotFound: 找不到“标题”字段 # ./spec/features/editing_tickets_spec.rb:29:in `block (2 levels) in '

【问题讨论】:

  • 使用 2 个空格来缩进 Ruby 代码,而不是 4 个(或制表符)

标签: ruby-on-rails testing capybara


【解决方案1】:

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

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

render :show

【讨论】:

  • 抱歉,这是我的错误,我忘了从项目视图中添加一个包含#ticket 的附加页面。
  • 并且没有显示(“Ticket has not been updated.”),因为我还没有添加更新按钮功能。
  • 更新票证时,您确定要进入“演出”页面吗?我在控制器中没有看到该逻辑。
  • 呃,这些漫长的编码时间对我的影响很大
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多