我正在为omniauth-facebook、devise、capybara、capybara-mechanize(可能带有VCR)和rpsec集成测试寻找一个简单的例子......
Capybara 无法连接到外部网站。所以需要水豚机械化。也许录像机可以加快速度。此链接描述了如何获取测试 facebook 用户:https ://developers.facebook.com/docs/test_users/
如omniauth 文档中所述,使用mock 可以很好地测试模型和控制器的逻辑。这需要 specs/helpers/omniauth.rb 中的以下代码,并在 rspec 集成测试的 :before 中调用 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