rails4 Rspec 0失败,但fill_in“string”没有按预期填充

时间:2013-12-27 19:00:16

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

当我运行Rspec时,我得到了这个回复:

在1.91秒内完成 5个例子,0个失败,2个未决

除了在tasks_spec.rb中我要求它编辑和填充更新的任务之外,哪个很好,但它没有这样做。我是rspec和编码的新手,但在我看来,我得到的反馈并没有正确地传达已发生的事情。假设要更新的任务未更新。

  1. 如果没有更新,为什么我会收到0次失败?
  2. 为什么要求编辑的任务不是使用fill_in字符串进行更新?
  3. 为什么在运行rspec和浏览器打开后点击编辑链接会给我带来错误信息? 为什么在检查localhost时手动完成所有工作,但在运行rspec后没有在浏览器中工作?
  4. 当我通过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 %>
    

1 个答案:

答案 0 :(得分:0)

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