我在rspec中测试时遇到了困难:
class RootOrganization
include ClassLevelInheritableAttributes
inheritable_attributes :role
@role = "Admin"
end
class Organization < RootOrganization
end
end
class ChildOrganizationOne < Organization
end
p ChildOrganizationOne.role #=> "Admin"
ChildOrganizationOne.role = "User"
p ChildOrganizationOne.role #=> "User"
有谁知道如何在rspec中设置这些变量?我想表明如果 RootOrganization.role =&#34; Admin&#34;和Organization.role =&#34;用户&#34; ChildOrganizationOne.role应该等于&#34; User&#34;?
(以下是为参考而创建的模块)
module ClassLevelInheritableAttributes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def inheritable_attributes(*args)
@inheritable_attributes ||= [:inheritable_attributes]
@inheritable_attributes += args
args.each do |arg|
class_eval %(
class << self; attr_accessor :#{arg} end
)
end
@inheritable_attributes
end
def inherited(subclass)
@inheritable_attributes.each do |inheritable_attribute|
instance_var = "@#{inheritable_attribute}"
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
end
end
end
end
答案 0 :(得分:1)
您应该stub role
个属性来返回需求值:
it "returns child organization as user" do
allow(RootOrganization).to receive(:role).and_return("Admin")
allow(Organization).to receive(:role).and_return("User")
expect(ChildOrganizationOne.role).to eq "User"
end