1

そこで、最初の 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 %>
4

1 に答える 1

2

フォームには:dayフィールドが 2 回あります。1番目はおそらくテストによって入力され、2番目の空の値によって上書きされます。

于 2012-10-14T21:22:14.587 に答える