Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Generate a spec file. #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/generators/data_migration/data_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def create_data_migration
set_local_assigns!
migration_template "data_migration.rb", data_migrations_file_path
end

def create_data_migration_spec
template "data_migration_spec.rb", data_migrations_spec_path
end

protected

Expand All @@ -39,6 +43,22 @@ def migration_base_class_name
def data_migrations_file_path
File.join(data_migrations_path, "#{file_name}.rb")
end

# The path to the real generated migration.
def data_migrations_load_path
File.join(
data_migrations_path,
[@migration_number, "#{file_name}.rb"].join("_")
)
end

def data_migrations_spec_path
File.join(
"spec",
data_migrations_path,
[@migration_number, "#{file_name}_spec.rb"].join("_")
)
end

def data_migrations_path
DataMigrate.config.data_migrations_path
Expand Down
22 changes: 22 additions & 0 deletions lib/generators/data_migration/templates/data_migration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"
require Rails.root.join(%q{<%= data_migrations_load_path %>})

describe <%= migration_class_name %> do
let(:migrator) {
described_class.new.tap do |m|
m.verbose = false
end
}

describe "#up" do
subject { migrator.migrate(:up) }

pending "Needs tests"
end

describe "#down" do
subject { migrator.migrate(:down) }

pending "Needs tests"
end
end
17 changes: 17 additions & 0 deletions spec/generators/data_migration/data_migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@
end
end
end

describe :create_data_migration_spec do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
let(:data_migrations_spec_path) { 'spec/abc/my_migration_spec.rb' }

before do
DataMigrate.config.data_migrations_path = 'abc/'
end

it 'returns correct file path' do
expect(subject).to receive(:migration_template).with(
'data_migration_spec.rb', data_migrations_spec_path
)

subject.create_data_migration_spec
end
end
end