【发布时间】:2012-03-21 13:11:55
【问题描述】:
我有两个模型 - 租户和用户,每个租户将有_many 用户,我正在尝试找到一种方法来测试创建用户和自动分配租户的能力。当我尝试运行测试时,出现以下错误:
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)
【问题讨论】:
-
您的测试毫无意义 - lambda 对象不属于任何东西。除此之外,作为一个非 rspec 用户,我看不到像 belong_to(:tenant) 这样的东西的意义。找到新用户并断言其租户是 X 有什么问题?
-
嗨 noodl,我想这基本上就是我想要做的,但我想我是以错误的方式去做。有没有更简单的方法可以做到这一点,因为我不知道 lambda 是错误的方法,我是 Rails 新手,我的测试工作基于教程示例
标签: ruby-on-rails ruby-on-rails-3 rspec rspec2 belongs-to