2012-02-05 2 views
0

나는 Michael Hartl의 Ruby on Rails 튜토리얼 http://ruby.railstutorial.org/chapters/sign-in-sign-out을 따르려고 노력하고 있지만, 무엇보다 연습, 몇 가지 변형 및 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

공식 레일 테스트 가이드 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 
관련 문제