2012-01-15 48 views
3

我有以下問題,我不明白: 我有一個用戶模式:Rspec的+集成測試+ activemodel的困惑= :)

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

貌似該方法與用戶名稱空間的一個範圍內運行,也許你是通過模塊定義它(只是猜測)。順便說一下,你可以寫如下:

::User.exists? 

它應該從根開始「名稱空間解析」。

+0

這真的很奇怪,我發現我在spec_helper.rb中有一個拼寫錯誤,所以我沒有在測試環境中運行規範。在測試中運行它們似乎修復了它。另一個有趣的事情是,該規範通過rspec命令傳遞,但不通過自動測試..我會寫更多後,我會深入挖掘。感謝::的事情,我不知道:) – mhlopko 2012-01-16 22:45:10