1
所以我没有测试就制作了我的第一个Rails应用程序。现在我正在重新测试应用程序并首先进行测试。我正在为我创建的模型(Task)提出请求规范。我正在测试创建新任务的表单。新型号规格上的水豚+ RSpec问题
任务的数量应该改变1(即,保存了新的任务),但是它没有改变。我基本上遵循迈克尔哈特尔的代码。
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 %>
我现在没有机会测试这个功能,但是我会在几个小时内给出答案。我不能相信我错过了这一点。谢谢。 – douglas
直到现在还没有机会检查修复程序,它确实有效。感谢您的帮助。 – douglas
@douglasisshiny真棒,很高兴听到它。 –