rspec-rails 3.5.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/Capybara.md +6 -55
- data/Changelog.md +321 -62
- data/README.md +265 -493
- data/lib/generators/rspec/channel/channel_generator.rb +12 -0
- data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
- data/lib/generators/rspec/controller/controller_generator.rb +21 -4
- data/lib/generators/rspec/controller/templates/request_spec.rb +14 -0
- data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
- data/lib/generators/rspec/feature/feature_generator.rb +4 -4
- data/lib/generators/rspec/generator/generator_generator.rb +24 -0
- data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
- data/lib/generators/rspec/helper/helper_generator.rb +1 -1
- data/lib/generators/rspec/install/install_generator.rb +4 -4
- data/lib/generators/rspec/install/templates/spec/rails_helper.rb +27 -14
- data/lib/generators/rspec/integration/integration_generator.rb +4 -4
- data/lib/generators/rspec/integration/templates/request_spec.rb +1 -1
- data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
- data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
- data/lib/generators/rspec/mailer/mailer_generator.rb +2 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
- data/lib/generators/rspec/mailer/templates/preview.rb +1 -1
- data/lib/generators/rspec/model/model_generator.rb +6 -5
- data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
- data/lib/generators/rspec/request/request_generator.rb +1 -1
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +52 -86
- data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
- data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +40 -74
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +5 -1
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +5 -1
- data/lib/generators/rspec/scaffold/templates/request_spec.rb +133 -0
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +13 -12
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +1 -1
- data/lib/generators/rspec/system/system_generator.rb +26 -0
- data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
- data/lib/generators/rspec/view/view_generator.rb +2 -2
- data/lib/generators/rspec.rb +0 -6
- data/lib/rspec/rails/active_record.rb +1 -1
- data/lib/rspec/rails/adapters.rb +11 -76
- data/lib/rspec/rails/configuration.rb +47 -31
- data/lib/rspec/rails/example/channel_example_group.rb +93 -0
- data/lib/rspec/rails/example/controller_example_group.rb +4 -4
- data/lib/rspec/rails/example/feature_example_group.rb +6 -26
- data/lib/rspec/rails/example/helper_example_group.rb +2 -9
- data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
- data/lib/rspec/rails/example/mailer_example_group.rb +1 -1
- data/lib/rspec/rails/example/rails_example_group.rb +1 -2
- data/lib/rspec/rails/example/request_example_group.rb +1 -3
- data/lib/rspec/rails/example/system_example_group.rb +124 -0
- data/lib/rspec/rails/example/view_example_group.rb +47 -28
- data/lib/rspec/rails/example.rb +3 -0
- data/lib/rspec/rails/extensions/active_record/proxy.rb +1 -9
- data/lib/rspec/rails/feature_check.rb +12 -29
- data/lib/rspec/rails/fixture_file_upload_support.rb +40 -0
- data/lib/rspec/rails/fixture_support.rb +37 -31
- data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
- data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
- data/lib/rspec/rails/matchers/action_cable.rb +65 -0
- data/lib/rspec/rails/matchers/action_mailbox.rb +64 -0
- data/lib/rspec/rails/matchers/active_job.rb +208 -18
- data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
- data/lib/rspec/rails/matchers/be_a_new.rb +8 -7
- data/lib/rspec/rails/matchers/be_new_record.rb +3 -3
- data/lib/rspec/rails/matchers/be_valid.rb +1 -1
- data/lib/rspec/rails/matchers/have_enqueued_mail.rb +198 -0
- data/lib/rspec/rails/matchers/have_http_status.rb +34 -13
- data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
- data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +14 -14
- data/lib/rspec/rails/matchers.rb +11 -0
- data/lib/rspec/rails/tasks/rspec.rake +7 -17
- data/lib/rspec/rails/vendor/capybara.rb +12 -13
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_path_builder.rb +2 -2
- data/lib/rspec/rails/view_rendering.rb +18 -5
- data/lib/rspec/rails.rb +1 -0
- data/lib/rspec-rails.rb +17 -11
- data.tar.gz.sig +0 -0
- metadata +57 -33
- metadata.gz.sig +0 -0
- data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'generators/rspec'
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
# @private
|
6
|
+
class ChannelGenerator < Base
|
7
|
+
def create_channel_spec
|
8
|
+
template 'channel_spec.rb.erb', File.join('spec/channels', class_path, "#{file_name}_channel_spec.rb")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rails_helper'
|
2
2
|
|
3
3
|
<% module_namespacing do -%>
|
4
|
-
RSpec.describe <%= class_name %>
|
4
|
+
RSpec.describe <%= class_name %>Channel, <%= type_metatag(:channel) %> do
|
5
5
|
pending "add some examples to (or delete) #{__FILE__}"
|
6
6
|
end
|
7
7
|
<% end -%>
|
@@ -4,11 +4,20 @@ module Rspec
|
|
4
4
|
module Generators
|
5
5
|
# @private
|
6
6
|
class ControllerGenerator < Base
|
7
|
-
argument :actions, :
|
7
|
+
argument :actions, type: :array, default: [], banner: "action action"
|
8
8
|
|
9
|
-
class_option :template_engine,
|
10
|
-
class_option :
|
11
|
-
class_option :
|
9
|
+
class_option :template_engine, desc: "Template engine to generate view files"
|
10
|
+
class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
|
11
|
+
class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
|
12
|
+
class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
|
13
|
+
class_option :routing_specs, type: :boolean, default: false, desc: "Generate routing specs"
|
14
|
+
|
15
|
+
def generate_request_spec
|
16
|
+
return unless options[:request_specs]
|
17
|
+
|
18
|
+
template 'request_spec.rb',
|
19
|
+
File.join('spec/requests', class_path, "#{file_name}_request_spec.rb")
|
20
|
+
end
|
12
21
|
|
13
22
|
def generate_controller_spec
|
14
23
|
return unless options[:controller_specs]
|
@@ -29,6 +38,14 @@ module Rspec
|
|
29
38
|
File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
|
30
39
|
end
|
31
40
|
end
|
41
|
+
|
42
|
+
def generate_routing_spec
|
43
|
+
return if actions.empty?
|
44
|
+
return unless options[:routing_specs]
|
45
|
+
|
46
|
+
template 'routing_spec.rb',
|
47
|
+
File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
|
48
|
+
end
|
32
49
|
end
|
33
50
|
end
|
34
51
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
|
4
|
+
<% namespaced_path = regular_class_path.join('/') %>
|
5
|
+
<% for action in actions -%>
|
6
|
+
describe "GET /<%= action %>" do
|
7
|
+
it "returns http success" do
|
8
|
+
get "<%= "/#{namespaced_path}" if namespaced_path != '' %>/<%= file_name %>/<%= action %>"
|
9
|
+
expect(response).to have_http_status(:success)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
<% end -%>
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
<% module_namespacing do -%>
|
4
|
+
RSpec.describe '<%= class_name %>Controller', <%= type_metatag(:routing) %> do
|
5
|
+
describe 'routing' do
|
6
|
+
<% for action in actions -%>
|
7
|
+
it 'routes to #<%= action %>' do
|
8
|
+
expect(get: "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>")
|
9
|
+
end
|
10
|
+
<% end -%>
|
11
|
+
end
|
12
|
+
end
|
13
|
+
<% end -%>
|
@@ -4,8 +4,8 @@ module Rspec
|
|
4
4
|
module Generators
|
5
5
|
# @private
|
6
6
|
class FeatureGenerator < Base
|
7
|
-
class_option :feature_specs, :
|
8
|
-
class_option :singularize, :
|
7
|
+
class_option :feature_specs, type: :boolean, default: true, desc: "Generate feature specs"
|
8
|
+
class_option :singularize, type: :boolean, default: false, desc: "Singularize the generated feature"
|
9
9
|
|
10
10
|
def generate_feature_spec
|
11
11
|
return unless options[:feature_specs]
|
@@ -19,9 +19,9 @@ module Rspec
|
|
19
19
|
|
20
20
|
def filename
|
21
21
|
if options[:singularize]
|
22
|
-
"#{
|
22
|
+
"#{file_name.singularize}_spec.rb"
|
23
23
|
else
|
24
|
-
"#{
|
24
|
+
"#{file_name}_spec.rb"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'generators/rspec'
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
# @private
|
6
|
+
class GeneratorGenerator < Base
|
7
|
+
class_option :generator_specs, type: :boolean, default: true, desc: "Generate generator specs"
|
8
|
+
|
9
|
+
def generate_generator_spec
|
10
|
+
return unless options[:generator_specs]
|
11
|
+
|
12
|
+
template template_name, File.join('spec/generator', class_path, filename)
|
13
|
+
end
|
14
|
+
|
15
|
+
def template_name
|
16
|
+
'generator_spec.rb'
|
17
|
+
end
|
18
|
+
|
19
|
+
def filename
|
20
|
+
"#{table_name}_generator_spec.rb"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -4,7 +4,7 @@ module Rspec
|
|
4
4
|
module Generators
|
5
5
|
# @private
|
6
6
|
class HelperGenerator < Base
|
7
|
-
class_option :helper_specs, :
|
7
|
+
class_option :helper_specs, type: :boolean, default: true
|
8
8
|
|
9
9
|
def generate_helper_spec
|
10
10
|
return unless options[:helper_specs]
|
@@ -32,8 +32,8 @@ DESC
|
|
32
32
|
|
33
33
|
def generate_rspec_init(tmpdir)
|
34
34
|
initializer = ::RSpec::Core::ProjectInitializer.new(
|
35
|
-
:
|
36
|
-
:
|
35
|
+
destination: tmpdir,
|
36
|
+
report_stream: StringIO.new
|
37
37
|
)
|
38
38
|
initializer.run
|
39
39
|
|
@@ -47,7 +47,7 @@ DESC
|
|
47
47
|
gsub_file spec_helper_path,
|
48
48
|
'rspec --init',
|
49
49
|
'rails generate rspec:install',
|
50
|
-
:
|
50
|
+
verbose: false
|
51
51
|
end
|
52
52
|
|
53
53
|
def remove_warnings_configuration(spec_helper_path)
|
@@ -56,7 +56,7 @@ DESC
|
|
56
56
|
gsub_file spec_helper_path,
|
57
57
|
/#{empty_line}(#{comment_line})+\s+config\.warnings = true\n/,
|
58
58
|
'',
|
59
|
-
:
|
59
|
+
verbose: false
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
require 'spec_helper'
|
2
3
|
ENV['RAILS_ENV'] ||= 'test'
|
3
|
-
require File.expand_path('
|
4
|
+
require File.expand_path('../config/environment', __dir__)
|
4
5
|
# Prevent database truncation if the environment is production
|
5
6
|
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
6
|
-
require 'spec_helper'
|
7
7
|
require 'rspec/rails'
|
8
8
|
# Add additional requires below this line. Rails is not loaded until this point!
|
9
9
|
|
@@ -20,18 +20,17 @@ require 'rspec/rails'
|
|
20
20
|
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
21
21
|
# require only the support files necessary.
|
22
22
|
#
|
23
|
-
# Dir[Rails.root.join('spec
|
24
|
-
|
25
|
-
<% if RSpec::Rails::FeatureCheck.can_maintain_test_schema? -%>
|
26
|
-
# Checks for pending migration and applies them before tests are run.
|
27
|
-
# If you are not using ActiveRecord, you can remove this line.
|
28
|
-
ActiveRecord::Migration.maintain_test_schema!
|
29
|
-
|
30
|
-
<% elsif RSpec::Rails::FeatureCheck.can_check_pending_migrations? -%>
|
31
|
-
# Checks for pending migrations before tests are run.
|
32
|
-
# If you are not using ActiveRecord, you can remove this line.
|
33
|
-
ActiveRecord::Migration.check_pending!
|
23
|
+
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
|
34
24
|
|
25
|
+
<% if RSpec::Rails::FeatureCheck.has_active_record_migration? -%>
|
26
|
+
# Checks for pending migrations and applies them before tests are run.
|
27
|
+
# If you are not using ActiveRecord, you can remove these lines.
|
28
|
+
begin
|
29
|
+
ActiveRecord::Migration.maintain_test_schema!
|
30
|
+
rescue ActiveRecord::PendingMigrationError => e
|
31
|
+
puts e.to_s.strip
|
32
|
+
exit 1
|
33
|
+
end
|
35
34
|
<% end -%>
|
36
35
|
RSpec.configure do |config|
|
37
36
|
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
|
@@ -43,6 +42,20 @@ RSpec.configure do |config|
|
|
43
42
|
# instead of true.
|
44
43
|
config.use_transactional_fixtures = true
|
45
44
|
|
45
|
+
# You can uncomment this line to turn off ActiveRecord support entirely.
|
46
|
+
# config.use_active_record = false
|
47
|
+
|
48
|
+
<% else -%>
|
49
|
+
# Remove this line to enable support for ActiveRecord
|
50
|
+
config.use_active_record = false
|
51
|
+
|
52
|
+
# If you enable ActiveRecord support you should unncomment these lines,
|
53
|
+
# note if you'd prefer not to run each example within a transaction, you
|
54
|
+
# should set use_transactional_fixtures to false.
|
55
|
+
#
|
56
|
+
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
57
|
+
# config.use_transactional_fixtures = true
|
58
|
+
|
46
59
|
<% end -%>
|
47
60
|
# RSpec Rails can automatically mix in different behaviours to your tests
|
48
61
|
# based on their file location, for example enabling you to call `get` and
|
@@ -51,7 +64,7 @@ RSpec.configure do |config|
|
|
51
64
|
# You can disable this behaviour by removing the line below, and instead
|
52
65
|
# explicitly tag your specs with their type, e.g.:
|
53
66
|
#
|
54
|
-
# RSpec.describe UsersController, :
|
67
|
+
# RSpec.describe UsersController, type: :controller do
|
55
68
|
# # ...
|
56
69
|
# end
|
57
70
|
#
|
@@ -7,15 +7,15 @@ module Rspec
|
|
7
7
|
# Add a deprecation for this class, before rspec-rails 4, to use the
|
8
8
|
# `RequestGenerator` instead
|
9
9
|
class_option :request_specs,
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
10
|
+
type: :boolean,
|
11
|
+
default: true,
|
12
|
+
desc: "Generate request specs"
|
13
13
|
|
14
14
|
def generate_request_spec
|
15
15
|
return unless options[:request_specs]
|
16
16
|
|
17
17
|
template 'request_spec.rb',
|
18
|
-
File.join('spec/requests',
|
18
|
+
File.join('spec/requests', "#{name.underscore.pluralize}_spec.rb")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rails_helper'
|
2
2
|
|
3
3
|
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
|
4
|
-
describe "GET /<%=
|
4
|
+
describe "GET /<%= name.underscore.pluralize %>" do
|
5
5
|
it "works! (now write some real specs)" do
|
6
6
|
get <%= index_helper %>_path
|
7
7
|
expect(response).to have_http_status(200)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'generators/rspec'
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
# @private
|
6
|
+
class MailboxGenerator < Base
|
7
|
+
def create_mailbox_spec
|
8
|
+
template('mailbox_spec.rb.erb',
|
9
|
+
File.join('spec/mailboxes', class_path, "#{file_name}_mailbox_spec.rb")
|
10
|
+
)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -5,7 +5,7 @@ module Rspec
|
|
5
5
|
module Generators
|
6
6
|
# @private
|
7
7
|
class MailerGenerator < Base
|
8
|
-
argument :actions, :
|
8
|
+
argument :actions, type: :array, default: [], banner: "method method"
|
9
9
|
|
10
10
|
def generate_mailer_spec
|
11
11
|
template "mailer_spec.rb", File.join('spec/mailers', class_path, "#{file_name}_spec.rb")
|
@@ -20,6 +20,7 @@ module Rspec
|
|
20
20
|
|
21
21
|
def generate_preview_files
|
22
22
|
return unless RSpec::Rails::FeatureCheck.has_action_mailer_preview?
|
23
|
+
|
23
24
|
template "preview.rb", File.join("spec/mailers/previews", class_path, "#{file_name}_preview.rb")
|
24
25
|
end
|
25
26
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "rails_helper"
|
2
2
|
|
3
3
|
<% module_namespacing do -%>
|
4
|
-
RSpec.describe <%=
|
4
|
+
RSpec.describe <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>, <%= type_metatag(:mailer) %> do
|
5
5
|
<% for action in actions -%>
|
6
6
|
describe "<%= action %>" do
|
7
|
-
let(:mail) { <%=
|
7
|
+
let(:mail) { <%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %> }
|
8
8
|
|
9
9
|
it "renders the headers" do
|
10
10
|
expect(mail.subject).to eq(<%= action.to_s.humanize.inspect %>)
|
@@ -5,7 +5,7 @@ class <%= class_name %>Preview < ActionMailer::Preview
|
|
5
5
|
|
6
6
|
# Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %>
|
7
7
|
def <%= action %>
|
8
|
-
<%=
|
8
|
+
<%= Rails.version.to_f >= 5.0 ? class_name.sub(/(Mailer)?$/, 'Mailer') : class_name %>.<%= action %>
|
9
9
|
end
|
10
10
|
<% end -%>
|
11
11
|
|
@@ -5,10 +5,10 @@ module Rspec
|
|
5
5
|
# @private
|
6
6
|
class ModelGenerator < Base
|
7
7
|
argument :attributes,
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
11
|
-
class_option :fixture, :
|
8
|
+
type: :array,
|
9
|
+
default: [],
|
10
|
+
banner: "field:type field:type"
|
11
|
+
class_option :fixture, type: :boolean
|
12
12
|
|
13
13
|
def create_model_spec
|
14
14
|
template_file = File.join(
|
@@ -23,7 +23,8 @@ module Rspec
|
|
23
23
|
|
24
24
|
def create_fixture_file
|
25
25
|
return unless missing_fixture_replacement?
|
26
|
-
|
26
|
+
|
27
|
+
template 'fixtures.yml', File.join('spec/fixtures', class_path, "#{(pluralize_table_names? ? plural_file_name : file_name)}.yml")
|
27
28
|
end
|
28
29
|
|
29
30
|
private
|
@@ -6,30 +6,47 @@ module Rspec
|
|
6
6
|
# @private
|
7
7
|
class ScaffoldGenerator < Base
|
8
8
|
include ::Rails::Generators::ResourceHelpers
|
9
|
-
source_paths << File.expand_path(
|
10
|
-
argument :attributes, :
|
11
|
-
|
12
|
-
class_option :orm, :
|
13
|
-
class_option :template_engine, :
|
14
|
-
class_option :singleton, :
|
15
|
-
|
16
|
-
|
17
|
-
class_option :
|
18
|
-
class_option :
|
19
|
-
class_option :
|
9
|
+
source_paths << File.expand_path('../helper/templates', __dir__)
|
10
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
11
|
+
|
12
|
+
class_option :orm, desc: "ORM used to generate the controller"
|
13
|
+
class_option :template_engine, desc: "Template engine to generate view files"
|
14
|
+
class_option :singleton, type: :boolean, desc: "Supply to create a singleton controller"
|
15
|
+
class_option :api, type: :boolean, desc: "Skip specs unnecessary for API-only apps"
|
16
|
+
|
17
|
+
class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
|
18
|
+
class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
|
19
|
+
class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
|
20
|
+
class_option :helper_specs, type: :boolean, default: true, desc: "Generate helper specs"
|
21
|
+
class_option :routing_specs, type: :boolean, default: true, desc: "Generate routing specs"
|
22
|
+
|
23
|
+
def initialize(*args, &blk)
|
24
|
+
@generator_args = args.first
|
25
|
+
super(*args, &blk)
|
26
|
+
end
|
20
27
|
|
21
28
|
def generate_controller_spec
|
22
29
|
return unless options[:controller_specs]
|
23
30
|
|
24
|
-
|
25
|
-
'
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
31
|
+
if options[:api]
|
32
|
+
template 'api_controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
|
33
|
+
else
|
34
|
+
template 'controller_spec.rb', template_file(folder: 'controllers', suffix: '_controller')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_request_spec
|
39
|
+
return unless options[:request_specs]
|
40
|
+
|
41
|
+
if options[:api]
|
42
|
+
template 'api_request_spec.rb', template_file(folder: 'requests')
|
43
|
+
else
|
44
|
+
template 'request_spec.rb', template_file(folder: 'requests')
|
45
|
+
end
|
30
46
|
end
|
31
47
|
|
32
48
|
def generate_view_specs
|
49
|
+
return if options[:api]
|
33
50
|
return unless options[:view_specs] && options[:template_engine]
|
34
51
|
|
35
52
|
copy_view :edit
|
@@ -49,97 +66,42 @@ module Rspec
|
|
49
66
|
template 'routing_spec.rb', template_file
|
50
67
|
end
|
51
68
|
|
52
|
-
hook_for :integration_tool, :as => :integration
|
53
|
-
|
54
69
|
protected
|
55
70
|
|
71
|
+
attr_reader :generator_args
|
72
|
+
|
56
73
|
def copy_view(view)
|
57
74
|
template "#{view}_spec.rb",
|
58
75
|
File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
|
59
76
|
end
|
60
77
|
|
61
|
-
def formatted_hash(hash)
|
62
|
-
formatted = hash.inspect
|
63
|
-
formatted.gsub!("{", "{ ")
|
64
|
-
formatted.gsub!("}", " }")
|
65
|
-
formatted.gsub!("=>", " => ")
|
66
|
-
formatted
|
67
|
-
end
|
68
|
-
|
69
78
|
# support for namespaced-resources
|
70
79
|
def ns_file_name
|
71
|
-
|
80
|
+
return file_name if ns_parts.empty?
|
81
|
+
|
82
|
+
"#{ns_prefix.map(&:underscore).join('/')}_#{ns_suffix.singularize.underscore}"
|
72
83
|
end
|
73
84
|
|
74
85
|
# support for namespaced-resources
|
75
86
|
def ns_table_name
|
76
|
-
|
87
|
+
return table_name if ns_parts.empty?
|
88
|
+
|
89
|
+
"#{ns_prefix.map(&:underscore).join('/')}/#{ns_suffix.tableize}"
|
77
90
|
end
|
78
91
|
|
79
92
|
def ns_parts
|
80
93
|
@ns_parts ||= begin
|
81
|
-
|
82
|
-
|
94
|
+
parts = generator_args[0].split(/\/|::/)
|
95
|
+
parts.size > 1 ? parts : []
|
83
96
|
end
|
84
97
|
end
|
85
98
|
|
86
|
-
|
87
|
-
|
88
|
-
#
|
89
|
-
# If a hash is given, it uses the hash key as the ORM method and the
|
90
|
-
# value as response. So, for ActiveRecord and file name "User":
|
91
|
-
#
|
92
|
-
# mock_file_name(:save => true)
|
93
|
-
# #=> mock_user(:save => true)
|
94
|
-
#
|
95
|
-
# If another ORM is being used and another method instead of save is
|
96
|
-
# called, it will be the one used.
|
97
|
-
#
|
98
|
-
def mock_file_name(hash = nil)
|
99
|
-
if hash
|
100
|
-
method, and_return = hash.to_a.first
|
101
|
-
method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
|
102
|
-
"mock_#{ns_file_name}(:#{method} => #{and_return})"
|
103
|
-
else
|
104
|
-
"mock_#{ns_file_name}"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
# Receives the ORM chain and convert to expects. For ActiveRecord:
|
109
|
-
#
|
110
|
-
# should! orm_class.find(User, "37")
|
111
|
-
# #=> User.should_receive(:find).with(37)
|
112
|
-
#
|
113
|
-
# For Datamapper:
|
114
|
-
#
|
115
|
-
# should! orm_class.find(User, "37")
|
116
|
-
# #=> User.should_receive(:get).with(37)
|
117
|
-
#
|
118
|
-
def should_receive(chain)
|
119
|
-
stub_or_should_chain(:should_receive, chain)
|
99
|
+
def ns_prefix
|
100
|
+
@ns_prefix ||= ns_parts[0..-2]
|
120
101
|
end
|
121
102
|
|
122
|
-
|
123
|
-
|
124
|
-
# stub orm_class.find(User, "37")
|
125
|
-
# #=> User.stub(:find).with(37)
|
126
|
-
#
|
127
|
-
# For Datamapper:
|
128
|
-
#
|
129
|
-
# stub orm_class.find(User, "37")
|
130
|
-
# #=> User.stub(:get).with(37)
|
131
|
-
#
|
132
|
-
def stub(chain)
|
133
|
-
stub_or_should_chain(:stub, chain)
|
134
|
-
end
|
135
|
-
|
136
|
-
def stub_or_should_chain(mode, chain)
|
137
|
-
receiver, method = chain.split(".")
|
138
|
-
method.gsub!(/\((.*?)\)/, '')
|
139
|
-
|
140
|
-
response = "#{receiver}.#{mode}(:#{method})"
|
141
|
-
response << ".with(#{$1})" unless $1.blank?
|
142
|
-
response
|
103
|
+
def ns_suffix
|
104
|
+
@ns_suffix ||= ns_parts[-1]
|
143
105
|
end
|
144
106
|
|
145
107
|
def value_for(attribute)
|
@@ -158,6 +120,10 @@ module Rspec
|
|
158
120
|
end
|
159
121
|
end
|
160
122
|
|
123
|
+
def template_file(folder:, suffix: '')
|
124
|
+
File.join('spec', folder, controller_class_path, "#{controller_file_name}#{suffix}_spec.rb")
|
125
|
+
end
|
126
|
+
|
161
127
|
def banner
|
162
128
|
self.class.banner
|
163
129
|
end
|