我很确定我在这里遗漏了一个基本错误,所以我希望另一组眼睛可能会有所帮助。我正在使用 Rails 3、Ruby 1.9.2 和 Rspec 2。
我想在模型上定义动态类方法,以便我可以在将可分配对象(例如帐户)添加到系统时返回它们的基本角色。例如:
BaseRole.creator_for_account
通过控制台一切正常:
ruby-1.9.2-p180 :003 > BaseRole.respond_to?(:creator_for_account)
=> true
但是当我为任何类方法运行规范时,我得到一个
NoMethodError
无论我在哪里调用规范中的方法。我假设有关我如何动态声明方法的某些内容与 RSpec 不兼容,但我似乎无法弄清楚原因。lib 目录是自动加载的路径,并且方法为 respond_to? 返回 true。
# /lib/assignable_base_role.rb
module AssignableBaseRole
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
BaseRole.all.each do |base_role|
role_type = RoleType.find(base_role.role_type_id)
assignable_name = base_role.assignable_type.downcase
method = "#{role_type.name}_for_#{assignable_name}"
define_method(method) do
self.where(:role_type_id => role_type.id,
:assignable_type => assignable_name).first
end
end
end
end
然后在 BaseRole 中包含 Module
# /models/base_role.rb
class BaseRole < ActiveRecord::Base
include AssignableBaseRole
belongs_to :role
belongs_to :role_type
......
......
end
然后在我的规范中:
it "adds correct authority for creator role" do
create_assignment
base_role = BaseRole.creator_for_account # <== NoMethodError here
user1 = Factory.create(:user)
account.users << user1
user1.roles_for_assignable(account).should include(base_role.role)
end
最佳答案
您的项目或规范中是否有另一个同名的类,但没有添加动态方法?我和你遇到了完全相同的问题,重命名其中一个类修复了它。
我的猜测是另一个类首先加载
关于ruby-on-rails-3 - RSpec 中的动态定义方法抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021242/