Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add test for generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sikachu committed Apr 9, 2012
1 parent a2a4c7a commit 03700c8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/generators/paperclip/paperclip_generator.rb
Expand Up @@ -29,5 +29,4 @@ def migration_file_name
def migration_class_name def migration_class_name
migration_name.camelize migration_name.camelize
end end

end end
78 changes: 78 additions & 0 deletions test/generator_test.rb
@@ -0,0 +1,78 @@
require './test/helper'
require 'rails/generators'
require 'generators/paperclip/paperclip_generator'

class GeneratorTest < Rails::Generators::TestCase
tests PaperclipGenerator
destination File.expand_path("../tmp", File.dirname(__FILE__))
setup :prepare_destination

context 'running migration' do
context 'with single attachment name' do
setup do
run_generator %w(user avatar)
end

should 'create a correct migration file' do
assert_migration 'db/migrate/add_attachment_avatar_to_user.rb' do |migration|
assert_match /class AddAttachmentAvatarToUser/, migration

assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up
assert_match /add_column :users, :avatar_content_type, :string/, up
assert_match /add_column :users, :avatar_file_size, :integer/, up
assert_match /add_column :users, :avatar_updated_at, :datetime/, up
end

assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down
assert_match /remove_column :users, :avatar_content_type/, down
assert_match /remove_column :users, :avatar_file_size/, down
assert_match /remove_column :users, :avatar_updated_at/, down
end
end
end
end

context 'with multiple attachment names' do
setup do
run_generator %w(user avatar photo)
end

should 'create a correct migration file' do
assert_migration 'db/migrate/add_attachment_avatar_photo_to_user.rb' do |migration|
assert_match /class AddAttachmentAvatarPhotoToUser/, migration

assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up
assert_match /add_column :users, :avatar_content_type, :string/, up
assert_match /add_column :users, :avatar_file_size, :integer/, up
assert_match /add_column :users, :avatar_updated_at, :datetime/, up
assert_match /add_column :users, :photo_file_name, :string/, up
assert_match /add_column :users, :photo_content_type, :string/, up
assert_match /add_column :users, :photo_file_size, :integer/, up
assert_match /add_column :users, :photo_updated_at, :datetime/, up
end

assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down
assert_match /remove_column :users, :avatar_content_type/, down
assert_match /remove_column :users, :avatar_file_size/, down
assert_match /remove_column :users, :avatar_updated_at/, down
assert_match /remove_column :users, :photo_file_name/, down
assert_match /remove_column :users, :photo_content_type/, down
assert_match /remove_column :users, :photo_file_size/, down
assert_match /remove_column :users, :photo_updated_at/, down
end
end
end
end

context 'without required arguments' do
should 'not create the migration' do
silence_stream(STDERR) { run_generator %w() }
assert_no_migration 'db/migrate/add_attachment_avatar_to_user.rb'
end
end
end
end

0 comments on commit 03700c8

Please sign in to comment.