Skip to content
This repository
Browse code

Add integration test in cucumber to test rake task

  • Loading branch information...
commit 898baf2cae9f24faa32b9713569335d9d79fd7a1 1 parent 7ab5ba6
Prem Sichanugrist sikachu authored
63 features/rake_tasks.feature
... ... @@ -0,0 +1,63 @@
  1 +Feature: Rake tasks
  2 +
  3 + Background:
  4 + Given I generate a new rails application
  5 + And I run a rails generator to generate a "User" scaffold with "name:string"
  6 + And I run a paperclip generator to add a paperclip "attachment" to the "User" model
  7 + And I run a migration
  8 + And I add this snippet to the User model:
  9 + """
  10 + has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
  11 + """
  12 +
  13 + Scenario: Paperclip refresh thumbnails task
  14 + When I modify my attachment definition to:
  15 + """
  16 + has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
  17 + :styles => { :medium => "200x200#" }
  18 + """
  19 + And I upload the fixture "5k.png"
  20 + Then the attachment "medium/5k.png" should have a dimension of 200x200
  21 + When I modify my attachment definition to:
  22 + """
  23 + has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
  24 + :styles => { :medium => "100x100#" }
  25 + """
  26 + When I successfully run `bundle exec rake paperclip:refresh:thumbnails CLASS=User --trace`
  27 + Then the attachment "original/5k.png" should exist
  28 + And the attachment "medium/5k.png" should have a dimension of 100x100
  29 +
  30 + Scenario: Paperclip refresh metadata task
  31 + When I upload the fixture "5k.png"
  32 + And I swap the attachment "original/5k.png" with the fixture "12k.png"
  33 + And I successfully run `bundle exec rake paperclip:refresh:metadata CLASS=User --trace`
  34 + Then the attachment should have the same content type as the fixture "12k.png"
  35 + And the attachment should have the same file size as the fixture "12k.png"
  36 +
  37 + Scenario: Paperclip refresh missing styles task
  38 + When I upload the fixture "5k.png"
  39 + Then the attachment file "original/5k.png" should exist
  40 + And the attachment file "medium/5k.png" should not exist
  41 + When I modify my attachment definition to:
  42 + """
  43 + has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
  44 + :styles => { :medium => "200x200#" }
  45 + """
  46 + When I successfully run `bundle exec rake paperclip:refresh:missing_styles --trace`
  47 + Then the attachment file "original/5k.png" should exist
  48 + And the attachment file "medium/5k.png" should exist
  49 +
  50 + @wip
  51 + Scenario: Paperclip clean task
  52 + When I upload the fixture "5k.png"
  53 + And I upload the fixture "12k.png"
  54 + Then the attachment file "original/5k.png" should exist
  55 + And the attachment file "original/12k.png" should exist
  56 + When I modify my attachment definition to:
  57 + """
  58 + has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
  59 + validates_attachment_size :attachment, :less_than => 10.kilobytes
  60 + """
  61 + And I successfully run `bundle exec rake paperclip:clean CLASS=User --trace`
  62 + Then the attachment file "original/5k.png" should exist
  63 + But the attachment file "original/12k.png" should not exist
70 features/step_definitions/attachment_steps.rb
... ... @@ -0,0 +1,70 @@
  1 +module AttachmentHelpers
  2 + def fixture_path(filename)
  3 + File.expand_path("#{PROJECT_ROOT}/test/fixtures/#{filename}")
  4 + end
  5 +
  6 + def attachment_path(filename)
  7 + File.expand_path("public/system/attachments/#{filename}")
  8 + end
  9 +end
  10 +World(AttachmentHelpers)
  11 +
  12 +When /^I modify my attachment definition to:$/ do |definition|
  13 + in_current_dir do
  14 + File.open("app/models/user.rb", "w") do |file|
  15 + file.write <<-FILE
  16 + class User < ActiveRecord::Base
  17 + #{definition}
  18 + end
  19 + FILE
  20 + end
  21 + end
  22 +end
  23 +
  24 +When /^I upload the fixture "([^"]*)"$/ do |filename|
  25 + in_current_dir do
  26 + run_simple %(bundle exec rails runner "User.create!(:attachment => File.open('#{fixture_path(filename)}'))")
  27 + end
  28 +end
  29 +
  30 +Then /^the attachment "([^"]*)" should have a dimension of (\d+x\d+)$/ do |filename, dimension|
  31 + in_current_dir do
  32 + geometry = `identify -format "%wx%h" "#{attachment_path(filename)}"`.strip
  33 + geometry.should == dimension
  34 + end
  35 +end
  36 +
  37 +Then /^the attachment "([^"]*)" should exist$/ do |filename|
  38 + in_current_dir do
  39 + File.exists?(attachment_path(filename)).should be
  40 + end
  41 +end
  42 +
  43 +When /^I swap the attachment "([^"]*)" with the fixture "([^"]*)"$/ do |attachment_filename, fixture_filename|
  44 + in_current_dir do
  45 + require 'fileutils'
  46 + FileUtils.rm_f attachment_path(attachment_filename)
  47 + FileUtils.cp fixture_path(fixture_filename), attachment_path(attachment_filename)
  48 + end
  49 +end
  50 +
  51 +Then /^the attachment should have the same content type as the fixture "([^"]*)"$/ do |filename|
  52 + in_current_dir do
  53 + require 'mime/types'
  54 + attachment_content_type = `bundle exec rails runner "puts User.last.attachment_content_type"`.strip
  55 + attachment_content_type.should == MIME::Types.type_for(filename).first.content_type
  56 + end
  57 +end
  58 +
  59 +Then /^the attachment should have the same file size as the fixture "([^"]*)"$/ do |filename|
  60 + in_current_dir do
  61 + attachment_file_size = `bundle exec rails runner "puts User.last.attachment_file_size"`.strip
  62 + attachment_file_size.should == File.size(fixture_path(filename)).to_s
  63 + end
  64 +end
  65 +
  66 +Then /^the attachment file "([^"]*)" should (not )?exist$/ do |filename, not_exist|
  67 + in_current_dir do
  68 + check_file_presence([attachment_path(filename)], !not_exist)
  69 + end
  70 +end

0 comments on commit 898baf2

Please sign in to comment.
Something went wrong with that request. Please try again.