1

When I run rspec on a specific file:

rspec spec/models/my_namespace/my_model_spec.rb

I run into an error because rspec appears not to load app/models/my_namespace.rb, which contains the declaration of self.table_name_prefix. In fact, if I use pry and run MyNamepsace.table_name_prefix # => NoMethodError: undefined method ``table_name_prefix'.

When I try to query the db from Rails console (no rspec), it works, though: MyNamespace::MyModel.where(foo:'bar') # => [...]

Any idea what the problem or fix is?

Rails 3.2.14, Ruby 1.9.3, Debian

4
  • possible duplicate of Rails table_name_prefix missing Commented Aug 12, 2013 at 23:36
  • Thanks for the link to the other question, but the answer there doesn't really satisfy, and the question does not discriminate between rspec-testing and actually running the rails application. When it is run in an ordinary rails console (not rspec), the table_name is produced correctly, including the prefix. Commented Aug 12, 2013 at 23:58
  • besides which, the contention and the answer on that thread is that the static method on the module (Foo) is in no way linked to the model (Setting), but that's not the problem in my case; in my case, the module file appears not even to be loading because table_name_prefix appears not even to be defined on the module. Commented Aug 13, 2013 at 0:04
  • Have you included the rspec-rails gem? Are you requiring spec_helper? Commented Aug 13, 2013 at 3:09

1 Answer 1

1

At the top of your spec file:

require_relative '../../../app/models/my_namespace'

This will load the namespace file which specifies the table prefix.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.