douglas
douglas

Reputation: 186

Trouble with Capybara + RSpec on a new model spec

So I made my first Rails app without testing. Now I'm redoing the app with testing first. I'm making a request spec for the model I'm creating (Task). I'm testing for the form for creating a new Task.

The number of tasks is supposed to change by 1 (i.e., a new task was saved), but it isn't changing. I basically followed Michael Hartl's code for this.

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)>'

Relevant Code: Model

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

Controller

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

Integration Test

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

And the form

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

Upvotes: 1

Views: 217

Answers (1)

Erik Peterson
Erik Peterson

Reputation: 4311

Your form has the :day field twice. The 1st is probably populated by your test and then clobbered by the empty value in the 2nd.

Upvotes: 2

Related Questions