【问题标题】:rails omniauth error in rspec outputrspec输出中的rails omniauth错误
【发布时间】:2013-10-20 21:57:41
【问题描述】:

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

【问题讨论】:

  • 您能找到打印该错误的代码吗?我将从您的源代码的git grep 开始。如果它不存在,那么我会在你的 gem 中搜索(~/.rvm/gems,如果你使用 rvm),如果它不存在,它一定来自另一个文件或网络之类的地方。追踪此日志的来源,我们可以弄清楚如何禁用它。

标签: ruby-on-rails facebook rspec omniauth


【解决方案1】:

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

# config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger

【讨论】:

  • 这应该是公认的答案——在没有一堆复杂的测试设置的情况下完全符合 OP 的要求。此外,正确设置记录器有助于调试开发和生产中的问题,因为错误实际上最终出现在日志中,而不是 STDOUT 或 STDERR。
  • 我试过这个但是解决方案,但似乎omniauth仍在登录到标准输出。请参阅我的问题stackoverflow.com/questions/32449040/…
【解决方案2】:

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

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

已添加

看来您没有完全复制并粘贴错误,所以我正在寻找一个永远不会出现的字符串!这是source of the error。您应该能够在测试期间将 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

原创

如果您找不到消息的来源,那么总是有核选项:在该测试期间完全禁用打印到标准输出流。另一个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 无疑会让某人在测试期间打印出调试信息而什么也看不到时感到困惑。我这样说是因为上周刚刚发生在我身上。 :-)

【讨论】:

  • 感谢您的建议 - 如果我在测试中打印到 stderr,静音功能似乎效果很好 - 但不会阻止我看到的问题 - 看起来消息正在发送到标准输出(问题更新细节)
  • 同样的静音技术也适用于标准输出。我已经更新了答案以包含它。
  • 我已经尝试过修改您的答案以处理标准输出,但这也不起作用 - 我可以看到静默功能就像我在 puts$stderrr.puts函数我可以隐藏输出,但这并没有隐藏运行我现有测试的输出。再次感谢您的帮助。
  • 我添加了一个特定于 OmniAuth 的版本,也许这会缩小范围。
  • 成功了,非常感谢 - 不知道复制错误消息时发生了什么
猜你喜欢
  • 1970-01-01
  • 2012-01-21
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
相关资源
最近更新 更多