Testing generators that depend on ActiveRecord Model #11

Closed
treybean opened this Issue Jun 18, 2012 · 8 comments

Projects

None yet

3 participants

@treybean

I've got a generator that calls hook_for to invoke ActiveRecord's model generator. I know the generator works because I can execute it and it creates the model and migration successfully, but when I'm testing, it seems like the model_generator isn't being called. I'm not sure if I need to tell my testcase to include rails generators or rails or what exactly. Have you run into this?

I tried setting the orm to :active_record like you recommend in your blog, but I'm still getting failures about --orm not being defined.

My Generator:

require 'rails/generators'
require 'rails/generators/resource_helpers'

module Tandem
  module Generators
    class ResourceGenerator < ::Rails::Generators::NamedBase
      include ::Rails::Generators::ResourceHelpers

      desc "Create a tandem resource"

      argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"

      hook_for :orm, :in => :rails, :as => :model, :required => true

      source_root File.expand_path('../templates', __FILE__)

      def create_tandem_resource_controller
        template 'controller.rb', File.join('app/controllers/tandem', "#{controller_file_name}_controller.rb")
      end

      def create_views
        [:index, :show, :new, :edit, :_form].each do |view|
          template "#{view}.html.slim", File.join('app/views/tandem', "#{file_name.pluralize}", "#{view}.html.slim")
        end
      end

      def add_resource_route
        route "resources :#{file_name.pluralize}, :module => 'tandem'"
      end
    end
  end
end

And a small version of my spec file that demonstrates the problem:

require 'spec_helper'
require 'generators/tandem/resource/resource_generator'

describe Tandem::Generators::ResourceGenerator do
  destination File.expand_path("#{ENGINE_RAILS_ROOT}/spec/dummy/tmp", __FILE__)

  before do
    prepare_destination

    ::Rails::Generators.options[:rails][:orm] = :active_record

    FileUtils.mkdir_p("#{destination_root}/config")
    FileUtils.cp "#{ENGINE_RAILS_ROOT}/config/routes.rb", "#{destination_root}/config/routes.rb"
  end

  describe 'tandem resource controller' do
    before do
      run_generator %w(Spoke) 
    end

    subject { file('app/controllers/tandem/spokes_controller.rb') }

    it { should contain(/module Tandem/) }
    it { should contain(/class SpokesController < ::Tandem::ResourceController/) }
  end
end

Is this supposed to work or should I just be stubbing out the hook_for call?

@alexrothenberg
Owner

You are right this was broken. I just pushed a commit that I think will fix it (a588a29)

Can you please test your generator with the head of ammeter off github and let me know if it does what you need.

@treybean

Yes! Thank you! And thank you for the great library for testing our generators. Raising a glass to the green dots. Cheers!

@alexrothenberg
Owner

You're welcome (& thanks for the kind words)

I'll release a new version of ammeter with this shortly. I'll close this issue when I do.

@alexrothenberg
Owner

released 0.2.6 with this fix

@EppO
EppO commented Feb 18, 2013

Hello,

@alexrothenberg, sorry to reopen a 8 months old bug, but I have the same issue in my project and can't figure out how to set the orm setting.
I put this in my spec, in a before block:

::Rails::Generators.options[:rails][:orm] = :active_record
puts generator.options.inspect

this displays:

{"skip_namespace"=>false, "old_style_hash"=>false, "orm"=>false}

and so my specs are failing because of this unset orm setting and I use hook_for method in my generator.
I tried to tweak directly generator.options but options hash is frozen:

can't modify frozen Thor::CoreExt::HashWithIndifferentAccess
@EppO
EppO commented Feb 18, 2013

BTW if I run my generator manually in a fresh new rails project, it works fine.

@EppO
EppO commented Feb 19, 2013

Ok, just saw #12 that is related to my issue. I added a Rails::Application class in my spec_helper.rb just before the ammeter/init require line and also requiring rails/all that sets the ORM default. My generator.options are now correct:

{"skip_namespace"=>false, "old_style_hash"=>false, "orm"=>:active_record}

sorry for the inconvenience :-)

@alexrothenberg
Owner

Glad you found the answer. Sorry I was away on vacation so didn't see this sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment