Capybara + RSpec在新型号规格上遇到麻烦

时间:2012-10-14 19:59:47

标签: ruby-on-rails rspec capybara

所以我没有经过测试就制作了我的第一个Rails应用程序。现在我首先通过测试重做应用程序。我正在为我正在创建的模型制作请求规范(任务)。我正在测试创建新任务的表单。

任务数量应该改变1(即保存了新任务),但它没有改变。我基本上遵循Michael Hartl的代码。

Error:
1) Task Pages Creating a Task with valid information creates a Task
   Failure/Error: expect { click_button "Create task" }.to change(Task, :count).by(1)
   count should have been changed by 1, but was changed by 0
# ./spec/requests/tasks_pages_spec.rb:22:in `block (4 levels) in <top (required)>'

相关守则: 模型

class Task < ActiveRecord::Base
  attr_accessible :begin_time, :day, :end_time, :gear, :notes, :preset, :room, :setup,                  
  :strike

  validates :room, presence: true
  validates :begin_time, presence: true
  validates :end_time, presence: true
  validates :gear, presence: true
  validates :day, presence: true
end

控制器

def new
  @task = Task.new
end

def create
  @task = Task.new(params[:task])

  if @task.save
    redirect_to root_path
  else
    render 'new'
  end
end

整合测试

require 'spec_helper'

describe "Task Pages" do

  subject { page }

  describe "Creating a Task" do

    let(:submit) { "Create task" }
    before { visit new_task_path }

    describe "with valid information" do 
      before do 
        fill_in "Day",    with: Date.today
        fill_in "Room",   with: "6W-002"
        fill_in "Begin",  with: Time.now
        fill_in "End",    with: 1.hour.from_now
        fill_in "Gear",   with: "LCD"
      end

      it "creates a Task" do 
        expect { click_button "Create task" }.to change(Task, :count).by(1)
      end

    end
  end
end

表格

<%= form_for(@task) do |t| %>

  <%= t.label :day %>
  <%= t.text_field :day %>

  <%= t.label :room %>
  <%= t.text_field :room %> 

  <%= t.label :begin_time, "Begin" %>
  <%= t.text_field :begin_time %> 

  <%= t.label :end_time, "End" %>
  <%= t.text_field :end_time %> 

  <%= t.label :gear %>
  <%= t.text_field :gear  %> 

  <%= t.label :day %>
  <%= t.text_field :day %> 

  <%= t.submit "Create task", class: "btn btn-large btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:2)

您的表单有两次:day字段。第一个可能是你的测试填充,然后被第二个空值淹没。