How can I test the processing with rspec ?

1,178 views
Skip to first unread message

tol...@gmail.com

unread,
Apr 23, 2016, 4:40:29 PM4/23/16
to Shrine

I've succeeded to implement this from reading a previous question: 
image = File.open("spec/fixtures/image.jpg")
                                             
it
"should upload image" do
  uploader
= ImageUploader.new(:store)
  uploaded_file
= uploader.upload(image)
  expect
(uploaded_file).to be_present
end

but now I wan to test if it's process the image to have a couple of different sizes..
let them be :box, :card for example.
also, if I want to test the background job, i.e if I processed the image in the background.

thanks for the work.

Janko Marohnić

unread,
Apr 23, 2016, 6:53:23 PM4/23/16
to Shrine, tol...@gmail.com
Since you define all processing inside ImageUploader#process, you can simply call that method directly and test its output:

image = File.open("spec/fixtures/image.jpg")

                                             
it
"should process image" do
  uploader
= ImageUploader.new(:store)
  processed
= uploader.process(image, phase: :store)
  expect
(processed[:box]).to ...
  expect
(processed[:card]).to ...
end

If you want to test that processing is happening inside a background job, your backgrounding library should have a mechanism for controlling when jobs are executed. This is how you could do it with Sidekiq, for example if you have a Photo model which has an "image" attached:

require "sidekiq/testing"

it
"should process in a background job" do
  photo
= Photo.create(image: image)
  expect
(photo.reload.image).not_to be_a(Hash) # not processed
 
PromoteJob.drain
  expect
(photo.reload.image).to be_a(Hash) # processed
end

Regards,
Janko

Ahmad Abd-Elghany

unread,
Apr 24, 2016, 7:35:26 AM4/24/16
to Janko Marohnić, Shrine
Thanks a lot! 
I HUG YOU :*

Ahmad Abd-Elghany

unread,
Apr 25, 2016, 3:48:09 PM4/25/16
to Shrine
Just as a reminder, I've got an error while testing it:

require 'image_processing/mini_magick'

class ImageUploader < Shrine
  include ImageProcessing::MiniMagick

  plugin :determine_mime_type
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :validation_helpers
  plugin :versions, names: [:original, :card, :box]
  plugin :pretty_location

  Attacher.validate do
    validate_max_size 8.megabytes, message: 'is too large(max is 8 MB)'
    validate_mime_type_inclusion ['image/jpeg', 'image/jpg', 'image/png']
  end

  def process(io, context)
    case context[:phase]
    when :store
      card = resize_to_fill!(io.download, 300, 300, gravity: "Center")
      box  = resize_to_fill!(io.download, 600, 600, gravity: "Center")
      { original: io, card: card, box: box }
    end
  end
end
 

require 'rails_helper'

RSpec.describe ImageUploader, type: :model do
  image = File.open("spec/fixtures/image.jpg")

  it "should process images and extract box size" do
    uploader = ImageUploader.new(:store)
    processed = uploader.process(image, phase: :store)
    expect(processed[:box]).to be_present
  end

  it "should process images and extract card size" do
    uploader = ImageUploader.new(:store)
    processed = uploader.process(image, phase: :store)
    expect(processed[:card]).to exist
  end
end

the error was: 
  1) ImageUploader should process images and extract box size
     Failure/Error: card = resize_to_fill!(io.download, 300, 300, gravity: "Center")
     
     NoMethodError:
       undefined method `download' for #<File:spec/fixtures/cheero.jpg>
     # ./app/models/image_uploader.rb:21:in `process'
     # ./spec/models/image_uploader_spec.rb:8:in `block (2 levels) in <top (required)>'

  2) ImageUploader should process images and extract card size
     Failure/Error: card = resize_to_fill!(io.download, 300, 300, gravity: "Center")
     
     NoMethodError:
       undefined method `download' for #<File:spec/fixtures/cheero.jpg>
     # ./app/models/image_uploader.rb:21:in `process'
     # ./spec/models/image_uploader_spec.rb:14:in `block (2 levels) in <top (required)>'



Janko Marohnić

unread,
Apr 25, 2016, 7:56:30 PM4/25/16
to Shrine
Ah, sorry, my bad. In the "store" phase `io` is not a raw file, it's a Shrine::UploadedFile representing a cached file. So you need to pass a cached file to `#process`:

require 'rails_helper'

RSpec.describe ImageUploader, type: :model do
  image
= File.open("spec/fixtures/image.jpg")

  cached
= ImageUploader.new(:cache).upload(image)


  it
"should process images and extract box size" do
    uploader
= ImageUploader.new(:store)

    processed
= uploader.process(cached, phase: :store)

    expect
(processed[:box]).to be_present
 
end

  it
"should process images and extract card size" do
    uploader
= ImageUploader.new(:store)

    processed
= uploader.process(cached, phase: :store)

    expect
(processed[:card]).to exist
 
end
end

Regards,
Janko

Ahmad Abd-Elghany

unread,
Apr 25, 2016, 7:59:21 PM4/25/16
to Janko Marohnić, Shrine
Interesting, do you consider writing a piece about the design of shrine?
I've read most of the code, but a blog -lengthy one -about it.
Will be great, as I'd like to start contributing to it.

--
You received this message because you are subscribed to a topic in the Google Groups "Shrine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-shrine/9upc1ADpy3M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/bb61c884-8a07-42ae-8a28-46468349e7a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Janko Marohnić

unread,
May 1, 2016, 1:14:06 AM5/1/16
to Ahmad Abd-Elghany, Shrine
That is a good idea, I've been meaning to a guide like Sequel Object Model, where I explain Shrine's design, mainly what is each class responsible for. I will let you know when I finish it.

Regards,
Janko

Janko Marohnić

unread,
May 7, 2016, 12:48:56 PM5/7/16
to Ahmad Abd-Elghany, Shrine

Ahmad Abd-Elghany

unread,
May 7, 2016, 2:42:36 PM5/7/16
to Shrine
😍 Thanks man.
Reply all
Reply to author
Forward
0 new messages