aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/helper_generator_test.rb
blob: 6d7168738e7289cb4e42b79d2513098720d60b21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'generators/generators_test_helper'
require 'generators/rails/helper/helper_generator'

ObjectHelper = Class.new
AnotherObjectHelperTest = Class.new

class HelperGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  arguments %w(admin)

  def test_helper_skeleton_is_created
    run_generator
    assert_file "app/helpers/admin_helper.rb", /module AdminHelper/
  end

  def test_invokes_default_test_framework
    run_generator
    assert_file "test/unit/helpers/admin_helper_test.rb", /class AdminHelperTest < ActionView::TestCase/
  end

  def test_logs_if_the_test_framework_cannot_be_found
    content = run_generator ["admin", "--test-framework=rspec"]
    assert_match /rspec \[not found\]/, content
  end

  def test_check_class_collision
    content = capture(:stderr){ run_generator ["object"] }
    assert_match /The name 'ObjectHelper' is either already used in your application or reserved/, content
  end

  def test_check_class_collision_on_tests
    content = capture(:stderr){ run_generator ["another_object"] }
    assert_match /The name 'AnotherObjectHelperTest' is either already used in your application or reserved/, content
  end

  def test_namespaced_and_not_namespaced_helpers
    run_generator ["products"]

    # We have to require the generated helper to show the problem because
    # the test helpers just check for generated files and contents but
    # do not actually load them. But they have to be loaded (as in a real environment)
    # to make the second generator run fail
    require "#{destination_root}/app/helpers/products_helper"

    assert_nothing_raised do
      begin
        run_generator ["admin::products"]
      ensure
        # cleanup
        Object.send(:remove_const, :ProductsHelper)
      end
    end
  end
end