【问题标题】:Integration tests for a Rails Shopify applicationRails Shopify 应用程序的集成测试
【发布时间】:2017-04-12 08:40:32
【问题描述】:

我正在尝试为使用 Rails 5 和 shopify_app gem 构建的 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]: 预期响应是 ,但是是 重定向到 http://www.example.com/login

问题:

  • 这是设置请求的环境变量的正确方法吗?
  • mock 应该返回什么来模拟成功的身份验证?

相关资源:

【问题讨论】:

  • 如消息所说,您已被重定向到登录页面,所以我认为您在那里有一些授权,您需要登录才能查看此页面。
  • 问题是关于如何存根 shopify_app 身份验证/授权过程以使测试成功。

标签: ruby-on-rails testing shopify ruby-on-rails-5 omniauth


【解决方案1】:

由于shopify_app 使用omniauth 进行身份验证,您应该查看omniauth wiki:https://github.com/omniauth/omniauth/wiki/Integration-Testing

您需要设置OmniAuth.config.test_mode = true,然后使用OmniAuth.config.mock_auth

希望这会有所帮助!

【讨论】:

  • 感谢您的回答,它绝对帮助我朝着正确的方向前进。仍然没有设法通过测试。我更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2020-12-13
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多