规格失败/错误:current_path.should == tasks_path

时间:2012-07-15 22:59:47

标签: ruby-on-rails ruby-on-rails-3 testing rspec2 ruby-on-rails-3.2

我正在研究本教程(时间是33:55):http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/

虽然localhost:3000中显示的内容正确且功能正常,但我仍然收到rspec错误。谢谢你的任何建议!

Ruby 1.9.2p290 Rails 3.2.3 RSpec 2.11.0

错误: 运行:spec / requests / tasks_spec.rb spec / controllers / tasks_controller_spec.rb ..F。

故障:

   1) Tasks PUT/tasks edits a task
       Failure/Error: current_path.should == tasks_path
        expected: "/tasks"
           got: "/tasks/1/edit" (using ==)
       # ./spec/requests/tasks_spec.rb:40:in `block (3 levels) in <top (required)>
       '

tasks_controller.rb:

     class TasksController < ApplicationController
        def index
         @task = Task.new
         @tasks = Task.all
        end

        def create
          Task.create params[: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]
              redirect_to tasks_path
            else
              redirect_to :back
            end
        end

       end

tasks_spec.rb:

要求'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 = 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'
        click_button 'Update Task'

        current_path.should == tasks_path

        page.should have_content 'updated task'
    end

端      端

1 个答案:

答案 0 :(得分:0)

我相信通过这项任务......

  

current_path = edit_task_path(@task)

...你创建了一个名为'current_path'的新变量,它覆盖了capybara的。

故事的寓意是不分配给'current_path'