Ruby 如何在rspec中调用类方法
我有以下的课堂教学方法Ruby 如何在rspec中调用类方法,ruby,ruby-on-rails-4,rspec,Ruby,Ruby On Rails 4,Rspec,我有以下的课堂教学方法 module Abc class Def class << self include Ghi::Lmn def mymethod(*args) puts 'class method' def value @value.nil? ? 'test' << name : @value end end end end 模块Abc 类别定义 类您可以这样调
module Abc
class Def
class << self
include Ghi::Lmn
def mymethod(*args)
puts 'class method'
def value
@value.nil? ? 'test' << name : @value
end
end
end
end
模块Abc
类别定义
类您可以这样调用您的方法:
Abc::Def.mymethod(1, 2, 3, :foo, :bar, :baz)
关于如何使用rspec
调用类方法的好链接。基本上,您可以在rspec
本身中创建对类方法的调用:
class FooBar
def initialize(foo, bar)
@foo = foo
@bar = bar
end
def output
puts @foo
puts @bar
end
end
describe Foo do
context bar do
subject { FooBar.new(<info>).output } # Create an instance of the class in the rspec
end
end
class FooBar
def初始化(foo,bar)
@foo=foo
@巴
结束
def输出
放@foo
放@bar
结束
结束
描述福多
上下文栏
subject{FooBar.new().output}#在rspec中创建类的实例
结束
结束
这是类方法,不是实例方法。谢谢。是否有任何方法也可以访问来自Ghi::Lmn的名称?更新的问题老实说,idk,这是个好问题,你应该问它!我很想找到答案。我假设您只需要像平常一样从调用本身访问它。Foo::Barr。