在 rspec 中没有任何预期时有记录器的错误消息
Have logger's error message when not expecting any in rspec
在我的一份规范中,我有以下期望:
expect(Rails.logger).not_to receive(:error)
问题是当它失败时,我没有关于错误本身的有用信息:
Failure/Error: expect(Rails.logger).not_to receive(:error)
(#<ActiveSupport::Logger:0x000000000b85dfa8 @level=1, @progname=nil, @default_formatter=#<Logger::Formatter:0x000000000b85df08 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x000000000b17f510 @datetime_format=nil, @thread_key="activesupport_tagged_logging_tags:93059720">, @logdev=#<Logger::LogDevice:0x000000000b85deb8 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/home/circleci/myproject/log/test.log>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000000b85de40>>, @local_levels=#<Concurrent::Map:0x000000000b85dda0 entries=0 default_proc=nil>, @thread_hash_level_key=:"ThreadSafeLogger#96661460@level">).error(*(any args))
expected: 0 times with any arguments
received: 1 time with any arguments
有没有办法以某种方式获取错误的内容?
我是否应该更改编写规范的方式以防万一失败?
在我的情况下,奇怪的部分似乎是 received: 1 time with any arguments
,我期待的是 received: 1 time with arguments: ("foo")
。
我没有在 rspec 文档中找到如何执行此操作,但这里有一个关于如何访问错误信息的想法:
allow(Rails.logger).to receive(:error) { |params| raise params.to_s }
这不是惯用的,但是当在记录器上调用 error
时你的测试中断了
这有点 hacky,但您可以故意进行失败测试以查看有关错误本身的信息
expect(Rails.logger).to receive(:error).with('foo')
在我的一份规范中,我有以下期望:
expect(Rails.logger).not_to receive(:error)
问题是当它失败时,我没有关于错误本身的有用信息:
Failure/Error: expect(Rails.logger).not_to receive(:error)
(#<ActiveSupport::Logger:0x000000000b85dfa8 @level=1, @progname=nil, @default_formatter=#<Logger::Formatter:0x000000000b85df08 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x000000000b17f510 @datetime_format=nil, @thread_key="activesupport_tagged_logging_tags:93059720">, @logdev=#<Logger::LogDevice:0x000000000b85deb8 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/home/circleci/myproject/log/test.log>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000000b85de40>>, @local_levels=#<Concurrent::Map:0x000000000b85dda0 entries=0 default_proc=nil>, @thread_hash_level_key=:"ThreadSafeLogger#96661460@level">).error(*(any args))
expected: 0 times with any arguments
received: 1 time with any arguments
有没有办法以某种方式获取错误的内容? 我是否应该更改编写规范的方式以防万一失败?
在我的情况下,奇怪的部分似乎是 received: 1 time with any arguments
,我期待的是 received: 1 time with arguments: ("foo")
。
我没有在 rspec 文档中找到如何执行此操作,但这里有一个关于如何访问错误信息的想法:
allow(Rails.logger).to receive(:error) { |params| raise params.to_s }
这不是惯用的,但是当在记录器上调用 error
时你的测试中断了
这有点 hacky,但您可以故意进行失败测试以查看有关错误本身的信息
expect(Rails.logger).to receive(:error).with('foo')