RSpec 3.2 Model Testing Error in Rails Mountable Engine

32 views
Skip to first unread message

Adam Perry-Pelletier

unread,
Feb 2, 2016, 2:41:44 PM2/2/16
to rspec
In old RSpec, in a Rails Mountable Engine project with no database underneath. We have the following expectation set up.

    User.should_receive(:find_by_user_id_and_type).with(42, 'GREAT_USER').and_return(user)

The above worked fine.

In rspec 3.2, we changed to 

    allow(User).to receive(:find_by_user_id_and_type).with(42, 'GREAT_USER').and_return(user)

This now fails with the error: 

    ActiveRecord::StatementInvalid: Could not find table 'users'

It appears in rspec 3.4 when the expectation is set up, rspec does try to load the ActiveRecord properties from the database. The old rspec did not do this. This allowed us to write rspec tests against a Rails Mountable Engine project without actually having to load and configure a test database. The database for this project is loaded and available in the main project which uses this subproject.

Thanks in advance for any help.
Adam


Myron Marston

unread,
Feb 2, 2016, 2:46:49 PM2/2/16
to rs...@googlegroups.com
In rspec 3.2, we changed to 
    allow(User).to receive(:find_by_user_id_and_type).with(42, 'GREAT_USER').and_return(user)

This is not equivalent to what you had before. The equivalent of `User.should_receive` in the new syntax is `expect(User).to receive`.  `allow` is the equivalent of a stub.

As for the error you're getting: I believe it is related to verifying doubles.  It is trying to validate that `find_by_user_id_and_type` is a valid method -- but to verify that, the schema must be loaded to see if the model has `user_id` and `type` fields.  If you don't want partial doubles to have that kind of verification, you can turn it off with a config option:


HTH,
Myron

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/68e6bdcf-a228-45c4-bcd8-c12b1e31af1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Perry-Pelletier

unread,
Feb 2, 2016, 3:40:23 PM2/2/16
to rspec
Myron,

Thanks for the help, but the same thing still occurs. I had tried expect syntax and just retried.

expect(User).to receive(:find_by_user_id_and_type).with(42, 'GREAT_USER').and_return(user)

As before, the result is.

ActiveRecord::StatementInvalid: Could not find table 'users'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/model_schema.rb:229:in `yield'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/model_schema.rb:229:in `columns'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/model_schema.rb:249:in `column_names'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/model_schema.rb:262:in `column_methods_hash'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:74:in `all_attributes_exists?'
/Users/mp/.rvm/gems/ruby-2.1.2_hc-2.1.2/gems/activerecord-3.2.21/lib/active_record/dynamic_matchers.rb:27:in `method_missing'
./lib/hc/qb/create_or_update_invoice.rb:17:in `create_or_update'
./spec/create_or_update_invoice_spec.rb:233:in `block (4 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'

Unlike 1.x, if 3.4 even touches an ActiveRecord class it tries to load it from the schema, before it is actually used.

Thanks,
Adam

Myron Marston

unread,
Feb 2, 2016, 3:46:35 PM2/2/16
to rs...@googlegroups.com
Looking at your stack trace, it looks like the bit of code that's triggering ActiveRecord to try to load the schema is a `create_or_update` call at ./lib/hc/qb/create_or_update_invoice.rb:17.  Are you sure the RSpec stubbing line is what's causing the error?  The stack trace makes it look like it's not.

HTH,
Myron

Reply all
Reply to author
Forward
0 new messages