2011-01-19 2 views
5

두 가지 카피 바라 테스트가 있습니다. 첫 번째 테스트는 사용자로 로그인하고, 두 번째 테스트는 로그인 한 사용자 만 사용할 수 있습니다.Capybara와 Rails 세션 유지 3

그러나 두 번째 테스트는 세션이 테스트를 통해 유지 관리되지 않으므로 수행 할 수 없습니다 (분명히 있어야 함).

require 'integration_test_helper' 

class SignupTest < ActionController::IntegrationTest 

    test 'sign up' do 
    visit '/' 
    click_link 'Sign Up!' 
    fill_in 'Email', :with => '[email protected]' 
    click_button 'Sign up' 
    assert page.has_content?("Password can't be blank") 
    fill_in 'Email', :with => '[email protected]' 
    fill_in 'Password', :with => 'password' 
    fill_in 'Password confirmation', :with => 'password' 
    click_button 'Sign up' 
    assert page.has_content?("You have signed up successfully.") 
    end 

    test 'create a product' do 
    visit '/admin' 
    save_and_open_page 
    end 

end 

save_and_open_page 호출에 의해 생성 된 페이지는 글로벌 로그인 화면이 아닌 내가 (가입에서 로그에서)를 예상하는대로 관리자 홈페이지입니다. 여기서 내가 뭘 잘못하고 있니?

답변

6

테스트가 트랜잭션이되므로 테스트간에 상태가 손실됩니다. 이 문제를 해결하려면 함수에서 로그인 기능을 복제 한 다음 다시 호출해야합니다.

 
def login 
    visit '/' 
    fill_in 'Email', :with => '[email protected]' 
    fill_in 'Password', :with => 'password' 
    fill_in 'Password confirmation', :with => 'password' 
    click_button 'Sign up' 
end 

test 'sign up' do 
... 
login 
assert page.has_content?("You have signed up successfully.") 
end 

test 'create a product' do 
    login 
    visit '/admin' 
    save_and_open_page 
end 
3

각 테스트는 깨끗한 환경에서 실행됩니다. 일반적인 설정 및 해체 작업을 수행하려면 Rails guides에 설명 된대로 setupteardown 방법을 정의하십시오.