通过给定的测试测试白名单操作:
require 'spec_helper'
describe '/orders' do
describe 'POST /order' do
it 'whitelists attributes' do
expect_any_instance_of(Logger).to_not receive(:warn)
order = {
account: "1234",
user: "zach",
}
post '/order', order.to_json, {'Content-Type' => 'application/json'}
end
end
end
通过pry shell跳转到控制器成功将错误打印到屏幕:
RSpec::Mocks::MockExpectationError: (#<Logger:0x007f2c4e5c4fc8>).warn(#<Logger:0x007f2c4e5c4fc8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f2c4e5c4f50 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f2c4e5c4f00 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f2c4e5c4ed8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f2c4e5c4e60>>>>)
expected: 0 times with any arguments
received: 1 time with arguments: (#<Logger:0x007f2c4e5c4fc8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f2c4e5c4f50 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f2c4e5c4f00 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f2c4e5c4ed8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f2c4e5c4e60>>>>)
from /vagrant/vendor/bundle/ruby/2.1.0/gems/rspec-mocks-3.1.3/lib/rspec/mocks/error_generator.rb:213:in `__raise'
但测试总是通过......
Finished in 1 minute 2.38 seconds (files took 33.04 seconds to load)
1 example, 0 failures
Randomized with seed 17770