rspec / rspec-activemodel-mocks

Add stub_model and mock_model to rspec-mocks
MIT License
119 stars 32 forks source link

stub_model still requires a database connection #26

Open sirlark opened 8 years ago

sirlark commented 8 years ago

I am trying to use stub_model to test non-database accessing methods on a Rails/ActiveRecord model specified in a common code library, but when I write

rip_entry = stub_model(RipEntry).as_new_record

in my specs, I get an ActiveRecord::ConnectionNotEstablished. My minimal spec code is here

require "spec_helper"

describe RipEntry do

  # spec description, path, expected value, expected error type (or nil), expected result (true/false)
  path_specs = {
    "valid paths" => [
      ["can access simple paths with a single matching value", "id:status", "active", nil, true],
      # there are several dozen entries here, split across various context keys
    ]
  }

  path_specs.each do |context_desc, example_specs|
    context "#{context_desc}" do
      example_specs.each do |example|
        it "#{example[0]}" do
          rip_entry = stub_model(RipEntry).as_new_record
          allow(rip_entry).to receive(:rip_path).and_return(example[1])
          allow(rip_entry).to receive(:rip_values).and_return(example[2])
          allow(rip_entry).to receive(:rip_helper).and_return(rip_helper_instance)
          if example[3].nil?
            expect(rip_entry.satisfied?).to eq(example[4])
          else
            expect(rip_entry.satisfied?).to raise_error(*example[3])
          end
        end
      end
    end
  end

end

Which, when run produces this output

$ rspec spec/common/models/rip_entry_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

RipEntry
  valid paths
    can access simple paths with a single matching value (FAILED - 1)

Failures:

  1) RipEntry valid paths can access simple paths with a single matching value
     Failure/Error: rip_entry = stub_model(RipEntry).as_new_record

     ActiveRecord::ConnectionNotEstablished:
       ActiveRecord::ConnectionNotEstablished
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/model_schema.rb:229:in `columns'
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/model_schema.rb:244:in `column_defaults'
     # /usr/lib/ruby1.9/gems/1.9.1/gems/activerecord-3.2.22.2/lib/active_record/base.rb:482:in `initialize'
     # ./spec/common/models/rip_entry_spec.rb:16:in `block (5 levels) in <top (required)>'

Finished in 0.0016 seconds (files took 7.53 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/common/models/rip_entry_spec.rb:16 # RipEntry valid paths can access simple paths with a single matching value