RSpec, Rack/Test, Sinatra message expectation always passes even though an error gets thrown

63 Views Asked by At

Testing a whitelisting operation via the given the test:

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

Jumping into the controller via pry shell successfully prints the error to screen:

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'

But the test always passes...

Finished in 1 minute 2.38 seconds (files took 33.04 seconds to load)
1 example, 0 failures

Randomized with seed 17770
0

There are 0 best solutions below