Permalink
Browse files

Add test for generator

  • Loading branch information...
1 parent a2a4c7a commit 03700c86970cdc3717398f1e9dc382222d5632fe @sikachu sikachu committed Apr 9, 2012
Showing with 78 additions and 1 deletion.
  1. +0 −1 lib/generators/paperclip/paperclip_generator.rb
  2. +78 −0 test/generator_test.rb
View
1 lib/generators/paperclip/paperclip_generator.rb
@@ -29,5 +29,4 @@ def migration_file_name
def migration_class_name
migration_name.camelize
end
-
end
View
78 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.