I have been creating a lot of Thor generators and Rails generators
during the past 4 months, but only this week did I find some good
examples of how to write tests for generators using
Rails::Generators::TestCase - in the "rails3-generators" project.

I prefer to write my tests in RSpec 2, so I was wondering if there is
an RSpec 2 equivalent out there yet?
If not, I will try writing my own RSpec 2 port... but I would be very
happy if anyone could help me in this effort, with suggestions or
anything.
Thanks!

Kristian

Test::Unit example
-------------------------

require 'generator_helper'

class Canable::Generators::ModelGeneratorTest <
Rails::Generators::TestCase
  destination File.join(Rails.root)
  tests Canable::Generators::ModelGenerator

  setup :prepare_destination
  setup :copy_routes

  def assert_model(name, options)
    assert_file "app/models/#{name}.rb" do |account|
      assert_class name.camelize, account do |klass|
        assert_match /include Canable::Ables/, klass

        assert_match(/userstamps!/, klass) if options[:userstamps]

        if method = options[:method]
          assert_match(/def #{method}_by?(user)/, klass) if method
        end
      end
    end
  end

  test "invoke with no arguments" do
    run_generator
  end

  # test include Canable:Ables statement is added
  test "invoke with model name" do
    name = 'house'
    run_generator(name)
    assert_model(name)
  end
end

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to