0

I have the following code (removed unrelated parts):

# Picture.rb
image_accessor :image_file do
  puts "config: #{Config.get(:preprocess_image_resize).present?}"
end

image_accessor is provided by dragonfly.

I want to stub Config.get (which works great for many other specs in other scenarios), but here it does not have any effect.

This is the test:

it "should resize the image file to the given value" do
  Config.stub!(:get) { |arg| arg == :preprocess_image_resize ? '1x1' : false }
end

When running the test I expect to see "config: true" in the console. But I always get "config: false".

I cant explain why - perhaps because of the evaluation in the block?

Any ideas how to stub this then?

Robin
  • 866
  • 7
  • 19

1 Answers1

0

This might be one of those weird cases where do...end and curly braces are not the same in Ruby.

If so, the block is being run after Config.stub!(:get) which in that case returns nil. That would explain why Config.get(:preprocess_image_resize).present? is always false.

My advice: Try changing the code to

it "should resize the image file to the given value" do
  Config.stub!(:get) do |arg|
    arg == :preprocess_image_resize ? '1x1' : false
  end
end

and see if that helps.

Community
  • 1
  • 1
awendt
  • 13,195
  • 5
  • 48
  • 66