ammeter icon indicating copy to clipboard operation
ammeter copied to clipboard

Generators calling 'generate' method fail.

Open dnoonan opened this issue 12 years ago • 7 comments

I have a generator that calls the generate method to take advantage of built-in migration functionality but my specs fail when checking for the existence of the created migrations.

The generator is in a gem.

The console contains this output: /home/rails/.rbenv/versions/1.9.2-p290/bin/ruby: No such file or directory -- script/rails (LoadError)

I've tried all the examples I could find from the blog http://www.alexrothenberg.com/2011/10/10/ammeter-the-way-to-write-specs-for-your-rails-generators.html, to the readme to the rspec repository and nothing I try seems to work; I don't think the generate calls are working correctly (as evidenced by the LoadError above).

Cheers, Devon

Sample Generator:

module Winner module Generators class MigrationsGenerator < Rails::Generators::Base argument :models, :type => :array, :banner => "model1 model2 model3...", :required => true

  #Create the migrations
  def create_migrations
    generate("migration", "AwesomeSauce")

    models.each do |model|
      create_migration(model)
    end
  end

  protected

  def create_migration(model_name)
    generate("migration", "AddFieldsTo#{model_name} newfield1:string newfield2:datetime")
  end

end

end end

Sample Test:

require 'spec_helper'

require 'rails/generators/winner/migrations_generator'

describe Winner::Generators::MigrationsGenerator do destination File.expand_path("../../../../tmp", FILE) #it's in lib/generators/winner/ NOT lib/generators/winner/models/

before { prepare_destination }

it 'should create a single migration with one argument' do run_generator %w(Model1)

migration_file('db/migrate/add_fields_to_model1.rb').should exist

end

end

dnoonan avatar Jul 30 '12 21:07 dnoonan

Yes I don't think I ever tried (or knew about) the generate command.

It looks like it basically shells out to script/rails g to run another generator and that's breaking because ammeter sticks the generated output outside your source project. I think you should be able to get your test to run by adding a bit to your before so it looks like:

before do 
  prepare_destination
  %w(config script).each do |dir|
    `ln -s #{Rails.root + dir} #{destination_root}`
  end
end

Let me know if this works and I'll move it into the prepare_destination method of the gem.

alexrothenberg avatar Jul 31 '12 13:07 alexrothenberg

Hey Alex.

Not sure if this issue was ever fixed, but I am stuck with the same problem. I tried the above piece of code in before, but it doesn't solve the issue.

Let me know how I can help, or what direction I should look in. I will try to crack the problem.

subhashb avatar Feb 12 '13 06:02 subhashb

Hmm I thought the before code made it work. Do you have an example of a failing project?

alexrothenberg avatar Feb 12 '13 14:02 alexrothenberg

Coincidentally, I do!! :) I am building a versioning gem which stores version data on a Big Data platform seamlessly. You can take a look here: https://github.com/subhashb/footprint

2 Specs that are failing because of the above problem are marked as pending, in these files:

  • footprint/spec/generators/rspec/document/document_generator_spec.rb
  • footprint/spec/generators/rspec/install/install_generator_spec.rb

subhashb avatar Feb 13 '13 05:02 subhashb

I think the problem is that in order to run your generator you need a rails app. The install generator will invoke 'script/rails' which doesn't exist while you're testing.

I'm trying to see if i can put together an example that works for your gem that will be something like what rspec-rails does by creating a rails app and using it while testing - see https://github.com/rspec/rspec-rails/blob/master/Rakefile#L47

alexrothenberg avatar Feb 14 '13 14:02 alexrothenberg

I got the generator specs working in your gem and sent a pull request -> https://github.com/subhashb/footprint/pull/1

Overall it seems like a lot to ask a gem author to do but I'm not sure that it could be pushed into ammeter in a general enough way (at least I can't think of how to do it right now). If you think it would be possible that'd be great to know.

Hope this helps.

alexrothenberg avatar Feb 14 '13 16:02 alexrothenberg

Hi @alexrothenberg, can you have a look at this issue in my project ? I'm having the same problem, but I couldn't resolve it using the infos in this thread. Thanks in advance.

alain-andre avatar Apr 13 '15 19:04 alain-andre