2012-02-05 60 views
0

我正在嘗試遵循Michael Hartl在http://ruby.railstutorial.org/chapters/sign-in-sign-out的Ruby on Rails教程,但對練習做了一些更改,首先是一些變體和Test :: Unit框架。在本教程中,使用RSpec,而我試圖堅持Test :: Unit + Shoulda-context。爲什麼控制器var在測試中不可用?

在第9章中,我試圖通過一些使用稱爲'控制器'的var的功能測試,但由於發現'controller'不存在,所以我的測試不起作用。這就是我得到:

[email protected]:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake test:recent Loaded suite /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake_test_loader Started F =============================================================================== Failure: test: POST 'create' with valid signin (email and password) should redirect to the user show page. (SessionsControllerTest) [test/functional/sessions_controller_test.rb:58]: Expected at least 1 element matching "title", found 0. is not true. =============================================================================== E =============================================================================== Error: test: POST 'create' with valid signin (email and password) should sign in the user. (SessionsControllerTest): NameError: undefined local variable or method `controller' for

test/functional/sessions_controller_test.rb:53:in `block (3 levels) in <class:SessionsControllerTest>' 

=============================================================================== Finished in 0.957865676 seconds. 7 tests, 6 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications 0% passed 7.31 tests/s, 6.26 assertions/s rake aborted! Command failed with status (1): [/home/marcel/.rvm/rubies/ruby-1.9.2-p290/b...] Tasks: TOP => test:recent (See full trace by running task with --trace)

這是原來的(RSpec的)測試:

describe SessionsController do 
    ... 
    describe "POST 'create'" do 
    ... 
    describe "with valid email and password" do 
     before(:each) do 
     @user = Factory(:user) 
     @attr = { :email => @user.email, :password => @user.password } 
     end 

     it "should sign the user in" do 
     post :create, :session => @attr 
     controller.current_user.should == @user 
     controller.should be_signed_in 
     end 

     it "should redirect to the user show page" do 
     post :create, :session => @attr 
     response.should redirect_to(user_path(@user)) 
     end 
    end 
    end 
end 

,這是我的翻譯(進入測試::單位+ Sholuda上下文)測試:

class SessionsControllerTest < ActionController::TestCase 
    context "POST 'create'" do 
    context "with valid signin (email and password)" do 
     setup do 
     @attr = {email: "[email protected]", password: "testpwd"} 
     @user=User.create! @attr.merge!({name: "test_user", password_confirmation: "testpwd"}) 
     end 

     should "sign in the user" do 
     post :create, :session => @attr 
     assert_equal @user, controller.current_user 
     end 

     should "redirect to the user show page" do 
     post :create, :session => @attr 
     assert_select "title", /Show/ 
     end 
    end 
    end 
end 

有沒有人知道如何讓我的測試工作?

回答

1

查看官方Rails測試指南http://guides.rubyonrails.org/testing.html,我已經看到在功能測試中啓用了名爲@controller的實例變量。所以,Test :: Unit版本應該是:

should "sign in the user" do 
    post :create, :session => @attr 
    assert_equal @user, @controller.current_user 
end 
相關問題