当我运行Rspec时,我得到的响应是:
1.91秒完成5个示例,0个故障,2个待决
这很好,只是在tasks_spec.rb中,我要求它编辑并填写更新后的任务,但它没有这样做。我对rspec和编码一般都是新手,但在我看来,我收到的反馈并没有正确地传达所发生的事情。本应更新的任务未更新。
- 如果不更新,为什么会出现0次失败
- 为什么不使用fill_in字符串更新请求编辑的任务
- 为什么在运行rspec并打开浏览器后,单击编辑链接会出现错误消息?为什么在签入localhost时全部手动工作,而在运行rspec后却不能在浏览器中工作
当我通过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 %>
检查config.use_transactional_fixtures = true
的规范助手。如果是这样的话,那就意味着所有的数据库交互都是从一个事务中执行的,本质上是取消了测试期间对数据库所做的任何更改。如果您希望手动执行此清理,我建议将其设置为false,并检查数据库清理程序gem。