rspec-rails 2.99.0 → 3.1.0

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.
Files changed (80) hide show
  1. checksums.yaml +6 -14
  2. checksums.yaml.gz.sig +1 -0
  3. data/.yardopts +1 -1
  4. data/Capybara.md +1 -3
  5. data/Changelog.md +148 -11
  6. data/License.txt +1 -0
  7. data/README.md +202 -105
  8. data/lib/generators/rspec/controller/controller_generator.rb +1 -0
  9. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  10. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  11. data/lib/generators/rspec/feature/feature_generator.rb +16 -0
  12. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  13. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  14. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  15. data/lib/generators/rspec/install/install_generator.rb +44 -5
  16. data/lib/generators/rspec/install/templates/spec/{spec_helper.rb.tt → rails_helper.rb} +19 -28
  17. data/lib/generators/rspec/integration/integration_generator.rb +1 -12
  18. data/lib/generators/rspec/integration/templates/request_spec.rb +3 -8
  19. data/lib/generators/rspec/job/job_generator.rb +12 -0
  20. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  21. data/lib/generators/rspec/mailer/mailer_generator.rb +1 -0
  22. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +6 -6
  23. data/lib/generators/rspec/model/model_generator.rb +19 -5
  24. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  25. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  26. data/lib/generators/rspec/observer/observer_generator.rb +1 -0
  27. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
  28. data/lib/generators/rspec/scaffold/scaffold_generator.rb +108 -135
  29. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +34 -39
  30. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +5 -13
  31. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -10
  32. data/lib/generators/rspec/scaffold/templates/new_spec.rb +6 -14
  33. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +9 -9
  34. data/lib/generators/rspec/scaffold/templates/show_spec.rb +4 -11
  35. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  36. data/lib/generators/rspec/view/view_generator.rb +1 -0
  37. data/lib/generators/rspec.rb +16 -6
  38. data/lib/rspec/rails/adapters.rb +33 -35
  39. data/lib/rspec/rails/configuration.rb +97 -0
  40. data/lib/rspec/rails/example/controller_example_group.rb +173 -159
  41. data/lib/rspec/rails/example/feature_example_group.rb +25 -20
  42. data/lib/rspec/rails/example/helper_example_group.rb +27 -26
  43. data/lib/rspec/rails/example/mailer_example_group.rb +29 -14
  44. data/lib/rspec/rails/example/model_example_group.rb +7 -7
  45. data/lib/rspec/rails/example/rails_example_group.rb +1 -0
  46. data/lib/rspec/rails/example/request_example_group.rb +19 -17
  47. data/lib/rspec/rails/example/routing_example_group.rb +40 -39
  48. data/lib/rspec/rails/example/view_example_group.rb +140 -137
  49. data/lib/rspec/rails/example.rb +0 -33
  50. data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
  51. data/lib/rspec/rails/extensions.rb +0 -1
  52. data/lib/rspec/rails/feature_check.rb +35 -0
  53. data/lib/rspec/rails/fixture_support.rb +5 -12
  54. data/lib/rspec/rails/matchers/be_a_new.rb +67 -62
  55. data/lib/rspec/rails/matchers/be_new_record.rb +23 -21
  56. data/lib/rspec/rails/matchers/be_valid.rb +41 -33
  57. data/lib/rspec/rails/matchers/have_http_status.rb +361 -0
  58. data/lib/rspec/rails/matchers/have_rendered.rb +35 -31
  59. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  60. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  61. data/lib/rspec/rails/matchers/routing_matchers.rb +104 -94
  62. data/lib/rspec/rails/matchers.rb +7 -4
  63. data/lib/rspec/rails/tasks/rspec.rake +1 -1
  64. data/lib/rspec/rails/vendor/capybara.rb +2 -33
  65. data/lib/rspec/rails/version.rb +3 -1
  66. data/lib/rspec/rails/view_assigns.rb +18 -18
  67. data/lib/rspec/rails/view_rendering.rb +20 -45
  68. data/lib/rspec/rails.rb +2 -13
  69. data/lib/rspec-rails.rb +4 -1
  70. data.tar.gz.sig +0 -0
  71. metadata +80 -99
  72. metadata.gz.sig +3 -0
  73. data/lib/autotest/rails_rspec2.rb +0 -91
  74. data/lib/generators/rspec/install/templates/.rspec +0 -1
  75. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  76. data/lib/rspec/rails/infer_type_configuration.rb +0 -26
  77. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  78. data/lib/rspec/rails/mocks.rb +0 -284
  79. data/lib/rspec/rails/module_inclusion.rb +0 -19
  80. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -1,7 +1,12 @@
1
+ require "rspec/support"
2
+ require "rspec/core"
3
+ RSpec::Support.require_rspec_core "project_initializer"
4
+ require "rspec/rails/feature_check"
5
+
1
6
  module Rspec
2
7
  module Generators
8
+ # @private
3
9
  class InstallGenerator < ::Rails::Generators::Base
4
-
5
10
  desc <<DESC
6
11
  Description:
7
12
  Copy rspec files to your application.
@@ -11,14 +16,48 @@ DESC
11
16
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
17
  end
13
18
 
14
- def copy_dot_rspec
15
- template '.rspec'
19
+ def copy_spec_files
20
+ Dir.mktmpdir do |dir|
21
+ generate_rspec_init dir
22
+ template File.join(dir, '.rspec'), '.rspec'
23
+ directory File.join(dir, 'spec'), 'spec'
24
+ end
25
+ end
26
+
27
+ def copy_rails_files
28
+ template 'spec/rails_helper.rb'
16
29
  end
17
30
 
18
- def copy_spec_files
19
- directory 'spec'
31
+ private
32
+
33
+ def generate_rspec_init(tmpdir)
34
+ initializer = ::RSpec::Core::ProjectInitializer.new(
35
+ :destination => tmpdir,
36
+ :report_stream => StringIO.new
37
+ )
38
+ initializer.run
39
+
40
+ spec_helper_path = File.join(tmpdir, 'spec', 'spec_helper.rb')
41
+
42
+ replace_generator_command(spec_helper_path)
43
+ remove_warnings_configuration(spec_helper_path)
20
44
  end
21
45
 
46
+ def replace_generator_command(spec_helper_path)
47
+ gsub_file spec_helper_path,
48
+ 'rspec --init',
49
+ 'rails generate rspec:install',
50
+ :verbose => false
51
+ end
52
+
53
+ def remove_warnings_configuration(spec_helper_path)
54
+ empty_line = '^\n'
55
+ comment_line = '^\s*#.+\n'
56
+ gsub_file spec_helper_path,
57
+ /#{empty_line}(#{comment_line})+\s+config\.warnings = true\n/,
58
+ '',
59
+ :verbose => false
60
+ end
22
61
  end
23
62
  end
24
63
  end
@@ -1,37 +1,38 @@
1
1
  # This file is copied to spec/ when you run 'rails generate rspec:install'
2
2
  ENV["RAILS_ENV"] ||= 'test'
3
+ require 'spec_helper'
3
4
  require File.expand_path("../../config/environment", __FILE__)
4
5
  require 'rspec/rails'
6
+ # Add additional requires below this line. Rails is not loaded until this point!
5
7
 
6
8
  # Requires supporting ruby files with custom matchers and macros, etc, in
7
9
  # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
8
10
  # run as spec files by default. This means that files in spec/support that end
9
11
  # in _spec.rb will both be required and run as specs, causing the specs to be
10
12
  # run twice. It is recommended that you do not name files matching this glob to
11
- # end with _spec.rb. You can configure this pattern with with the --pattern
13
+ # end with _spec.rb. You can configure this pattern with the --pattern
12
14
  # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
13
- Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
14
-
15
- <% if ::Rails::VERSION::STRING >= '4.1' -%>
15
+ #
16
+ # The following line is provided for convenience purposes. It has the downside
17
+ # of increasing the boot-up time by auto-requiring all files in the support
18
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
19
+ # require only the support files necessary.
20
+ #
21
+ # Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
22
+
23
+ <% if RSpec::Rails::FeatureCheck.can_maintain_test_schema? -%>
16
24
  # Checks for pending migrations before tests are run.
17
25
  # If you are not using ActiveRecord, you can remove this line.
18
26
  ActiveRecord::Migration.maintain_test_schema!
19
27
 
20
- <% elsif ::Rails::VERSION::STRING >= '4' -%>
28
+ <% elsif RSpec::Rails::FeatureCheck.can_check_pending_migrations? -%>
21
29
  # Checks for pending migrations before tests are run.
22
30
  # If you are not using ActiveRecord, you can remove this line.
23
- ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
31
+ ActiveRecord::Migration.check_pending!
24
32
 
25
33
  <% end -%>
26
34
  RSpec.configure do |config|
27
- # ## Mock Framework
28
- #
29
- # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
30
- #
31
- # config.mock_with :mocha
32
- # config.mock_with :flexmock
33
- # config.mock_with :rr
34
-
35
+ <% if RSpec::Rails::FeatureCheck.has_active_record? -%>
35
36
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
36
37
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
37
38
 
@@ -40,29 +41,19 @@ RSpec.configure do |config|
40
41
  # instead of true.
41
42
  config.use_transactional_fixtures = true
42
43
 
43
- # If true, the base class of anonymous controllers will be inferred
44
- # automatically. This will be the default behavior in future versions of
45
- # rspec-rails.
46
- config.infer_base_class_for_anonymous_controllers = false
47
-
48
- # Run specs in random order to surface order dependencies. If you find an
49
- # order dependency and want to debug it, you can fix the order by providing
50
- # the seed, which is printed after each run.
51
- # --seed 1234
52
- config.order = "random"
53
-
44
+ <% end -%>
54
45
  # RSpec Rails can automatically mix in different behaviours to your tests
55
46
  # based on their file location, for example enabling you to call `get` and
56
47
  # `post` in specs under `spec/controllers`.
57
48
  #
58
49
  # You can disable this behaviour by removing the line below, and instead
59
- # explictly tag your specs with their type, e.g.:
50
+ # explicitly tag your specs with their type, e.g.:
60
51
  #
61
- # describe UsersController, :type => :controller do
52
+ # RSpec.describe UsersController, :type => :controller do
62
53
  # # ...
63
54
  # end
64
55
  #
65
56
  # The different available types are documented in the features, such as in
66
- # https://relishapp.com/rspec/rspec-rails/v/3-0/docs
57
+ # https://relishapp.com/rspec/rspec-rails/docs
67
58
  config.infer_spec_type_from_file_location!
68
59
  end
@@ -2,27 +2,16 @@ require 'generators/rspec'
2
2
 
3
3
  module Rspec
4
4
  module Generators
5
+ # @private
5
6
  class IntegrationGenerator < Base
6
7
  class_option :request_specs, :type => :boolean, :default => true, :desc => "Generate request specs"
7
- class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
8
- class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"
9
8
 
10
9
  def generate_request_spec
11
10
  return unless options[:request_specs]
12
11
 
13
- RSpec.deprecate("the --webrat-matchers option", :replacement => nil) if options[:webrat_matchers]
14
- RSpec.deprecate("the --webrat option", :replacement => nil) if options[:webrat]
15
-
16
12
  template 'request_spec.rb',
17
13
  File.join('spec/requests', class_path, "#{table_name}_spec.rb")
18
14
  end
19
-
20
- protected
21
-
22
- def webrat?
23
- options[:webrat] || options[:webrat_matchers]
24
- end
25
-
26
15
  end
27
16
  end
28
17
  end
@@ -1,15 +1,10 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
- describe "<%= class_name.pluralize %>" do
3
+ RSpec.describe "<%= class_name.pluralize %>", :type => :request do
4
4
  describe "GET /<%= table_name %>" do
5
5
  it "works! (now write some real specs)" do
6
- <% if webrat? -%>
7
- visit <%= index_helper %>_path
8
- <% else -%>
9
- # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
10
6
  get <%= index_helper %>_path
11
- <% end -%>
12
- response.status.should be(200)
7
+ expect(response).to have_http_status(200)
13
8
  end
14
9
  end
15
10
  end
@@ -0,0 +1,12 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ # @private
6
+ class JobGenerator < Base
7
+ def create_job_spec
8
+ template 'job_spec.rb.erb', File.join('spec/jobs', class_path, "#{file_name}_job_spec.rb")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ RSpec.describe <%= class_name %>Job, :type => :job do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ <% end -%>
@@ -2,6 +2,7 @@ require 'generators/rspec'
2
2
 
3
3
  module Rspec
4
4
  module Generators
5
+ # @private
5
6
  class MailerGenerator < Base
6
7
  argument :actions, :type => :array, :default => [], :banner => "method method"
7
8
 
@@ -1,19 +1,19 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= class_name %> do
4
+ RSpec.describe <%= class_name %>, :type => :mailer do
5
5
  <% for action in actions -%>
6
6
  describe "<%= action %>" do
7
7
  let(:mail) { <%= class_name %>.<%= action %> }
8
8
 
9
9
  it "renders the headers" do
10
- mail.subject.should eq(<%= action.to_s.humanize.inspect %>)
11
- mail.to.should eq(["to@example.org"])
12
- mail.from.should eq(["from@example.com"])
10
+ expect(mail.subject).to eq(<%= action.to_s.humanize.inspect %>)
11
+ expect(mail.to).to eq(["to@example.org"])
12
+ expect(mail.from).to eq(["from@example.com"])
13
13
  end
14
14
 
15
15
  it "renders the body" do
16
- mail.body.encoded.should match("Hi")
16
+ expect(mail.body.encoded).to match("Hi")
17
17
  end
18
18
  end
19
19
 
@@ -2,20 +2,34 @@ require 'generators/rspec'
2
2
 
3
3
  module Rspec
4
4
  module Generators
5
+ # @private
5
6
  class ModelGenerator < Base
6
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
+ argument :attributes,
8
+ :type => :array,
9
+ :default => [],
10
+ :banner => "field:type field:type"
7
11
  class_option :fixture, :type => :boolean
8
12
 
9
13
  def create_model_spec
10
- template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
14
+ template_file = File.join(
15
+ 'spec/models',
16
+ class_path,
17
+ "#{file_name}_spec.rb"
18
+ )
19
+ template 'model_spec.rb', template_file
11
20
  end
12
21
 
13
22
  hook_for :fixture_replacement
14
23
 
15
24
  def create_fixture_file
16
- if options[:fixture] && options[:fixture_replacement].nil?
17
- template 'fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
18
- end
25
+ return unless missing_fixture_replacement?
26
+ template 'fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
27
+ end
28
+
29
+ private
30
+
31
+ def missing_fixture_replacement?
32
+ options[:fixture] && options[:fixture_replacement].nil?
19
33
  end
20
34
  end
21
35
  end
@@ -1,4 +1,4 @@
1
- # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
2
 
3
3
  <% unless attributes.empty? -%>
4
4
  one:
@@ -1,7 +1,7 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= class_name %> do
4
+ RSpec.describe <%= class_name %>, :type => :model do
5
5
  pending "add some examples to (or delete) #{__FILE__}"
6
6
  end
7
7
  <% end -%>
@@ -2,6 +2,7 @@ require 'generators/rspec'
2
2
 
3
3
  module Rspec
4
4
  module Generators
5
+ # @private
5
6
  class ObserverGenerator < Base
6
7
  def generate_observer_spec
7
8
  template 'observer_spec.rb',
@@ -1,7 +1,7 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= class_name %>Observer do
4
+ RSpec.describe <%= class_name %>Observer, :type => :observer do
5
5
  pending "add some examples to (or delete) #{__FILE__}"
6
6
  end
7
7
  <% end -%>
@@ -3,6 +3,7 @@ require 'rails/generators/resource_helpers'
3
3
 
4
4
  module Rspec
5
5
  module Generators
6
+ # @private
6
7
  class ScaffoldGenerator < Base
7
8
  include ::Rails::Generators::ResourceHelpers
8
9
  source_paths << File.expand_path("../../helper/templates", __FILE__)
@@ -14,19 +15,18 @@ module Rspec
14
15
 
15
16
  class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
16
17
  class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
17
- class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
18
- class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"
19
18
  class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
20
19
  class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
21
20
 
22
21
  def generate_controller_spec
23
22
  return unless options[:controller_specs]
24
23
 
25
- RSpec.deprecate("the --webrat-matchers option", :replacement => nil) if options[:webrat_matchers]
26
- RSpec.deprecate("the --webrat option", :replacement => nil) if options[:webrat]
27
-
28
- template 'controller_spec.rb',
29
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
24
+ template_file = File.join(
25
+ 'spec/controllers',
26
+ controller_class_path,
27
+ "#{controller_file_name}_controller_spec.rb"
28
+ )
29
+ template 'controller_spec.rb', template_file
30
30
  end
31
31
 
32
32
  def generate_view_specs
@@ -41,153 +41,126 @@ module Rspec
41
41
  def generate_routing_spec
42
42
  return unless options[:routing_specs]
43
43
 
44
- template 'routing_spec.rb',
45
- File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
44
+ template_file = File.join(
45
+ 'spec/routing',
46
+ controller_class_path,
47
+ "#{controller_file_name}_routing_spec.rb"
48
+ )
49
+ template 'routing_spec.rb', template_file
46
50
  end
47
51
 
48
52
  hook_for :integration_tool, :as => :integration
49
53
 
50
- protected
51
-
52
- def webrat?
53
- options[:webrat] || options[:webrat_matchers]
54
- end
55
-
56
- def copy_view(view)
57
- template "#{view}_spec.rb",
58
- File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
59
- end
60
-
61
- def example_valid_attributes
62
- # Only take the first attribute so this hash does not become unweildy and large in the
63
- # generated controller spec. It is the responsibility of the user to keep the the valid
64
- # attributes method up-to-date as they add validations.
65
- @example_valid_attributes ||=
66
- if attributes.any?
67
- { attributes.first.name => attributes.first.default.to_s }
68
- else
69
- { }
70
- end
71
- end
54
+ protected
72
55
 
73
- def example_invalid_attributes
74
- @example_invalid_attributes ||=
75
- if attributes.any?
76
- { attributes.first.name => "invalid value" }
77
- else
78
- { }
79
- end
80
- end
81
-
82
- def example_params_for_update
83
- @example_params_for_update ||=
84
- if example_valid_attributes.any?
85
- example_valid_attributes
86
- else
87
- { "these" => "params" }
88
- end
89
- end
56
+ def copy_view(view)
57
+ template "#{view}_spec.rb",
58
+ File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
59
+ end
90
60
 
91
- def formatted_hash(hash)
92
- formatted = hash.inspect
93
- formatted.gsub!("{", "{ ")
94
- formatted.gsub!("}", " }")
95
- formatted.gsub!("=>", " => ")
96
- formatted
97
- end
61
+ def formatted_hash(hash)
62
+ formatted = hash.inspect
63
+ formatted.gsub!("{", "{ ")
64
+ formatted.gsub!("}", " }")
65
+ formatted.gsub!("=>", " => ")
66
+ formatted
67
+ end
98
68
 
99
- # support for namespaced-resources
100
- def ns_file_name
101
- ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
102
- end
69
+ # support for namespaced-resources
70
+ def ns_file_name
71
+ ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
72
+ end
103
73
 
104
- # support for namespaced-resources
105
- def ns_table_name
106
- ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
107
- end
74
+ # support for namespaced-resources
75
+ def ns_table_name
76
+ ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
77
+ end
108
78
 
109
- def ns_parts
110
- @ns_parts ||= begin
111
- matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
112
- matches ? [matches[1], matches[2]] : []
113
- end
114
- end
79
+ def ns_parts
80
+ @ns_parts ||= begin
81
+ matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
82
+ matches ? [matches[1], matches[2]] : []
83
+ end
84
+ end
115
85
 
116
- # Returns the name of the mock. For example, if the file name is user,
117
- # it returns mock_user.
118
- #
119
- # If a hash is given, it uses the hash key as the ORM method and the
120
- # value as response. So, for ActiveRecord and file name "User":
121
- #
122
- # mock_file_name(:save => true)
123
- # #=> mock_user(:save => true)
124
- #
125
- # If another ORM is being used and another method instead of save is
126
- # called, it will be the one used.
127
- #
128
- def mock_file_name(hash=nil)
129
- if hash
130
- method, and_return = hash.to_a.first
131
- method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
132
- "mock_#{ns_file_name}(:#{method} => #{and_return})"
133
- else
134
- "mock_#{ns_file_name}"
135
- end
86
+ # Returns the name of the mock. For example, if the file name is user,
87
+ # it returns mock_user.
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}"
136
105
  end
106
+ end
137
107
 
138
- # Receives the ORM chain and convert to expects. For ActiveRecord:
139
- #
140
- # should! orm_class.find(User, "37")
141
- # #=> User.should_receive(:find).with(37)
142
- #
143
- # For Datamapper:
144
- #
145
- # should! orm_class.find(User, "37")
146
- # #=> User.should_receive(:get).with(37)
147
- #
148
- def should_receive(chain)
149
- stub_or_should_chain(:should_receive, chain)
150
- end
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)
120
+ end
151
121
 
152
- # Receives the ORM chain and convert to stub. For ActiveRecord:
153
- #
154
- # stub orm_class.find(User, "37")
155
- # #=> User.stub(:find).with(37)
156
- #
157
- # For Datamapper:
158
- #
159
- # stub orm_class.find(User, "37")
160
- # #=> User.stub(:get).with(37)
161
- #
162
- def stub(chain)
163
- stub_or_should_chain(:stub, chain)
164
- end
122
+ # Receives the ORM chain and convert to stub. For ActiveRecord:
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
165
135
 
166
- def stub_or_should_chain(mode, chain)
167
- receiver, method = chain.split(".")
168
- method.gsub!(/\((.*?)\)/, '')
136
+ def stub_or_should_chain(mode, chain)
137
+ receiver, method = chain.split(".")
138
+ method.gsub!(/\((.*?)\)/, '')
169
139
 
170
- response = "#{receiver}.#{mode}(:#{method})"
171
- response << ".with(#{$1})" unless $1.blank?
172
- response
173
- end
140
+ response = "#{receiver}.#{mode}(:#{method})"
141
+ response << ".with(#{$1})" unless $1.blank?
142
+ response
143
+ end
174
144
 
175
- def value_for(attribute)
176
- case attribute.type
177
- when :string
178
- "#{attribute.name.titleize}".inspect
179
- when :integer
180
- @attribute_id_map ||= {}
181
- @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next.to_s
182
- else
183
- attribute.default.inspect
184
- end
185
- end
145
+ def value_for(attribute)
146
+ raw_value_for(attribute).inspect
147
+ end
186
148
 
187
- def banner
188
- self.class.banner
149
+ def raw_value_for(attribute)
150
+ case attribute.type
151
+ when :string
152
+ attribute.name.titleize
153
+ when :integer
154
+ @attribute_id_map ||= {}
155
+ @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next
156
+ else
157
+ attribute.default
189
158
  end
159
+ end
190
160
 
161
+ def banner
162
+ self.class.banner
163
+ end
191
164
  end
192
165
  end
193
166
  end