2014-11-11 2 views
0

블로그 기능이 포함 된 레일 앱이 있습니다.Capybara Rails TestCase를 단일 트랜잭션으로 실행하려면 어떻게해야합니까?

Ruby 테스트 (asserts/refutes) (즉, 사양이 아닌 Capybara :: Rails :: TestCase 사용)에서 블로그를 테스트 할 때 게시물 추가, 주석 추가, 게시물 편집 테스트를하고 싶습니다 각 테스트는 마지막 테스트를 바탕으로 이루어지며 첫 번째 테스트에서 생성 된 포스트는 두 번째 테스트에서 주석 처리됩니다.

단위 테스트 (글로벌 변수, setup/teardown 사용)에서이 작업을 수행 할 때 해결 방법을 보여주는 게시물을 보았지만 기능 테스트에서 직접 테스트하는 방법이 있는지 궁금해했습니다. 여기서 더 일반적입니다.

이상적으로는 이전 테스트에서 생성 된 데이터베이스 레코드뿐만 아니라 로그인 세션이 지속되도록하여 TestCase의 각 테스트에서 지속되도록합니다.

class BlogTest< Capybara::Rails::TestCase 
    test 'can sign in' do 
     user = User.create!(name: "user", 
      email: "[email protected]", 
      password: "passw0rd!", password_confirmation: "passw0rd!") 

     visit new_user_session_path 
     fill_in('Login', :with => user.email) 
     fill_in('Password', :with => user.password) 
     check('Remember me') 
     click_button('Sign in') 
    end 

    test 'can create post' do 
    visit new_post_path # how can I have user logged in? 
    fill_in "Title", with: "My first post title!" 
    fill_in "Body", with: "My first post body!" 
    click_button "Publish" 
    end 

    test 'can comment on post' do 
    visit post_path(Post.first) # should go to post created in last test 
    click_button "Add comment" 
    ... 
    end 
end 

내가이 오이에서 가능하다 들었습니다 : 설치 및 해체는 게시물, 댓글에 대해 생성 된 중간 기록 등

내가 좋아하는 뭔가를 할을 때마다 로그인하는 데 사용하지만 수 , 다른 이유로 오이를 사용하지 않기로 선택 했으므로 Minitest와 Capybara와 함께 작업하길 원합니다.

답변

1

Capybara::Rails::TestCaseActiveSupport::TestCase에서 상속됩니다. ActiveSupport::TestCase의 주요 기능 중 하나는 데이터베이스 트랜잭션에서 각 테스트를 실행한다는 것입니다. 이 문제를 해결할 수있는 방법이 있지만 권장하지는 않습니다.

대신 레일 테스트 클래스의 동작을 제안하십시오. 이 경우 테스트간에 작업을 공유하려고합니다. 이러한 작업을 메서드로 추출하고 테스트에서 해당 메서드를 호출하는 것이 좋습니다. 다음은 테스트 코드를 사용하여 구현하는 방법입니다.

class BlogTest< Capybara::Rails::TestCase 
    def user 
    @user ||= User.create!(name: "user", 
          email: "[email protected]", 
          password: user_password, 
          password_confirmation: user_password) 
    end 

    def user_password 
    "passw0rd!" 
    end 

    def sign_in(email, password) 
    visit new_user_session_path 
    fill_in('Login', :with => email) 
    fill_in('Password', :with => password) 
    check('Remember me') 
    click_button('Sign in') 
    end 

    def create_post(title = "My first post title!", 
        body = "My first post body!") 
    visit new_post_path # how can I have user logged in? 
    fill_in "Title", with: title 
    fill_in "Body", with: body 
    click_button "Publish" 
    end 

    def comment_on_post(post, comment) 
    visit post_path(post) 
    click_button "Add comment" 
    # ... 
    end 

    test "can sign in" do 
    sign_in(user.email, user_password) 
    # add assertions here that you are signed in correctly 
    end 

    test "can't sign in with a bad password" do 
    sign_in(user.email, "Not the real password") 
    # add assertions here that you are not signed in 
    end 

    test "can create post when signed in" do 
    sign_in(user.email, user_password) 
    create_post 
    # add assertions here that post was created correctly 
    end 

    test "can't create post when not signed in" do 
    create_post 
    # add assertions here that post was not created 
    end 

    test "can comment on post when signed in" do 
    sign_in(user.email, user_password) 
    create_post 
    post = user.posts.order(:created_at).last 
    comment_on_post(post, "I can comment because I'm signed in!") 
    # add assertions here that comment was created correctly 
    end 

    test "can't comment on post when not signed in" do 
    post = Post.first 
    comment_on_post(post, "I can't comment because I'm not signed in!") 
    # add assertions here that comment was not created 
    end 
end 

각 작업의 이름이 적절하며 다른 유형의 테스트에 대해 이러한 작업을 다시 사용할 수 있습니다. 각 테스트는 데이터베이스 트랜잭션 내에서 실행되므로 각 테스트 메소드가 실행될 때마다 데이터베이스가 동일하게 보입니다.

+0

아마도 이러한 메서드 중 일부는 test_helper.rb의 Capybara :: Rails :: TestCase에 추가 될 수 있습니까? user와 user_password 같은 것이 더 일반적으로 유용합니까? – Anand

+0

블로그 게시물 [여기] (http://rcanand.github.io/How_To_Test_Rails_With_Minitest_And_Capybara/)으로 응답 연장 (계층화 된 테스트 사용 가능)을 게시했습니다. – Anand

관련 문제