在rpsec 2.12中,我希望这个辅助方法定义能够工作:
module X
private
def build_them(type)
puts 'Catching the star'
end
end
context 'public/private instance methods' do
subject{ Class.new { extend(::X) } }
def subject.build(type)
puts "Throwing a star"
build_them(type)
end
it{ should respond_to :build}
end
实际结果是规格失败:
expected #<Class:0x00000002ea5f90> to respond to :build
我希望这个例子能够通过
有关如何正确执行此操作的任何建议吗?
答案 0 :(得分:1)
调用subject
而不传递一个块实际上会返回proc形式的'subject'块。这意味着在您的代码中执行此操作时:
def subject.build(type)
#...
end
您实际上是在 proc 本身定义'build',而不是proc返回的对象。
在it {...}
示例块中,对proc返回的对象执行期望,因此测试失败,如您所见。
要使测试通过,您需要在'subject'块将返回的实际对象上定义'build'方法:
module X
private
def build_them(type)
puts 'Catching the star'
end
end
describe 'public/private instance methods' do
subject {
extender = Class.new { extend(::X) }
def extender.build
puts "Throwing a star"
build_them(type)
end
extender
}
it { should respond_to :build }
end
请注意,describe
必须用作顶级关联,context
必须嵌套