【发布时间】:2013-12-27 19:00:16
【问题描述】:
当我运行 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 %>
【问题讨论】:
标签: ruby-on-rails rspec ruby-on-rails-4