Rspec +集成测试+ activemodel = confused :)

时间:2012-01-15 22:22:27

标签: ruby-on-rails rspec factory-bot

我有以下问题,我不明白: 我有一个用户模型:

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

和集成测试:

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

失败

User.exists?

NameError uninitialized constant User::User

替换上面提到的行
self.class.exists?

修复它.. 有人可以请我摆脱困惑吗? :) 提前谢谢..

1 个答案:

答案 0 :(得分:1)

看起来该方法在使用User命名空间的范围内运行,也许您是通过模块定义它(只是猜测)。顺便说一句,您可以写如下:

::User.exists?

它应该从root开始“命名空间解析”。