我正在寻找omniauth-facebook,devise,capybara,capybara-mechanize,(可能使用VCR)和rpsec集成测试的简单示例...
Capybara无法连接到外部网站。所以需要水豚机械化。也许录像机可以加快速度。此链接描述了如何获取测试Facebook用户:https://developers.facebook.com/docs/test_users/
使用模拟,如omniauth文档中所述,可以很好地测试模型和控制器的逻辑。这需要specs / helpers / omniauth.rb中的以下代码,并在:rspec集成测试之前调用set_omniauth。
然而,对于Facebook服务器进行真正的集成测试会很不错。当然,你想要标记这么慢的测试,这样他们就不会通过后卫和默认的rake任务。
# You can read about this gist at: http://wealsodocookies.com/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara
# which is for twitter. Below is for facebook
def set_omniauth(opts = {})
default = {:provider => :facebook,
:uuid => "1234",
:facebook => {
:email => "foobar@example.com",
:gender => "Male",
:first_name => "foo",
:last_name => "bar",
:nickname => "foo bar",
:image => 'http://graph.facebook.com/659307629/picture?type=square'
}
}
credentials = default.merge(opts)
provider = credentials[:provider]
user_hash = credentials[provider]
OmniAuth.config.test_mode = true
h = {
'provider' => provider,
'uid' => credentials[:uuid],
'info' => user_hash,
"extra" => {
"info" => user_hash
}
}
OmniAuth.config.mock_auth[provider] = CollectionUtility.deep_stringify_keys(h)
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