使用不同的提供程序为Devise OAuth创建功能规范

时间:2015-08-09 20:58:53

标签: ruby-on-rails ruby devise

我正在编写一个100%测试覆盖率的Rails应用程序。我有Capybara的功​​能规格,用户名和密码登录,但我没有通过Facebook或LinkedIn登录的规格。

我没有在设计OmniAuth页面上找到答案。这可以测试吗?我不应该测试这个吗?

1 个答案:

答案 0 :(得分:1)

我会看看https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

所以我最终在 support / omniauth.rb 文件中创建了2个帮助器:

def set_omniauth(opts = {})
  default = {:provider => :facebook,
             :uuid     => "1234",
             :facebook => {
                            :email => "foobar@example.com",
                            :gender => "Male",
                            :first_name => "foo",
                            :last_name => "bar"
                          }
            }

  credentials = default.merge(opts)
  provider = credentials[:provider]
  user_hash = credentials[provider]

  OmniAuth.config.test_mode = true

  OmniAuth.config.mock_auth[provider] = {
    'uid' => credentials[:uuid],
    "extra" => {
    "user_hash" => {
      "email" => user_hash[:email],
      "first_name" => user_hash[:first_name],
      "last_name" => user_hash[:last_name],
      "gender" => user_hash[:gender]
      }
    }
  }
end

def set_invalid_omniauth(opts = {})

  credentials = { :provider => :facebook,
                  :invalid  => :invalid_crendentials
                 }.merge(opts)

  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[credentials[:provider]] = credentials[:invalid]

end

有了这个甜蜜的设置,我现在可以在我的测试中有多个默认值,这使得更改非常干净:

background do
   set_omniauth()
   click_link_or_button 'Sign up with Facebook'
end

快乐黑客