rspec-rails 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. data/History.txt +57 -0
  2. data/Manifest.txt +158 -0
  3. data/README.txt +81 -0
  4. data/Rakefile +39 -0
  5. data/UPGRADE +7 -0
  6. data/generators/rspec/CHANGES +1 -0
  7. data/generators/rspec/rspec_generator.rb +40 -0
  8. data/generators/rspec/templates/all_stories.rb +4 -0
  9. data/generators/rspec/templates/previous_failures.txt +0 -0
  10. data/generators/rspec/templates/rcov.opts +2 -0
  11. data/generators/rspec/templates/rspec.rake +132 -0
  12. data/generators/rspec/templates/script/autospec +3 -0
  13. data/generators/rspec/templates/script/spec +4 -0
  14. data/generators/rspec/templates/script/spec_server +116 -0
  15. data/generators/rspec/templates/spec.opts +4 -0
  16. data/generators/rspec/templates/spec_helper.rb +47 -0
  17. data/generators/rspec/templates/stories_helper.rb +3 -0
  18. data/generators/rspec_controller/USAGE +33 -0
  19. data/generators/rspec_controller/rspec_controller_generator.rb +49 -0
  20. data/generators/rspec_controller/templates/controller_spec.rb +25 -0
  21. data/generators/rspec_controller/templates/helper_spec.rb +11 -0
  22. data/generators/rspec_controller/templates/view_spec.rb +12 -0
  23. data/generators/rspec_default_values.rb +19 -0
  24. data/generators/rspec_model/USAGE +18 -0
  25. data/generators/rspec_model/rspec_model_generator.rb +35 -0
  26. data/generators/rspec_model/templates/model_spec.rb +15 -0
  27. data/generators/rspec_scaffold/rspec_scaffold_generator.rb +154 -0
  28. data/generators/rspec_scaffold/templates/controller_spec.rb +173 -0
  29. data/generators/rspec_scaffold/templates/edit_erb_spec.rb +26 -0
  30. data/generators/rspec_scaffold/templates/helper_spec.rb +11 -0
  31. data/generators/rspec_scaffold/templates/index_erb_spec.rb +27 -0
  32. data/generators/rspec_scaffold/templates/new_erb_spec.rb +26 -0
  33. data/generators/rspec_scaffold/templates/routing_spec.rb +59 -0
  34. data/generators/rspec_scaffold/templates/show_erb_spec.rb +23 -0
  35. data/init.rb +9 -0
  36. data/lib/autotest/discover.rb +1 -0
  37. data/lib/autotest/rails_rspec.rb +76 -0
  38. data/lib/spec/rails.rb +15 -0
  39. data/lib/spec/rails/example.rb +47 -0
  40. data/lib/spec/rails/example/assigns_hash_proxy.rb +43 -0
  41. data/lib/spec/rails/example/controller_example_group.rb +256 -0
  42. data/lib/spec/rails/example/cookies_proxy.rb +25 -0
  43. data/lib/spec/rails/example/functional_example_group.rb +87 -0
  44. data/lib/spec/rails/example/helper_example_group.rb +166 -0
  45. data/lib/spec/rails/example/model_example_group.rb +14 -0
  46. data/lib/spec/rails/example/rails_example_group.rb +33 -0
  47. data/lib/spec/rails/example/render_observer.rb +93 -0
  48. data/lib/spec/rails/example/view_example_group.rb +183 -0
  49. data/lib/spec/rails/extensions.rb +12 -0
  50. data/lib/spec/rails/extensions/action_controller/base.rb +14 -0
  51. data/lib/spec/rails/extensions/action_controller/rescue.rb +21 -0
  52. data/lib/spec/rails/extensions/action_controller/test_response.rb +11 -0
  53. data/lib/spec/rails/extensions/action_view/base.rb +31 -0
  54. data/lib/spec/rails/extensions/active_record/base.rb +30 -0
  55. data/lib/spec/rails/extensions/object.rb +5 -0
  56. data/lib/spec/rails/extensions/spec/example/configuration.rb +71 -0
  57. data/lib/spec/rails/extensions/spec/matchers/have.rb +21 -0
  58. data/lib/spec/rails/interop/testcase.rb +14 -0
  59. data/lib/spec/rails/matchers.rb +31 -0
  60. data/lib/spec/rails/matchers/assert_select.rb +131 -0
  61. data/lib/spec/rails/matchers/change.rb +11 -0
  62. data/lib/spec/rails/matchers/have_text.rb +57 -0
  63. data/lib/spec/rails/matchers/include_text.rb +54 -0
  64. data/lib/spec/rails/matchers/redirect_to.rb +113 -0
  65. data/lib/spec/rails/matchers/render_template.rb +90 -0
  66. data/lib/spec/rails/mocks.rb +132 -0
  67. data/lib/spec/rails/story_adapter.rb +79 -0
  68. data/lib/spec/rails/version.rb +15 -0
  69. data/spec/rails/autotest/mappings_spec.rb +36 -0
  70. data/spec/rails/example/assigns_hash_proxy_spec.rb +65 -0
  71. data/spec/rails/example/configuration_spec.rb +83 -0
  72. data/spec/rails/example/controller_isolation_spec.rb +62 -0
  73. data/spec/rails/example/controller_spec_spec.rb +272 -0
  74. data/spec/rails/example/cookies_proxy_spec.rb +74 -0
  75. data/spec/rails/example/example_group_factory_spec.rb +112 -0
  76. data/spec/rails/example/helper_spec_spec.rb +161 -0
  77. data/spec/rails/example/model_spec_spec.rb +18 -0
  78. data/spec/rails/example/shared_behaviour_spec.rb +16 -0
  79. data/spec/rails/example/test_unit_assertion_accessibility_spec.rb +33 -0
  80. data/spec/rails/example/view_spec_spec.rb +280 -0
  81. data/spec/rails/extensions/action_controller_rescue_action_spec.rb +54 -0
  82. data/spec/rails/extensions/action_view_base_spec.rb +48 -0
  83. data/spec/rails/extensions/active_record_spec.rb +14 -0
  84. data/spec/rails/interop/testcase_spec.rb +66 -0
  85. data/spec/rails/matchers/assert_select_spec.rb +814 -0
  86. data/spec/rails/matchers/description_generation_spec.rb +37 -0
  87. data/spec/rails/matchers/errors_on_spec.rb +13 -0
  88. data/spec/rails/matchers/have_text_spec.rb +62 -0
  89. data/spec/rails/matchers/include_text_spec.rb +64 -0
  90. data/spec/rails/matchers/redirect_to_spec.rb +209 -0
  91. data/spec/rails/matchers/render_template_spec.rb +176 -0
  92. data/spec/rails/matchers/should_change_spec.rb +15 -0
  93. data/spec/rails/mocks/ar_classes.rb +10 -0
  94. data/spec/rails/mocks/mock_model_spec.rb +106 -0
  95. data/spec/rails/mocks/stub_model_spec.rb +80 -0
  96. data/spec/rails/sample_modified_fixture.rb +8 -0
  97. data/spec/rails/sample_spec.rb +8 -0
  98. data/spec/rails/spec_server_spec.rb +96 -0
  99. data/spec/rails/spec_spec.rb +11 -0
  100. data/spec/rails_suite.rb +7 -0
  101. data/spec/spec_helper.rb +57 -0
  102. data/spec_resources/controllers/action_view_base_spec_controller.rb +2 -0
  103. data/spec_resources/controllers/controller_spec_controller.rb +94 -0
  104. data/spec_resources/controllers/redirect_spec_controller.rb +59 -0
  105. data/spec_resources/controllers/render_spec_controller.rb +30 -0
  106. data/spec_resources/controllers/rjs_spec_controller.rb +58 -0
  107. data/spec_resources/helpers/explicit_helper.rb +38 -0
  108. data/spec_resources/helpers/more_explicit_helper.rb +5 -0
  109. data/spec_resources/helpers/plugin_application_helper.rb +6 -0
  110. data/spec_resources/helpers/view_spec_helper.rb +13 -0
  111. data/spec_resources/views/controller_spec/_partial.rhtml +0 -0
  112. data/spec_resources/views/controller_spec/action_setting_flash_after_session_reset.rhtml +1 -0
  113. data/spec_resources/views/controller_spec/action_setting_flash_before_session_reset.rhtml +1 -0
  114. data/spec_resources/views/controller_spec/action_setting_the_assigns_hash.rhtml +0 -0
  115. data/spec_resources/views/controller_spec/action_with_errors_in_template.rhtml +1 -0
  116. data/spec_resources/views/controller_spec/action_with_template.rhtml +1 -0
  117. data/spec_resources/views/layouts/application.rhtml +0 -0
  118. data/spec_resources/views/layouts/simple.rhtml +0 -0
  119. data/spec_resources/views/objects/_object.html.erb +1 -0
  120. data/spec_resources/views/render_spec/_a_partial.rhtml +0 -0
  121. data/spec_resources/views/render_spec/action_with_alternate_layout.rhtml +0 -0
  122. data/spec_resources/views/render_spec/some_action.js.rjs +1 -0
  123. data/spec_resources/views/render_spec/some_action.rhtml +0 -0
  124. data/spec_resources/views/render_spec/some_action.rjs +1 -0
  125. data/spec_resources/views/rjs_spec/_replacement_partial.rhtml +1 -0
  126. data/spec_resources/views/rjs_spec/hide_div.rjs +1 -0
  127. data/spec_resources/views/rjs_spec/hide_page_element.rjs +1 -0
  128. data/spec_resources/views/rjs_spec/insert_html.rjs +1 -0
  129. data/spec_resources/views/rjs_spec/replace.rjs +1 -0
  130. data/spec_resources/views/rjs_spec/replace_html.rjs +1 -0
  131. data/spec_resources/views/rjs_spec/replace_html_with_partial.rjs +1 -0
  132. data/spec_resources/views/rjs_spec/visual_effect.rjs +1 -0
  133. data/spec_resources/views/rjs_spec/visual_toggle_effect.rjs +1 -0
  134. data/spec_resources/views/tag_spec/no_tags.rhtml +1 -0
  135. data/spec_resources/views/tag_spec/single_div_with_no_attributes.rhtml +1 -0
  136. data/spec_resources/views/tag_spec/single_div_with_one_attribute.rhtml +1 -0
  137. data/spec_resources/views/view_spec/_partial.rhtml +2 -0
  138. data/spec_resources/views/view_spec/_partial_used_twice.rhtml +0 -0
  139. data/spec_resources/views/view_spec/_partial_with_local_variable.rhtml +1 -0
  140. data/spec_resources/views/view_spec/_partial_with_sub_partial.rhtml +1 -0
  141. data/spec_resources/views/view_spec/_spacer.rhtml +1 -0
  142. data/spec_resources/views/view_spec/accessor.rhtml +5 -0
  143. data/spec_resources/views/view_spec/block_helper.rhtml +3 -0
  144. data/spec_resources/views/view_spec/entry_form.rhtml +2 -0
  145. data/spec_resources/views/view_spec/explicit_helper.rhtml +2 -0
  146. data/spec_resources/views/view_spec/foo/show.rhtml +1 -0
  147. data/spec_resources/views/view_spec/implicit_helper.rhtml +2 -0
  148. data/spec_resources/views/view_spec/multiple_helpers.rhtml +3 -0
  149. data/spec_resources/views/view_spec/should_not_receive.rhtml +3 -0
  150. data/spec_resources/views/view_spec/template_with_partial.rhtml +5 -0
  151. data/spec_resources/views/view_spec/template_with_partial_using_collection.rhtml +3 -0
  152. data/spec_resources/views/view_spec/template_with_partial_with_array.rhtml +1 -0
  153. data/stories/all.rb +10 -0
  154. data/stories/configuration/stories.rb +5 -0
  155. data/stories/helper.rb +6 -0
  156. data/stories/steps/people.rb +8 -0
  157. data/stories/transactions_should_rollback +15 -0
  158. data/stories/transactions_should_rollback.rb +25 -0
  159. metadata +234 -0
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['RSPEC'] = 'true'
3
+ system 'autotest'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../vendor/plugins/rspec/lib"))
3
+ require 'spec'
4
+ exit ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(ARGV, STDERR, STDOUT))
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
3
+ require 'rubygems'
4
+ require 'drb/drb'
5
+ require 'rbconfig'
6
+ require 'spec'
7
+ require 'optparse'
8
+
9
+ # This is based on Florian Weber's TDDMate
10
+ module Spec
11
+ module Runner
12
+ class RailsSpecServer
13
+ def run(argv, stderr, stdout)
14
+ $stdout = stdout
15
+ $stderr = stderr
16
+
17
+ base = ActiveRecord::Base
18
+ def base.clear_reloadable_connections!
19
+ active_connections.each do |name, conn|
20
+ if conn.requires_reloading?
21
+ conn.disconnect!
22
+ active_connections.delete(name)
23
+ end
24
+ end
25
+ end
26
+
27
+ if ActionController.const_defined?(:Dispatcher)
28
+ dispatcher = ::ActionController::Dispatcher.new($stdout)
29
+ dispatcher.cleanup_application
30
+ elsif ::Dispatcher.respond_to?(:reset_application!)
31
+ ::Dispatcher.reset_application!
32
+ else
33
+ raise "Application reloading failed"
34
+ end
35
+ if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
36
+ Fixtures.reset_cache
37
+ end
38
+
39
+ ::Dependencies.mechanism = :load
40
+ require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
41
+ load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
42
+
43
+ if in_memory_database?
44
+ load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
45
+ ActiveRecord::Migrator.up('db/migrate') # use migrations
46
+ end
47
+
48
+ ::Spec::Runner::CommandLine.run(
49
+ ::Spec::Runner::OptionParser.parse(
50
+ argv,
51
+ $stderr,
52
+ $stdout
53
+ )
54
+ )
55
+ end
56
+
57
+ def in_memory_database?
58
+ ENV["RAILS_ENV"] == "test" and
59
+ ::ActiveRecord::Base.connection.class.to_s == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" and
60
+ ::Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
61
+ end
62
+ end
63
+ end
64
+ end
65
+ puts "Loading Rails environment"
66
+
67
+ ENV["RAILS_ENV"] = "test"
68
+ require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
69
+ require 'dispatcher'
70
+
71
+ def restart_test_server
72
+ puts "restarting"
73
+ config = ::Config::CONFIG
74
+ ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
75
+ command_line = [ruby, $0, ARGV].flatten.join(' ')
76
+ exec(command_line)
77
+ end
78
+
79
+ def daemonize(pid_file = nil)
80
+ return yield if $DEBUG
81
+ pid = Process.fork{
82
+ Process.setsid
83
+ Dir.chdir(RAILS_ROOT)
84
+ trap("SIGINT"){ exit! 0 }
85
+ trap("SIGTERM"){ exit! 0 }
86
+ trap("SIGHUP"){ restart_test_server }
87
+ File.open("/dev/null"){|f|
88
+ STDERR.reopen f
89
+ STDIN.reopen f
90
+ STDOUT.reopen f
91
+ }
92
+ yield
93
+ }
94
+ puts "spec_server launched. (PID: %d)" % pid
95
+ File.open(pid_file,"w"){|f| f.puts pid } if pid_file
96
+ exit! 0
97
+ end
98
+
99
+ options = Hash.new
100
+ opts = OptionParser.new
101
+ opts.on("-d", "--daemon"){|v| options[:daemon] = true }
102
+ opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
103
+ opts.parse!(ARGV)
104
+
105
+ puts "Ready"
106
+ exec_server = lambda {
107
+ trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
108
+ DRb.start_service("druby://127.0.0.1:8989", Spec::Runner::RailsSpecServer.new)
109
+ DRb.thread.join
110
+ }
111
+
112
+ if options[:daemon]
113
+ daemonize(options[:pid], &exec_server)
114
+ else
115
+ exec_server.call
116
+ end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,47 @@
1
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
+ # from the project root directory.
3
+ ENV["RAILS_ENV"] = "test"
4
+ require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
5
+ require 'spec'
6
+ require 'spec/rails'
7
+
8
+ Spec::Runner.configure do |config|
9
+ # If you're not using ActiveRecord you should remove these
10
+ # lines, delete config/database.yml and disable :active_record
11
+ # in your config/boot.rb
12
+ config.use_transactional_fixtures = true
13
+ config.use_instantiated_fixtures = false
14
+ config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
15
+
16
+ # == Fixtures
17
+ #
18
+ # You can declare fixtures for each example_group like this:
19
+ # describe "...." do
20
+ # fixtures :table_a, :table_b
21
+ #
22
+ # Alternatively, if you prefer to declare them only once, you can
23
+ # do so right here. Just uncomment the next line and replace the fixture
24
+ # names with your fixtures.
25
+ #
26
+ # config.global_fixtures = :table_a, :table_b
27
+ #
28
+ # If you declare global fixtures, be aware that they will be declared
29
+ # for all of your examples, even those that don't use them.
30
+ #
31
+ # You can also declare which fixtures to use (for example fixtures for test/fixtures):
32
+ #
33
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
34
+ #
35
+ # == Mock Framework
36
+ #
37
+ # RSpec uses it's own mocking framework by default. If you prefer to
38
+ # use mocha, flexmock or RR, uncomment the appropriate line:
39
+ #
40
+ # config.mock_with :mocha
41
+ # config.mock_with :flexmock
42
+ # config.mock_with :rr
43
+ #
44
+ # == Notes
45
+ #
46
+ # For more information take a look at Spec::Example::Configuration and Spec::Runner
47
+ end
@@ -0,0 +1,3 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
3
+ require 'spec/rails/story_adapter'
@@ -0,0 +1,33 @@
1
+ Description:
2
+ The rspec_controller generator creates stub specs and files for a new
3
+ controller and its views.
4
+
5
+ The generator takes a controller name and a list of views as arguments.
6
+ The controller name may be given in CamelCase or under_score and should
7
+ not be suffixed with 'Controller'. To create a controller within a
8
+ module, specify the controller name as 'module/controller'.
9
+
10
+ The generator creates stubs for a controller (and spec), a view (and spec)
11
+ for each view in the argument list, plus a helper.
12
+
13
+ Example:
14
+ ./script/generate rspec_controller dog bark fetch
15
+ ...
16
+ create spec/controllers/dog_controller_spec.rb
17
+ create app/controllers/dog_controller.rb
18
+ create app/helpers/dog_helper.rb
19
+ create spec/views/dog/bark_view_spec.rb
20
+ create app/views/dog/bark.rhtml
21
+ create spec/views/dog/fetch_view_spec.rb
22
+ create app/views/dog/fetch.rhtml
23
+
24
+ Modules Example:
25
+ ./script/generate rspec_controller 'pets/dog' bark fetch
26
+ ...
27
+ create spec/controllers/pets/dog_controller_spec.rb
28
+ create app/controllers/pets/dog_controller.rb
29
+ create app/helpers/pets/dog_helper.rb
30
+ create spec/views/pets/dog/bark_view_spec.rb
31
+ create app/views/pets/dog/bark.rhtml
32
+ create spec/views/pets/dog/fetch_view_spec.rb
33
+ create app/views/pets/dog/fetch.rhtml
@@ -0,0 +1,49 @@
1
+ require 'rails_generator/generators/components/controller/controller_generator'
2
+
3
+ class RspecControllerGenerator < ControllerGenerator
4
+
5
+ def manifest
6
+ record do |m|
7
+ # Check for class naming collisions.
8
+ m.class_collisions class_path, "#{class_name}Controller", "#{class_name}Helper"
9
+
10
+ # Controller, helper, views, and spec directories.
11
+ m.directory File.join('app/controllers', class_path)
12
+ m.directory File.join('app/helpers', class_path)
13
+ m.directory File.join('app/views', class_path, file_name)
14
+ m.directory File.join('spec/controllers', class_path)
15
+ m.directory File.join('spec/helpers', class_path)
16
+ m.directory File.join('spec/views', class_path, file_name)
17
+
18
+ if Rails::VERSION::STRING < "2.0.0"
19
+ @default_file_extension = "rhtml"
20
+ else
21
+ @default_file_extension = "html.erb"
22
+ end
23
+
24
+ # Controller spec, class, and helper.
25
+ m.template 'controller_spec.rb',
26
+ File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")
27
+
28
+ m.template 'helper_spec.rb',
29
+ File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
30
+
31
+ m.template 'controller:controller.rb',
32
+ File.join('app/controllers', class_path, "#{file_name}_controller.rb")
33
+
34
+ m.template 'controller:helper.rb',
35
+ File.join('app/helpers', class_path, "#{file_name}_helper.rb")
36
+
37
+ # Spec and view template for each action.
38
+ actions.each do |action|
39
+ m.template 'view_spec.rb',
40
+ File.join('spec/views', class_path, file_name, "#{action}.#{@default_file_extension}_spec.rb"),
41
+ :assigns => { :action => action, :model => file_name }
42
+ path = File.join('app/views', class_path, file_name, "#{action}.#{@default_file_extension}")
43
+ m.template "controller:view.#{@default_file_extension}",
44
+ path,
45
+ :assigns => { :action => action, :path => path }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2
+
3
+ describe <%= class_name %>Controller do
4
+
5
+ <% if actions.empty? -%>
6
+ #Delete this example and add some real ones
7
+ <% else -%>
8
+ #Delete these examples and add some real ones
9
+ <% end -%>
10
+ it "should use <%= class_name %>Controller" do
11
+ controller.should be_an_instance_of(<%= class_name %>Controller)
12
+ end
13
+
14
+ <% unless actions.empty? -%>
15
+ <% for action in actions -%>
16
+
17
+ describe "GET '<%= action %>'" do
18
+ it "should be successful" do
19
+ get '<%= action %>'
20
+ response.should be_success
21
+ end
22
+ end
23
+ <% end -%>
24
+ <% end -%>
25
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2
+
3
+ describe <%= class_name %>Helper do
4
+
5
+ #Delete this example and add some real ones or delete this file
6
+ it "should be included in the object returned by #helper" do
7
+ included_modules = (class << helper; self; end).send :included_modules
8
+ included_modules.should include(<%= class_name %>Helper)
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../../spec_helper')
2
+
3
+ describe "/<%= class_name.underscore %>/<%= action %>" do
4
+ before(:each) do
5
+ render '<%= class_name.underscore %>/<%= action %>'
6
+ end
7
+
8
+ #Delete this example and add some real ones or delete this file
9
+ it "should tell you where to find the file" do
10
+ response.should have_tag('p', %r[Find me in app/views/<%= class_name.underscore %>/<%= action %>])
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Rails
2
+ module Generator
3
+ class GeneratedAttribute
4
+ def default_value
5
+ @default_value ||= case type
6
+ when :int, :integer then "\"1\""
7
+ when :float then "\"1.5\""
8
+ when :decimal then "\"9.99\""
9
+ when :datetime, :timestamp, :time then "Time.now"
10
+ when :date then "Date.today"
11
+ when :string, :text then "\"value for #{@name}\""
12
+ when :boolean then "false"
13
+ else
14
+ ""
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ Description:
2
+ The rspec_model generator creates stubs for a new model.
3
+
4
+ The generator takes a model name as its argument. The model name may be
5
+ given in CamelCase or under_score and should not be suffixed with 'Model'.
6
+
7
+ The generator creates a model class in app/models, an RSpec spec in
8
+ spec/models, database fixtures in spec/fixtures/plural_name.yml, and a migration
9
+ in db/migrate.
10
+
11
+ Example:
12
+ ./script/generate rspec_model Account
13
+
14
+ This will create an Account model:
15
+ Model: app/models/account.rb
16
+ Spec: spec/models/account_spec.rb
17
+ Fixtures: spec/fixtures/accounts.yml
18
+ Migration: db/migrate/XXX_add_accounts.rb
@@ -0,0 +1,35 @@
1
+ require 'rails_generator/generators/components/model/model_generator'
2
+ require File.dirname(__FILE__) + '/../rspec_default_values'
3
+
4
+ class RspecModelGenerator < ModelGenerator
5
+
6
+ def manifest
7
+
8
+ record do |m|
9
+ # Check for class naming collisions.
10
+ m.class_collisions class_path, class_name
11
+
12
+ # Model, spec, and fixture directories.
13
+ m.directory File.join('app/models', class_path)
14
+ m.directory File.join('spec/models', class_path)
15
+ unless options[:skip_fixture]
16
+ m.directory File.join('spec/fixtures', class_path)
17
+ end
18
+
19
+ # Model class, spec and fixtures.
20
+ m.template 'model:model.rb', File.join('app/models', class_path, "#{file_name}.rb")
21
+ m.template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
22
+ unless options[:skip_fixture]
23
+ m.template 'model:fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
24
+ end
25
+
26
+ unless options[:skip_migration]
27
+ m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
28
+ :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
29
+ }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2
+
3
+ describe <%= class_name %> do
4
+ before(:each) do
5
+ @valid_attributes = {
6
+ <% attributes.each_with_index do |attribute, attribute_index| -%>
7
+ :<%= attribute.name %> => <%= attribute.default_value %><%= attribute_index == attributes.length - 1 ? '' : ','%>
8
+ <% end -%>
9
+ }
10
+ end
11
+
12
+ it "should create a new instance given valid attributes" do
13
+ <%= class_name %>.create!(@valid_attributes)
14
+ end
15
+ end
@@ -0,0 +1,154 @@
1
+ require File.dirname(__FILE__) + '/../rspec_default_values'
2
+
3
+ class RspecScaffoldGenerator < Rails::Generator::NamedBase
4
+ default_options :skip_migration => false
5
+
6
+ attr_reader :controller_name,
7
+ :controller_class_path,
8
+ :controller_file_path,
9
+ :controller_class_nesting,
10
+ :controller_class_nesting_depth,
11
+ :controller_class_name,
12
+ :controller_singular_name,
13
+ :controller_plural_name,
14
+ :resource_edit_path,
15
+ :default_file_extension
16
+ alias_method :controller_file_name, :controller_singular_name
17
+ alias_method :controller_table_name, :controller_plural_name
18
+
19
+ def initialize(runtime_args, runtime_options = {})
20
+ super
21
+
22
+ @controller_name = @name.pluralize
23
+
24
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
25
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
26
+
27
+ if @controller_class_nesting.empty?
28
+ @controller_class_name = @controller_class_name_without_nesting
29
+ else
30
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
31
+ end
32
+
33
+ if Rails::VERSION::STRING < "2.0.0"
34
+ @resource_generator = "scaffold_resource"
35
+ @default_file_extension = "rhtml"
36
+ else
37
+ @resource_generator = "scaffold"
38
+ @default_file_extension = "html.erb"
39
+ end
40
+
41
+ if ActionController::Base.respond_to?(:resource_action_separator)
42
+ @resource_edit_path = "/edit"
43
+ else
44
+ @resource_edit_path = ";edit"
45
+ end
46
+ end
47
+
48
+ def manifest
49
+ record do |m|
50
+
51
+ # Check for class naming collisions.
52
+ m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
53
+ m.class_collisions(class_path, "#{class_name}")
54
+
55
+ # Controller, helper, views, and spec directories.
56
+ m.directory(File.join('app/models', class_path))
57
+ m.directory(File.join('app/controllers', controller_class_path))
58
+ m.directory(File.join('app/helpers', controller_class_path))
59
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
60
+ m.directory(File.join('spec/controllers', controller_class_path))
61
+ m.directory(File.join('spec/models', class_path))
62
+ m.directory(File.join('spec/helpers', class_path))
63
+ m.directory File.join('spec/fixtures', class_path)
64
+ m.directory File.join('spec/views', controller_class_path, controller_file_name)
65
+
66
+ # Controller spec, class, and helper.
67
+ m.template 'rspec_scaffold:routing_spec.rb',
68
+ File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb")
69
+
70
+ m.template 'rspec_scaffold:controller_spec.rb',
71
+ File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
72
+
73
+ m.template "#{@resource_generator}:controller.rb",
74
+ File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
75
+
76
+ m.template 'rspec_scaffold:helper_spec.rb',
77
+ File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
78
+
79
+ m.template "#{@resource_generator}:helper.rb",
80
+ File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")
81
+
82
+ for action in scaffold_views
83
+ m.template(
84
+ "#{@resource_generator}:view_#{action}.#{@default_file_extension}",
85
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}")
86
+ )
87
+ end
88
+
89
+ # Model class, unit test, and fixtures.
90
+ m.template 'model:model.rb', File.join('app/models', class_path, "#{file_name}.rb")
91
+ m.template 'model:fixtures.yml', File.join('spec/fixtures', class_path, "#{table_name}.yml")
92
+ m.template 'rspec_model:model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
93
+
94
+ # View specs
95
+ m.template "rspec_scaffold:edit_erb_spec.rb",
96
+ File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb")
97
+ m.template "rspec_scaffold:index_erb_spec.rb",
98
+ File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb")
99
+ m.template "rspec_scaffold:new_erb_spec.rb",
100
+ File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb")
101
+ m.template "rspec_scaffold:show_erb_spec.rb",
102
+ File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb")
103
+
104
+ unless options[:skip_migration]
105
+ m.migration_template(
106
+ 'model:migration.rb', 'db/migrate',
107
+ :assigns => {
108
+ :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
109
+ :attributes => attributes
110
+ },
111
+ :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
112
+ )
113
+ end
114
+
115
+ m.route_resources controller_file_name
116
+
117
+ end
118
+ end
119
+
120
+ protected
121
+ # Override with your own usage banner.
122
+ def banner
123
+ "Usage: #{$0} rspec_scaffold ModelName [field:type field:type]"
124
+ end
125
+
126
+ def add_options!(opt)
127
+ opt.separator ''
128
+ opt.separator 'Options:'
129
+ opt.on("--skip-migration",
130
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
131
+ end
132
+
133
+ def scaffold_views
134
+ %w[ index show new edit ]
135
+ end
136
+
137
+ def model_name
138
+ class_name.demodulize
139
+ end
140
+ end
141
+
142
+ module Rails
143
+ module Generator
144
+ class GeneratedAttribute
145
+ def input_type
146
+ @input_type ||= case type
147
+ when :text then "textarea"
148
+ else
149
+ "input"
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end