2012-03-21 46 views
0

我有兩個型號 - 用戶和用戶,每個租戶將have_many用戶,我試圖找到測試的一種方式,能夠創建一個用戶,並自動分配租戶。當我嘗試運行我收到以下錯誤測試:測試一個模式「belongs_to的」另一種模式

NoMethodError: 
    undefined method 'reflect_on_association' for Proc:Class 

租客代碼:

class Tenant < ActiveRecord::Base 
attr_accessible :name, :billing_email, :country 

validates :name,  :presence => true, 
         :length => { :maximum => 75 }, 
         :uniqueness => true 

validates :billing_email, :email => true 

has_many :users 
end 

用戶代碼:

class User < ActiveRecord::Base 
attr_accessible :email, :first_name, :last_name, :password, :tenant_id 

validates :email,  :presence => true, 
         :uniqueness => true, 
         :email => true 

validates :first_name, :presence => true, 
         :length => { :maximum => 50 } 

validates :last_name, :presence => true, 
         :length => { :maximum => 50 } 

validate :password_validation 

has_many :sessions, :dependent => :destroy 
belongs_to :tenant 

end 

測試嘗試:

lambda do 
    @attr = FactoryGirl.attributes_for(:user) 
    post users_path, :user => @attr 
    response.should be_success 
end.should belong_to(:tenant) 
+0

您的測試是沒有意義的 - 拉姆達對象不屬於任何東西了。這且不說,作爲非rspec的用戶,我不能看到的東西像belong_to點(:租客)。找到新用戶並聲稱其租戶是X是什麼問題? – noodl 2012-03-21 23:15:10

+0

嗨noodl,我想這基本上就是我希望做的,但我想,我在錯誤的方式去了解它。是否有這樣做,因爲我不知道該拉姆達是錯誤的做法的任何更簡單的方法,我是新來的鐵軌和我從實例教程立足我的測試工作 – Jay 2012-03-22 14:32:30

回答

0

這並不是特別清楚你想要做什麼達到這裏。斷言一個對象「belongs_to」另一個味道不好。你真的關心belongs_to被用於協會嗎?很多時候似乎RSpec的測試結果爲重複測試中的代碼,而無需實際測試中有用的東西。

更有可能的是你有興趣,看看是否張貼到你的控制器的結果產生正確的結果。既然你已經不是我們顯示控制器,因爲我覺得RSpec的是毫無意義的噪音,這裏就是我想解決這個..

assert_difference ->{User.count} do 
    post: users_path, user: FactoryGirl.attributes_for(:user) 
end 
# Assuming your users table is cleared before each test.. 
assert_kind_of Tenant, User.first.tenant