0

2 人のユーザーを必要とするフローの統合を行う方法を探しています。このフローでは、順番にジャンプすることはできません。

User A does 1
User B does 2
User A does 3
User B does 4
User A does 5
... 

そのため、テスト コードはランダムな順序で実行されます。次のような一連のテストを書くことができず、test "user A does 1" do ... endそれらが順番に実行されることを期待しています

では、上記の状況に対してどのように統合テストを作成すればよいでしょうか?

require 'test_helper'

class MyIntegrationTest < ActionController::IntegrationTest

  test "Test interaction between 2 users" do 
    sign_in 'userA@mysite.com'
    assert_response :success

    get '/does/1'
    assert_response :success

    sign_out

    sign_in 'userB@mysite.com'
    assert_response :success

    get '/does/2'
    assert_response :success

    sign_out

    sign_in 'userA@mysite.com'
    assert_response :success

    get '/does/3'
    assert_response :success

    sign_out

    sign_in 'userB@mysite.com'
    # ahhhhhhhhhhhhhhhhhhhhhhhhhhh! .....
  end

Rails 5 ではコントローラーのテストが削除される可能性があることに注意してください。

https://github.com/rails/rails/issues/18950#issuecomment-77924771

Railsの問題でこれを見つけました:

https://github.com/rails/rails/issues/22742
4

2 に答える 2

1

ここに来る他の人の利益のために: 何らかの理由で、関連するヘルパーが現在の Rails ガイドに記載されていないようですが、https://guides.rubyonrails.org/v4.1/testing.htmlからこの例を見つけました

require 'test_helper'



class UserFlowsTest < ActionDispatch::IntegrationTest
  test "login and browse site" do
    # User david logs in
    david = login(:david)
    # User guest logs in
    guest = login(:guest)
 
    # Both are now available in different sessions
    assert_equal 'Welcome david!', david.flash[:notice]
    assert_equal 'Welcome guest!', guest.flash[:notice]
 
    # User david can browse site
    david.browses_site
    # User guest can browse site as well
    guest.browses_site
 
    # Continue with other assertions
  end
 
  private
 
    module CustomDsl
      def browses_site
        get "/products/all"
        assert_response :success
        assert assigns(:products)
      end
    end
 
    def login(user)
      open_session do |sess|
        sess.extend(CustomDsl)
        u = users(user)
        sess.https!
        sess.post "/login", username: u.username, password: u.password
        assert_equal '/welcome', sess.path
        sess.https!(false)
      end
    end
end

この手法は、Rails 5.1 アプリでも機能するように見えました。

于 2020-06-23T20:47:31.760 に答える
0

次の URL にサンプル コードがあります。

http://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html
http://guides.rubyonrails.org/testing.html#integration-testing-examples

これは、2016 年 1 月 10 日現在、エッジガイドには存在しません。

于 2016-01-10T18:58:21.230 に答える