我有以下代码(删除了不相关的部分):
# Picture.rb
image_accessor :image_file do
puts "config: #{Config.get(:preprocess_image_resize).present?}"
end
image_accessor由dragonfly提供。
我想要存根Config.get
(在其他情况下适用于许多其他规格),但这里没有任何效果。
这是测试:
it "should resize the image file to the given value" do
Config.stub!(:get) { |arg| arg == :preprocess_image_resize ? '1x1' : false }
end
运行测试时,我希望在控制台中看到“config:true”。 但我总是得到“config:false”。
我无法解释原因 - 也许是因为块中的评估?
任何想法如何存根呢?
答案 0 :(得分:0)
这可能是Ruby中do...end and curly braces are not the same的奇怪案例之一。
如果是,则在 Config.stub!(:get)
之后运行该块,在这种情况下返回nil
。这可以解释为什么Config.get(:preprocess_image_resize).present?
总是false
。
我的建议:尝试将代码更改为
it "should resize the image file to the given value" do
Config.stub!(:get) do |arg|
arg == :preprocess_image_resize ? '1x1' : false
end
end
看看是否有帮助。