thank_you
thank_you

Reputation: 11107

Rails Has One Rspec Test tells me Unitialized Constant

I have a have one relationship between two models. The relationship works, the problem however is that when I write a Rspec test with shoulda matchers I get returned an error.

 Failure/Error: it { should have_one(:userresetpassword)}
 NameError:
   uninitialized constant User::Userresetpassword

My code in the user.rb

has_one :userresetpassword

My rspec test

describe "associations" do
  it { should have_one(:userresetpassword)}
end

Upvotes: 0

Views: 583

Answers (1)

Daniel Evans
Daniel Evans

Reputation: 6808

Your association is mis-named, and either rails, shoulda-matchers or both are having a hard time guessing the class name from it.

You have two choices. First, and preferable is to rename the association to be conventional:

has_one :user_reset_password

This will allow rails to correctly guess the classname UserResetPassword

Second, you could simply remove the guesswork and tell rails what the classname is. This is only preferable if you can't or very much do not want to change the association name.

has_one :userresetpassword, :class_name => "UserResetPassword"

Upvotes: 2

Related Questions