5

我有一个使用 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
4

2 回答 2

12

我在 Rails 4.2 应用程序中遇到了同样的问题。修复方法是根据 README在初始化程序中将 OmniAuth 记录器设置为 Rails 记录器:

# config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger
于 2015-01-05T03:59:46.683 回答
6

最好的办法是找到打印该错误的代码,然后您可以确定是否有一种精确的方法可以消除该错误。

该错误消息有一个独特的拼写错误 (encountererd),在设计源中似乎没有发生(或曾经发生过)。也许它来自您的代码或其他宝石?快速的 github 搜索没有任何有用的结果。

添加

看来您没有完全复制并粘贴错误,所以我正在寻找一个永远不会出现的字符串!这就是错误的根源。您应该能够在测试期间将 OmniAuth 记录器设置为其他内容。这与下面的样式相同silence

def silence_omniauth
  previous_logger = OmniAuth.config.logger
  OmniAuth.config.logger = Logger.new("/dev/null")
  yield
ensure
  OmniAuth.config.logger = previous_logger
end

原来的

如果您找不到消息的来源,那么总是有核选项:在该测试期间完全禁用打印到标准输出流。This other SO question有一个如何做到这一点的例子,我将在这里修改和重现:

def silence
  previous_stdout, $stdout = $stdout, StringIO.new
  previous_stderr, $stderr = $stderr, StringIO.new
  yield
ensure
  # Restore the previous values of stdout and stderr
  $stdout = previous_stdout
  $stderr = previous_stderr
end

# And in your test
it 'does whatever' do
  silence { code.that_causes.the_warning }
end

重申一下,找到一个更窄的解决方案要好得多静音 stdout / stderr 无疑会使某人在测试期间打印出调试信息而什么也看不到时感到困惑。我这样说是因为上周刚刚发生在我身上。:-)

于 2013-10-26T14:08:39.983 回答