richzhou-rspec-rails 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +7 -0
- data/.gitignore +8 -0
- data/Contribute.rdoc +4 -0
- data/Gemfile +4 -0
- data/History.rdoc +321 -0
- data/License.txt +33 -0
- data/Manifest.txt +165 -0
- data/README.rdoc +48 -0
- data/Rakefile +12 -0
- data/TODO.txt +17 -0
- data/Upgrade.rdoc +148 -0
- data/generators/integration_spec/integration_spec_generator.rb +10 -0
- data/generators/integration_spec/templates/integration_spec.rb +4 -0
- data/generators/rspec/CHANGES +1 -0
- data/generators/rspec/rspec_generator.rb +72 -0
- data/generators/rspec/templates/previous_failures.txt +0 -0
- data/generators/rspec/templates/rcov.opts +2 -0
- data/generators/rspec/templates/rspec.rake +146 -0
- data/generators/rspec/templates/script/autospec +6 -0
- data/generators/rspec/templates/script/spec +10 -0
- data/generators/rspec/templates/spec.opts +4 -0
- data/generators/rspec/templates/spec_helper.rb +54 -0
- data/generators/rspec_controller/USAGE +33 -0
- data/generators/rspec_controller/rspec_controller_generator.rb +47 -0
- data/generators/rspec_controller/templates/controller_spec.rb +25 -0
- data/generators/rspec_controller/templates/helper_spec.rb +11 -0
- data/generators/rspec_controller/templates/view_spec.rb +12 -0
- data/generators/rspec_default_values.rb +28 -0
- data/generators/rspec_model/USAGE +18 -0
- data/generators/rspec_model/rspec_model_generator.rb +35 -0
- data/generators/rspec_model/templates/model_spec.rb +13 -0
- data/generators/rspec_scaffold/rspec_scaffold_generator.rb +154 -0
- data/generators/rspec_scaffold/templates/controller_spec.rb +131 -0
- data/generators/rspec_scaffold/templates/edit_erb_spec.rb +25 -0
- data/generators/rspec_scaffold/templates/helper_spec.rb +11 -0
- data/generators/rspec_scaffold/templates/index_erb_spec.rb +27 -0
- data/generators/rspec_scaffold/templates/new_erb_spec.rb +25 -0
- data/generators/rspec_scaffold/templates/routing_spec.rb +33 -0
- data/generators/rspec_scaffold/templates/show_erb_spec.rb +22 -0
- data/init.rb +9 -0
- data/lib/autotest/discover.rb +5 -0
- data/lib/autotest/rails_rspec.rb +76 -0
- data/lib/spec/rails.rb +26 -0
- data/lib/spec/rails/example.rb +48 -0
- data/lib/spec/rails/example/assigns_hash_proxy.rb +39 -0
- data/lib/spec/rails/example/controller_example_group.rb +285 -0
- data/lib/spec/rails/example/cookies_proxy.rb +29 -0
- data/lib/spec/rails/example/functional_example_group.rb +106 -0
- data/lib/spec/rails/example/helper_example_group.rb +153 -0
- data/lib/spec/rails/example/integration_example_group.rb +16 -0
- data/lib/spec/rails/example/model_example_group.rb +15 -0
- data/lib/spec/rails/example/render_observer.rb +82 -0
- data/lib/spec/rails/example/routing_example_group.rb +16 -0
- data/lib/spec/rails/example/routing_helpers.rb +66 -0
- data/lib/spec/rails/example/view_example_group.rb +203 -0
- data/lib/spec/rails/extensions.rb +11 -0
- data/lib/spec/rails/extensions/action_controller/rescue.rb +42 -0
- data/lib/spec/rails/extensions/action_controller/test_case.rb +16 -0
- data/lib/spec/rails/extensions/action_controller/test_response.rb +21 -0
- data/lib/spec/rails/extensions/action_view/base.rb +35 -0
- data/lib/spec/rails/extensions/active_record/base.rb +45 -0
- data/lib/spec/rails/extensions/active_support/test_case.rb +7 -0
- data/lib/spec/rails/extensions/spec/matchers/have.rb +23 -0
- data/lib/spec/rails/extensions/spec/runner/configuration.rb +45 -0
- data/lib/spec/rails/interop/testcase.rb +14 -0
- data/lib/spec/rails/matchers.rb +32 -0
- data/lib/spec/rails/matchers/ar_be_valid.rb +27 -0
- data/lib/spec/rails/matchers/assert_select.rb +180 -0
- data/lib/spec/rails/matchers/change.rb +13 -0
- data/lib/spec/rails/matchers/have_text.rb +57 -0
- data/lib/spec/rails/matchers/include_text.rb +54 -0
- data/lib/spec/rails/matchers/redirect_to.rb +126 -0
- data/lib/spec/rails/matchers/render_template.rb +129 -0
- data/lib/spec/rails/matchers/route_to.rb +149 -0
- data/lib/spec/rails/mocks.rb +140 -0
- data/lib/spec/rails/version.rb +16 -0
- data/rspec-rails.gemspec +25 -0
- data/spec/autotest/mappings_spec.rb +86 -0
- data/spec/rails_suite.rb +7 -0
- data/spec/resources/controllers/action_view_base_spec_controller.rb +2 -0
- data/spec/resources/controllers/application.rb +9 -0
- data/spec/resources/controllers/controller_spec_controller.rb +127 -0
- data/spec/resources/controllers/example.txt +1 -0
- data/spec/resources/controllers/redirect_spec_controller.rb +70 -0
- data/spec/resources/controllers/render_spec_controller.rb +34 -0
- data/spec/resources/controllers/rjs_spec_controller.rb +58 -0
- data/spec/resources/helpers/addition_helper.rb +5 -0
- data/spec/resources/helpers/explicit_helper.rb +46 -0
- data/spec/resources/helpers/more_explicit_helper.rb +5 -0
- data/spec/resources/helpers/plugin_application_helper.rb +6 -0
- data/spec/resources/helpers/view_spec_helper.rb +13 -0
- data/spec/resources/models/animal.rb +4 -0
- data/spec/resources/models/person.rb +18 -0
- data/spec/resources/models/thing.rb +3 -0
- data/spec/resources/views/controller_spec/_partial.html.erb +0 -0
- data/spec/resources/views/controller_spec/action_setting_flash_after_session_reset.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_setting_flash_before_session_reset.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_setting_the_assigns_hash.html.erb +0 -0
- data/spec/resources/views/controller_spec/action_with_errors_in_template.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_with_template.html.erb +1 -0
- data/spec/resources/views/controller_spec/non_existent_action_with_existent_template.html.erb +1 -0
- data/spec/resources/views/layouts/application.html.erb +0 -0
- data/spec/resources/views/layouts/simple.html.erb +0 -0
- data/spec/resources/views/objects/_object.html.erb +1 -0
- data/spec/resources/views/render_spec/_a_partial.html.erb +0 -0
- data/spec/resources/views/render_spec/action_with_alternate_layout.html.erb +0 -0
- data/spec/resources/views/render_spec/some_action.html.erb +0 -0
- data/spec/resources/views/render_spec/some_action.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/_replacement_partial.html.erb +1 -0
- data/spec/resources/views/rjs_spec/hide_div.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/hide_page_element.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/insert_html.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace_html.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace_html_with_partial.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/visual_effect.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/visual_toggle_effect.js.rjs +1 -0
- data/spec/resources/views/tag_spec/no_tags.html.erb +1 -0
- data/spec/resources/views/tag_spec/single_div_with_no_attributes.html.erb +1 -0
- data/spec/resources/views/tag_spec/single_div_with_one_attribute.html.erb +1 -0
- data/spec/resources/views/view_spec/_partial.html.erb +2 -0
- data/spec/resources/views/view_spec/_partial_used_twice.html.erb +0 -0
- data/spec/resources/views/view_spec/_partial_with_local_variable.html.erb +1 -0
- data/spec/resources/views/view_spec/_partial_with_sub_partial.html.erb +1 -0
- data/spec/resources/views/view_spec/_spacer.html.erb +1 -0
- data/spec/resources/views/view_spec/accessor.html.erb +6 -0
- data/spec/resources/views/view_spec/block_helper.html.erb +3 -0
- data/spec/resources/views/view_spec/entry_form.html.erb +2 -0
- data/spec/resources/views/view_spec/explicit_helper.html.erb +2 -0
- data/spec/resources/views/view_spec/foo/show.html.erb +1 -0
- data/spec/resources/views/view_spec/implicit_helper.html.erb +2 -0
- data/spec/resources/views/view_spec/multiple_helpers.html.erb +3 -0
- data/spec/resources/views/view_spec/path_params.html.erb +1 -0
- data/spec/resources/views/view_spec/should_not_receive.html.erb +3 -0
- data/spec/resources/views/view_spec/template_with_partial.html.erb +5 -0
- data/spec/resources/views/view_spec/template_with_partial_using_collection.html.erb +3 -0
- data/spec/resources/views/view_spec/template_with_partial_with_array.html.erb +1 -0
- data/spec/resources/views/view_spec/view_helpers.html.erb +1 -0
- data/spec/spec/rails/example/assigns_hash_proxy_spec.rb +109 -0
- data/spec/spec/rails/example/configuration_spec.rb +67 -0
- data/spec/spec/rails/example/controller_example_group_spec.rb +307 -0
- data/spec/spec/rails/example/controller_isolation_spec.rb +75 -0
- data/spec/spec/rails/example/cookies_proxy_spec.rb +87 -0
- data/spec/spec/rails/example/error_handling_spec.rb +90 -0
- data/spec/spec/rails/example/example_group_factory_spec.rb +112 -0
- data/spec/spec/rails/example/helper_example_group_spec.rb +247 -0
- data/spec/spec/rails/example/model_example_group_spec.rb +32 -0
- data/spec/spec/rails/example/routing_example_group_spec.rb +9 -0
- data/spec/spec/rails/example/shared_routing_example_group_examples.rb +241 -0
- data/spec/spec/rails/example/test_unit_assertion_accessibility_spec.rb +33 -0
- data/spec/spec/rails/example/view_example_group_spec.rb +346 -0
- data/spec/spec/rails/extensions/action_view_base_spec.rb +79 -0
- data/spec/spec/rails/extensions/active_record_spec.rb +14 -0
- data/spec/spec/rails/interop/testcase_spec.rb +70 -0
- data/spec/spec/rails/matchers/ar_be_valid_spec.rb +19 -0
- data/spec/spec/rails/matchers/assert_select_spec.rb +835 -0
- data/spec/spec/rails/matchers/errors_on_spec.rb +37 -0
- data/spec/spec/rails/matchers/have_text_spec.rb +69 -0
- data/spec/spec/rails/matchers/include_text_spec.rb +62 -0
- data/spec/spec/rails/matchers/redirect_to_spec.rb +253 -0
- data/spec/spec/rails/matchers/render_template_spec.rb +208 -0
- data/spec/spec/rails/matchers/should_change_spec.rb +15 -0
- data/spec/spec/rails/mocks/ar_classes.rb +10 -0
- data/spec/spec/rails/mocks/mock_model_spec.rb +112 -0
- data/spec/spec/rails/mocks/stub_model_spec.rb +80 -0
- data/spec/spec/rails/sample_modified_fixture.rb +8 -0
- data/spec/spec/rails/sample_spec.rb +8 -0
- data/spec/spec/rails/spec_spec.rb +11 -0
- data/spec/spec_helper.rb +78 -0
- metadata +363 -0
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
|
3
|
+
ENV['RSPEC'] = 'true' # allows autotest to discover rspec
|
4
|
+
ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux
|
5
|
+
system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) ||
|
6
|
+
$stderr.puts("Unable to find autotest. Please install ZenTest or fix your PATH")
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)}
|
3
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
4
|
+
else
|
5
|
+
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
|
6
|
+
ENV["RAILS_ENV"] ||= 'test'
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT)
|
8
|
+
end
|
9
|
+
require 'spec/autorun'
|
10
|
+
exit ::Spec::Runner::CommandLine.run
|
@@ -0,0 +1,54 @@
|
|
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.join(File.dirname(__FILE__),'..','config','environment'))
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'spec/rails'
|
7
|
+
|
8
|
+
# Uncomment the next line to use webrat's matchers
|
9
|
+
#require 'webrat/integrations/rspec-rails'
|
10
|
+
|
11
|
+
# Requires supporting files with custom matchers and macros, etc,
|
12
|
+
# in ./support/ and its subdirectories.
|
13
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
|
14
|
+
|
15
|
+
Spec::Runner.configure do |config|
|
16
|
+
# If you're not using ActiveRecord you should remove these
|
17
|
+
# lines, delete config/database.yml and disable :active_record
|
18
|
+
# in your config/boot.rb
|
19
|
+
config.use_transactional_fixtures = true
|
20
|
+
config.use_instantiated_fixtures = false
|
21
|
+
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
|
22
|
+
|
23
|
+
# == Fixtures
|
24
|
+
#
|
25
|
+
# You can declare fixtures for each example_group like this:
|
26
|
+
# describe "...." do
|
27
|
+
# fixtures :table_a, :table_b
|
28
|
+
#
|
29
|
+
# Alternatively, if you prefer to declare them only once, you can
|
30
|
+
# do so right here. Just uncomment the next line and replace the fixture
|
31
|
+
# names with your fixtures.
|
32
|
+
#
|
33
|
+
# config.global_fixtures = :table_a, :table_b
|
34
|
+
#
|
35
|
+
# If you declare global fixtures, be aware that they will be declared
|
36
|
+
# for all of your examples, even those that don't use them.
|
37
|
+
#
|
38
|
+
# You can also declare which fixtures to use (for example fixtures for test/fixtures):
|
39
|
+
#
|
40
|
+
# config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
|
41
|
+
#
|
42
|
+
# == Mock Framework
|
43
|
+
#
|
44
|
+
# RSpec uses its own mocking framework by default. If you prefer to
|
45
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
46
|
+
#
|
47
|
+
# config.mock_with :mocha
|
48
|
+
# config.mock_with :flexmock
|
49
|
+
# config.mock_with :rr
|
50
|
+
#
|
51
|
+
# == Notes
|
52
|
+
#
|
53
|
+
# For more information take a look at Spec::Runner::Configuration and Spec::Runner
|
54
|
+
end
|
@@ -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,47 @@
|
|
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
|
+
@default_file_extension = "html.erb"
|
19
|
+
|
20
|
+
# Controller spec, class, and helper.
|
21
|
+
m.template 'controller_spec.rb',
|
22
|
+
File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")
|
23
|
+
|
24
|
+
m.template 'helper_spec.rb',
|
25
|
+
File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
|
26
|
+
|
27
|
+
m.template 'controller:controller.rb',
|
28
|
+
File.join('app/controllers', class_path, "#{file_name}_controller.rb")
|
29
|
+
|
30
|
+
m.template 'controller:helper.rb',
|
31
|
+
File.join('app/helpers', class_path, "#{file_name}_helper.rb")
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
# Spec and view template for each action.
|
36
|
+
actions.each do |action|
|
37
|
+
m.template 'view_spec.rb',
|
38
|
+
File.join('spec/views', class_path, file_name, "#{action}.#{@default_file_extension}_spec.rb"),
|
39
|
+
:assigns => { :action => action, :model => file_name }
|
40
|
+
path = File.join('app/views', class_path, file_name, "#{action}.#{@default_file_extension}")
|
41
|
+
m.template "controller:view.#{@default_file_extension}",
|
42
|
+
path,
|
43
|
+
:assigns => { :action => action, :path => path }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require '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 '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 '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,28 @@
|
|
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
|
+
when :belongs_to, :references then "1"
|
14
|
+
else
|
15
|
+
""
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def name_or_reference
|
20
|
+
if ::Rails::VERSION::STRING >= '2.2'
|
21
|
+
reference? ? :"#{name}_id" : name
|
22
|
+
else
|
23
|
+
name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
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,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe <%= class_name %> do
|
4
|
+
before(:each) do
|
5
|
+
@valid_attributes = {
|
6
|
+
<%= attributes.map{|a| ":#{a.name_or_reference} => #{a.default_value}" }.join(",\n ") %>
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should create a new instance given valid attributes" do
|
11
|
+
<%= class_name %>.create!(@valid_attributes)
|
12
|
+
end
|
13
|
+
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
|
+
@default_file_extension = "html.erb"
|
34
|
+
end
|
35
|
+
|
36
|
+
def manifest
|
37
|
+
record do |m|
|
38
|
+
|
39
|
+
# Check for class naming collisions.
|
40
|
+
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
|
41
|
+
m.class_collisions(class_path, "#{class_name}")
|
42
|
+
|
43
|
+
# Controller, helper, views, and spec directories.
|
44
|
+
m.directory(File.join('app/models', class_path))
|
45
|
+
m.directory(File.join('app/controllers', controller_class_path))
|
46
|
+
m.directory(File.join('app/helpers', controller_class_path))
|
47
|
+
m.directory(File.join('app/views', controller_class_path, controller_file_name))
|
48
|
+
m.directory(File.join('app/views/layouts', controller_class_path))
|
49
|
+
m.directory(File.join('public/stylesheets', class_path))
|
50
|
+
|
51
|
+
m.directory(File.join('spec/controllers', controller_class_path))
|
52
|
+
m.directory(File.join('spec/routing', controller_class_path))
|
53
|
+
m.directory(File.join('spec/models', class_path))
|
54
|
+
m.directory(File.join('spec/helpers', class_path))
|
55
|
+
m.directory File.join('spec/fixtures', class_path)
|
56
|
+
m.directory File.join('spec/views', controller_class_path, controller_file_name)
|
57
|
+
m.directory File.join('spec/integration', class_path)
|
58
|
+
|
59
|
+
# Layout and stylesheet.
|
60
|
+
m.template("scaffold:layout.html.erb", File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
|
61
|
+
m.template("scaffold:style.css", 'public/stylesheets/scaffold.css')
|
62
|
+
|
63
|
+
# Controller spec, class, and helper.
|
64
|
+
m.template 'rspec_scaffold:routing_spec.rb',
|
65
|
+
File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
|
66
|
+
|
67
|
+
m.template 'rspec_scaffold:controller_spec.rb',
|
68
|
+
File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
|
69
|
+
|
70
|
+
m.template "scaffold:controller.rb",
|
71
|
+
File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
|
72
|
+
|
73
|
+
m.template 'rspec_scaffold:helper_spec.rb',
|
74
|
+
File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
|
75
|
+
|
76
|
+
m.template "scaffold:helper.rb",
|
77
|
+
File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")
|
78
|
+
|
79
|
+
for action in scaffold_views
|
80
|
+
m.template(
|
81
|
+
"scaffold:view_#{action}.#{@default_file_extension}",
|
82
|
+
File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}")
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Model class, unit test, and fixtures.
|
87
|
+
m.template 'model:model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
88
|
+
m.template 'model:fixtures.yml', File.join('spec/fixtures', class_path, "#{table_name}.yml")
|
89
|
+
m.template 'rspec_model:model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
|
90
|
+
|
91
|
+
# View specs
|
92
|
+
m.template "rspec_scaffold:edit_erb_spec.rb",
|
93
|
+
File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb")
|
94
|
+
m.template "rspec_scaffold:index_erb_spec.rb",
|
95
|
+
File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb")
|
96
|
+
m.template "rspec_scaffold:new_erb_spec.rb",
|
97
|
+
File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb")
|
98
|
+
m.template "rspec_scaffold:show_erb_spec.rb",
|
99
|
+
File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb")
|
100
|
+
|
101
|
+
# Integration
|
102
|
+
m.template 'integration_spec:integration_spec.rb', File.join('spec/integration', class_path, "#{table_name}_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
|