【发布时间】: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 应该返回什么来模拟成功的身份验证?
相关资源:
- mock_shopify_omniauth shopify_app gem 中的方法
- Omniauth wiki
【问题讨论】:
-
如消息所说,您已被重定向到登录页面,所以我认为您在那里有一些授权,您需要登录才能查看此页面。
-
问题是关于如何存根 shopify_app 身份验证/授权过程以使测试成功。
标签: ruby-on-rails testing shopify ruby-on-rails-5 omniauth