我正在嘗試爲使用Rails 5和shopify_app gem構建的Shopify應用程序編寫控制器/集成測試。Rails Shopify應用程序的集成測試
我面臨的問題是我無法找到如何繞過我的測試環境中的身份驗證過程。我無法找到存根的正確方法。
我試了一下:
#test_helper.rb
class ActiveSupport::TestCase
# Turn on "test mode" for OmniAuth
OmniAuth.config.test_mode = true
end
。
#products_controller.test.rb
class ProductsControllerTest < ActionDispatch::IntegrationTest
def setup
# Add OAuth mock provider
# Sets authentication hash to return during integration testing
@shop = build(:shop)
OmniAuth.config.add_mock(
:shopify,
prodiver: :shopify,
uid: @shop.shopify_domain,
credentials: {
token: @shop.shopify_token
}
# This get request gets redirected to http://www.example.com/
get shopify_app.auth_shopify_callback_url, params: {shop: @shop.shopify_domain}
end
test "should get index" do
get root_url, { env: {
"omniauth.auth" => OmniAuth.config.mock_auth[:shopify],
"omniauth.params" => { shop: @shop.shopify_domain }
}}
assert_response :success
end
end
測試結果:
失敗: ProductsControllerTest#test_should_get_index [/shopify_stock_exporter/test/controllers/products_controller_test.rb:7]: 預期響應是一個< 2XX:成功>,但被一個< 302:實測值>重定向到http://www.example.com/login
問題:
- 這是設置請求的env變量的正確方法嗎?
- 模擬返回模擬成功認證應該怎麼辦?
相關資源:從shopify_app寶石
正如消息所述,您已被重定向到登錄頁面,所以我認爲您有一些授權,您需要登錄才能查看此頁面。 – Argonus
問題是關於如何對shopify_app認證/授權過程進行存根以使測試成功。 –