【问题标题】:rails4 Rspec 0 failures, but fill_in "string" does not fill_in as expectedrails4 Rspec 0 失败,但 fill_in "string" 没有按预期填写
【发布时间】:2013-12-27 19:00:16
【问题描述】:

当我运行 Rspec 时,我得到这个响应:

在 1.91 秒内完成 5 个示例,0 个失败,2 个待处理

这很好,除了在 tasks_spec.rb 中我要求它编辑和填写更新的任务,但它没有这样做。我是 rspec 和一般编码的新手,但在我看来,我收到的反馈不能正确传达发生的事情。应该更新的任务没有更新。

  1. 如果不更新,为什么会出现 0 次失败?
  2. 为什么要求编辑的任务没有使用 fill_in 字符串更新?
  3. 为什么在运行 rspec 并打开浏览器后单击编辑链接会出现错误消息? 为什么在检入 localhost 时全部手动运行,而在运行 rspec 后在浏览器中却不行?

当我通过 localhost:3000 手动检查时,一切正常。


tasks_spec.rb

require 'spec_helper'

describe "Tasks" do
  before do @task = Task.create task: "go to bed" 
  end
  describe "GET /tasks" do
    it "display some tasks" do    
      visit tasks_path
      page.should have_content "go to bed"
    end

    it "creates a new task" do
      visit tasks_path
      fill_in 'Task', with: "go to work"
      click_button 'Create Task'

      current_path.should == tasks_path
      page.should have_content "go to work"
      save_and_open_page
    end
  end

  describe "PUT /tasks" do
    it "edits a task" do
      visit tasks_path
      click_link "Edit"

      current_path.should == edit_task_path(@task)

      #page.should have_content "go to bed"
      find_field('Task').value.should == "go to bed"

      fill_in 'Task', :with => "updated task edit"
      click_button 'Update Task'

      current_path.should == tasks_path

      page.should have_content "updated task edit"
    end
   end
 end

tasks_controller.rb

class TasksController < ApplicationController
  def index 
    @task = Task.new 
    @tasks = Task.all
  end

  def create 
   Task.create params[:task].permit(:task) 
   redirect_to :back 
  end 

  def edit
    @task = Task.find(params[:id])
  end

  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task].permit(:task))
      redirect_to tasks_path
    else
      redirect_to :back 
    end   
  end
end

index.html.erb

<h1>Tasks</h1>

<%= render 'form' %>

<ul>
  <% for task in @tasks %>
    <li><%= task.task %>
      | <%= link_to 'Edit', edit_task_path(task) %>
    </li>
  <% end %>
</ul>

_form.html.erb

<%= form_for @task do |f|%>
  <%= f.label :task %>
  <%= f.text_field :task %>
  <%= f.submit %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails rspec ruby-on-rails-4


    【解决方案1】:

    检查你的规范助手config.use_transactional_fixtures = true。如果它在那里,这意味着所有数据库交互都是在事务中执行的,基本上会使测试期间对数据库所做的任何更改无效。如果您希望手动执行此清理,我建议将其设置为 false 并查看 database cleaner gem

    【讨论】:

    • 我将 config.use_transactional_fixtures 更改为 false 以查看会发生什么。我看到它添加了一个额外的“上床睡觉”任务而没有删除已经存在的任务,我认为这就是为什么您建议我查看“数据库清洁器 gem”。但它仍然不会用我要求它填写的新任务替换上床任务。在阅读了一些建议的 gem 语言之后,我认为它不会帮助我解决这个问题。我觉得我已经成功了一半。为什么它不会用新字符串填充?
    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多