我有一个使用 devise / omniauth 的 rails 4 应用程序来允许通过 facebook 登录。该应用程序似乎工作,测试似乎也工作。但是,我有一项测试检查用户通过 facebook 登录但决定不授予权限的情况。
此测试正常工作,但将错误消息发送到 rspec 输出
$ rspec -fd --tag inspect
Sign in with facebook
new user
E, [2013-10-20T21:25:24.232573 #17137] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials encountered
does not grant permission
Finished in 0.21152 seconds
1 example, 0 failures
Randomised with seed 52495
或者
$ rspec
....E, [2013-10-20T21:25:24.232573 #17137] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials encountered.
............................................................
Finished in 0.95531 seconds
74 examples, 0 failures
Randomized with seed 2946
相关测试
require 'spec_helper.rb'
feature "sign in with facebook" do
context "new user" do
scenario "does not grant permission", inspect: true do
dont_sign_in_with_facebook
expect(notice_area).to have_content "Could not authenticate you from Facebook because \"Invalid credentials\""
end
end
end
和 authentication_helper
module Features
module AuthenticationHelpers
def dont_sign_in_with_facebook
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
visit "/users/auth/facebook"
end
end
end
我可以做些什么来抑制 rspec 输出中的错误消息吗?
编辑
我尝试实现@Shepmaster 描述的静音功能,但这并没有解决问题。然后尝试从 rspec 命令重定向错误:
rspec -fd --tag inspect 2> /dev/null
Sign in with facebook
new user
E, [2013-10-20T21:25:24.232573 #17137] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials encounterd
does not grant permission
Finished in 0.21152 seconds
1 example, 0 failures
Randomised with seed 65190
并重定向标准输出
rspec -fd --tag inspect > /dev/null
没有输出!
最后使用输出选项
rspec -fd --tag inspect -o /tmp/rspec.out 2> /dev/null
cat /tmp/rspec.out
Sign in with facebook
new user
does not grant permission
Finished in 0.21152 seconds
1 example, 0 failures
Randomised with seed 65190
使用@shepmaster 的答案回答
module Features
module AuthenticationHelpers
def dont_sign_in_with_facebook
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
silence_omniauth {visit "/users/auth/facebook"}
end
end
end