2012-02-05 18 views
0

私はhttp://ruby.railstutorial.org/chapters/sign-in-sign-outのMichael HartlのRuby on Railsチュートリアルに従っていますが、とりわけ、いくつかのバリエーションとTest :: Unitフレームワークをいくつか変更しています。チュートリアルではRSpecが使われていますが、私はTest :: Unit + Shoulda-contextに固執しようとしています。なぜコントローラvarはテストで利用できないのですか?

第9章では、「controller」という名前のvarを使用するいくつかの機能テストに合格することを提案しましたが、「コントローラ」が存在しないことがわかったのでテストは機能しません。これは、元の(RSpecの)テストである

[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)

:これは私が得るものです

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 
関連する問題