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)
您的測試是沒有意義的 - 拉姆達對象不屬於任何東西了。這且不說,作爲非rspec的用戶,我不能看到的東西像belong_to點(:租客)。找到新用戶並聲稱其租戶是X是什麼問題? – noodl 2012-03-21 23:15:10
嗨noodl,我想這基本上就是我希望做的,但我想,我在錯誤的方式去了解它。是否有這樣做,因爲我不知道該拉姆達是錯誤的做法的任何更簡單的方法,我是新來的鐵軌和我從實例教程立足我的測試工作 – Jay 2012-03-22 14:32:30