Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec + integration testing + activemodel = confused :)

I have following problem, which I don't understand: I have an User model:

class User < ActiveRecord::Base
...
  private
    def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end while User.exists?(column => self[column])
    end
end

and an integration test:

it "should sign an user in" do
  user = FactoryGirl.create(:user)
  visit root_path
  click_link "Sign in"
  fill_in :email, with: user.email
  fill_in :password, with: user.password
  click_button
  controller.should be_signed_in
  click_link "Sign out"
  controller.should_not be_signed_in
end

which fails on

User.exists?

with

NameError uninitialized constant User::User

replacing mentioned row with

self.class.exists?

fixes it.. Can someone please lead me out of confusion? :) thanks in advance..

like image 939
hlopko Avatar asked Apr 26 '26 21:04

hlopko


1 Answers

Looks like that method is running inside a scope namespaced with User, maybe you are defining it through a module (just guessing). By the way, you can write as following:

::User.exists?

And it should start the "namespace resolving" from root.

like image 146
jeffersongirao Avatar answered Apr 29 '26 11:04

jeffersongirao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!