rspec-rails 2.0.0.beta.12 → 2.0.0.beta.13

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 (43) hide show
  1. data/Gemfile +8 -2
  2. data/README.markdown +39 -0
  3. data/Rakefile +16 -32
  4. data/VERSION +1 -1
  5. data/features/routing_specs/be_routable_matcher.feature +64 -0
  6. data/features/support/env.rb +19 -2
  7. data/lib/autotest/rails_rspec2.rb +4 -1
  8. data/lib/generators/rspec/install/templates/lib/tasks/rspec.rake +1 -1
  9. data/lib/generators/rspec/scaffold/scaffold_generator.rb +1 -1
  10. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -1
  11. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +1 -1
  12. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  13. data/lib/rspec/rails.rb +0 -1
  14. data/lib/rspec/rails/adapters.rb +2 -2
  15. data/lib/rspec/rails/example.rb +1 -0
  16. data/lib/rspec/rails/example/controller_example_group.rb +3 -1
  17. data/lib/rspec/rails/example/helper_example_group.rb +3 -2
  18. data/lib/rspec/rails/example/mailer_example_group.rb +1 -0
  19. data/lib/rspec/rails/example/request_example_group.rb +15 -0
  20. data/lib/rspec/rails/example/routing_example_group.rb +25 -0
  21. data/lib/rspec/rails/example/view_example_group.rb +21 -7
  22. data/lib/rspec/rails/matchers.rb +16 -58
  23. data/lib/rspec/rails/matchers/be_a_new.rb +5 -0
  24. data/lib/rspec/rails/matchers/have_extension.rb +23 -0
  25. data/lib/rspec/rails/matchers/redirect_to.rb +15 -0
  26. data/lib/rspec/rails/matchers/render_template.rb +16 -0
  27. data/lib/rspec/rails/matchers/routing_spec_matchers.rb +28 -0
  28. data/lib/rspec/rails/monkey.rb +0 -3
  29. data/lib/rspec/rails/view_rendering.rb +4 -1
  30. data/rspec-rails.gemspec +18 -11
  31. data/spec/rspec/rails/example/helper_example_group_spec.rb +1 -1
  32. data/spec/rspec/rails/example/routing_example_group_spec.rb +13 -0
  33. data/spec/rspec/rails/example/view_example_group_spec.rb +3 -0
  34. data/spec/rspec/rails/matchers/redirect_to_spec.rb +1 -1
  35. data/spec/rspec/rails/matchers/render_template_spec.rb +1 -1
  36. data/spec/rspec/rails/matchers/route_to_spec.rb +53 -0
  37. data/templates/Gemfile +7 -26
  38. data/templates/run_specs.rb +1 -1
  39. metadata +26 -19
  40. data/lib/rspec/rails/monkey/action_controller/test_case.rb +0 -207
  41. data/lib/rspec/rails/monkey/action_view/test_case.rb +0 -203
  42. data/lib/rspec/rails/monkey/active_support/notifications/fanout.rb +0 -20
  43. data/lib/rspec/rails/null_resolver.rb +0 -10
data/Gemfile CHANGED
@@ -2,9 +2,15 @@ source "http://rubygems.org"
2
2
 
3
3
  gem 'arel', :path => "./vendor/arel"
4
4
  gem 'rails', :path => "./vendor/rails"
5
- gem 'rspec', '>= 2.0.0.beta.10'
5
+
6
+ gem 'rspec-core', :path => "../rspec-core"
7
+ gem 'rspec-expectations', :path => "../rspec-expectations"
8
+ gem 'rspec-mocks', :path => "../rspec-mocks"
9
+ gem 'rspec', :path => "../rspec"
10
+
11
+ gem 'thor'
6
12
  gem 'cucumber'
7
13
  gem 'aruba'
8
14
  gem 'jeweler'
9
15
  gem 'webrat'
10
-
16
+ gem 'sqlite3-ruby', :require => 'sqlite3'
@@ -1,5 +1,9 @@
1
1
  # rspec-rails-2
2
2
 
3
+ This README aligns with the code in git HEAD. If you're looking for the README for
4
+ the latest release, go to [http://github.com/rspec/rspec-rails](http://github.com/rspec/rspec-rails),
5
+ and select the appropriate tag from the Switch Tags select list.
6
+
3
7
  ## RSpec-2 for Rails-3
4
8
 
5
9
  rspec-rails-2 brings rspec-2 to rails-3 with lightweight extensions to both
@@ -132,6 +136,14 @@ Delegates to assert_redirect
132
136
 
133
137
  View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
134
138
 
139
+ describe "events/index.html.erb" do
140
+ it "renders _event partial for each event" do
141
+ assign(:events, [stub_model(Event), stub_model(Event)])
142
+ render
143
+ view.should render_template(:partial => "_event", :count => 2)
144
+ end
145
+ end
146
+
135
147
  describe "events/show.html.erb" do
136
148
  it "displays the event location" do
137
149
  assign(:event, stub_model(Event,
@@ -167,6 +179,33 @@ This represents the rendered view.
167
179
 
168
180
  `rendered` replaces `response` from rspec-rails-1.3
169
181
 
182
+ # Routing specs
183
+
184
+ Routing specs live in spec/routing.
185
+
186
+ describe "routing to profiles" do
187
+ it "routes /profile/:username to profile#show for username" do
188
+ { :get => "/profiles/jsmith" }.should route_to(
189
+ :controller => "profiles",
190
+ :action => "show",
191
+ :username => "jsmith"
192
+ )
193
+ end
194
+
195
+ it "does not expose a list of profiles" do
196
+ { :get => "/profiles" }.should_not be_routable
197
+ end
198
+ end
199
+
200
+ ## `route_to`
201
+
202
+ Delegates to Rails' assert_routing.
203
+
204
+ ## `be_routable`
205
+
206
+ Passes if the path is recognized by Rails' routing. This is primarily intended
207
+ to be used with `should_not` to specify routes that should not be routable.
208
+
170
209
  # Helper specs
171
210
 
172
211
  Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.
data/Rakefile CHANGED
@@ -1,3 +1,16 @@
1
+ unless File.directory?("vendor/rails") && File.directory?("vendor/arel")
2
+ raise <<-MESSAGE
3
+ You need to clone the rails and arel git repositories into ./vendor
4
+ before you can use any of the rake tasks.
5
+
6
+ git clone git://github.com/rails/arel.git vendor/arel
7
+ git clone git://github.com/rails/rails.git vendor/rails
8
+
9
+ MESSAGE
10
+ end
11
+ require "bundler"
12
+ Bundler.setup
13
+
1
14
  require 'rake'
2
15
  require 'yaml'
3
16
 
@@ -54,41 +67,12 @@ namespace :gem do
54
67
  end
55
68
  end
56
69
 
57
- namespace :rails do
58
- desc "clone the rails repo"
59
- task :clone do
60
- mkdir 'vendor' unless File.directory?('vendor')
61
- unless File.directory?('vendor/rails')
62
- Dir.chdir('vendor') do
63
- sh "git clone git://github.com/rails/rails"
64
- end
65
- end
66
- unless File.directory?('vendor/arel')
67
- Dir.chdir('vendor') do
68
- sh "git clone git://github.com/rails/arel"
69
- end
70
- end
71
- end
72
-
73
- desc "update the rails repo"
74
- task :update => :clone do
75
- Dir.chdir('vendor/rails') do
76
- sh "git checkout master"
77
- sh "git pull"
78
- end
79
- Dir.chdir('vendor/arel') do
80
- sh "git checkout master"
81
- sh "git pull"
82
- end
83
- end
84
- end
85
-
86
70
  namespace :generate do
87
71
  desc "generate a fresh app with rspec installed"
88
- task :app => ["rails:clone"] do |t|
72
+ task :app do |t|
89
73
  unless File.directory?('./tmp/example_app')
90
- ruby "./vendor/rails/bin/rails new ./tmp/example_app"
91
- system "cp ./templates/Gemfile ./tmp/example_app/"
74
+ sh "bundle exec rails new ./tmp/example_app"
75
+ sh "cp ./templates/Gemfile ./tmp/example_app/"
92
76
  end
93
77
  end
94
78
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.beta.12
1
+ 2.0.0.beta.13
@@ -0,0 +1,64 @@
1
+ Feature: be_routable matcher
2
+
3
+ The be_routable matcher is intended for use with should_not to specify
4
+ that a given route should_not be_routable.
5
+
6
+ Scenario: specify routeable route should be routable (passes)
7
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
8
+ """
9
+ require "spec_helper"
10
+
11
+ describe WidgetsController do
12
+ it "routes to /widgets" do
13
+ { :get => "/widgets" }.should be_routable
14
+ end
15
+ end
16
+ """
17
+
18
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
19
+ Then I should see "1 example, 0 failures"
20
+
21
+ Scenario: specify routeable route should not be routable (fails)
22
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
23
+ """
24
+ require "spec_helper"
25
+
26
+ describe WidgetsController do
27
+ it "does not route to widgets" do
28
+ { :get => "/widgets" }.should_not be_routable
29
+ end
30
+ end
31
+ """
32
+
33
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
34
+ Then I should see "1 example, 1 failure"
35
+
36
+ Scenario: specify non-routeable route should not be routable (passes)
37
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
38
+ """
39
+ require "spec_helper"
40
+
41
+ describe WidgetsController do
42
+ it "does not route to widgets/foo/bar" do
43
+ { :get => "/widgets/foo/bar" }.should_not be_routable
44
+ end
45
+ end
46
+ """
47
+
48
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
49
+ Then I should see "1 example, 0 failures"
50
+
51
+ Scenario: specify non-routeable route should be routable (fails)
52
+ Given a file named "spec/routing/widgets_routing_spec.rb" with:
53
+ """
54
+ require "spec_helper"
55
+
56
+ describe WidgetsController do
57
+ it "routes to widgets/foo/bar" do
58
+ { :get => "/widgets/foo/bar" }.should be_routable
59
+ end
60
+ end
61
+ """
62
+
63
+ When I run "rspec spec/routing/widgets_routing_spec.rb"
64
+ Then I should see "1 example, 1 failure"
@@ -1,5 +1,22 @@
1
- require "aruba"
2
- require "rspec/expectations"
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'aruba'
5
+ require 'rspec/expectations'
6
+
7
+ module ArubaOverrides
8
+ def detect_ruby_script(cmd)
9
+ if cmd =~ /^rspec /
10
+ "bundle exec ../../../rspec-core/bin/#{cmd}"
11
+ elsif cmd =~ /^ruby /
12
+ "bundle exec #{cmd}"
13
+ else
14
+ super(cmd)
15
+ end
16
+ end
17
+ end
18
+
19
+ World(ArubaOverrides)
3
20
 
4
21
  unless File.directory?('./tmp/example_app')
5
22
  system "rake generate:app generate:stuff"
@@ -35,7 +35,7 @@ Autotest.add_hook :initialize do |at|
35
35
  at.add_mapping(%r%^(test|spec)/fixtures/(.*).yml$%) { |_, m|
36
36
  ["spec/models/#{m[2].singularize}_spec.rb"] + at.files_matching(%r%^spec\/views\/#{m[2]}/.*_spec\.rb$%)
37
37
  }
38
- at.add_mapping(%r%^spec/(models|controllers|routing|views|helpers|lib)/.*rb$%) { |filename, _|
38
+ at.add_mapping(%r%^spec/(models|controllers|routing|views|helpers|mailers|lib)/.*rb$%) { |filename, _|
39
39
  filename
40
40
  }
41
41
  at.add_mapping(%r%^app/models/(.*)\.rb$%) { |_, m|
@@ -70,6 +70,9 @@ Autotest.add_hook :initialize do |at|
70
70
  at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
71
71
  ["spec/lib/#{m[1]}_spec.rb"]
72
72
  }
73
+ at.add_mapping(%r%^app/mailers/(.*)\.rb$%) { |_, m|
74
+ ["spec/mailers/#{m[1]}_spec.rb"]
75
+ }
73
76
  end
74
77
 
75
78
  class Autotest::RailsRspec2 < Autotest::Rspec2
@@ -39,7 +39,7 @@ desc "Run all specs in spec directory (excluding plugin specs)"
39
39
  RSpec::Core::RakeTask.new(:spec => spec_prereq)
40
40
 
41
41
  namespace :spec do
42
- [:requests, :models, :controllers, :views, :helpers, :mailers, :lib].each do |sub|
42
+ [:requests, :models, :controllers, :views, :helpers, :mailers, :lib, :routing].each do |sub|
43
43
  desc "Run the code examples in spec/#{sub}"
44
44
  RSpec::Core::RakeTask.new(sub => spec_prereq) do |t|
45
45
  t.pattern = "./spec/#{sub}/**/*_spec.rb"
@@ -15,7 +15,7 @@ module Rspec
15
15
  class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
16
16
  class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
17
17
  class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
18
- class_option :routing_specs, :type => :boolean, :default => false, :desc => "Generate routing specs"
18
+ class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
19
19
 
20
20
  def copy_controller_files
21
21
  return unless options[:controller_specs]
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
4
  describe "<%= table_name %>/edit.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
- assign(:<%= file_name %>, @<%= file_name %> = stub_model(<%= class_name %>,
6
+ @<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %>,
7
7
  :new_record? => false<%= output_attributes.empty? ? '' : ',' %>
8
8
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
9
9
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe <%= controller_class_name %>Controller do
4
4
  describe "routing" do
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
4
  describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
- assign(:<%= file_name %>, @<%= file_name %> = stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
6
+ @<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
8
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
@@ -15,7 +15,7 @@ describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
15
15
  it "renders attributes in <p>" do
16
16
  render
17
17
  <% for attribute in output_attributes -%>
18
- rendered.should contain(<%= attribute.default.inspect %>)
18
+ rendered.should contain(<%= attribute.default.inspect %>)
19
19
  <% end -%>
20
20
  end
21
21
  end
@@ -1,7 +1,6 @@
1
1
  require 'rspec/core'
2
2
  require 'rspec/rails/monkey'
3
3
  require 'rspec/rails/extensions'
4
- require 'rspec/rails/null_resolver'
5
4
  require 'rspec/rails/view_rendering'
6
5
  require 'rspec/rails/adapters'
7
6
  require 'rspec/rails/matchers'
@@ -19,8 +19,8 @@ module RSpec
19
19
 
20
20
  module TestUnitAssertionAdapter
21
21
  extend ActiveSupport::Concern
22
- def method_name
23
- @running_example
22
+ def method_name
23
+ @example
24
24
  end
25
25
 
26
26
  include Test::Unit::Assertions
@@ -3,3 +3,4 @@ require 'rspec/rails/example/request_example_group'
3
3
  require 'rspec/rails/example/helper_example_group'
4
4
  require 'rspec/rails/example/view_example_group'
5
5
  require 'rspec/rails/example/mailer_example_group'
6
+ require 'rspec/rails/example/routing_example_group'
@@ -36,6 +36,7 @@ module RSpec::Rails
36
36
  # describe WidgetsController do
37
37
  # describe "GET index" do
38
38
  # fixtures :widgets
39
+ #
39
40
  # it "assigns all widgets to @widgets" do
40
41
  # get :index
41
42
  # assigns(:widgets).should eq(Widget.all)
@@ -82,7 +83,8 @@ module RSpec::Rails
82
83
  include Webrat::Matchers
83
84
  include Webrat::Methods
84
85
  include RSpec::Matchers
85
- include RSpec::Rails::ControllerSpecMatchers
86
+ include RSpec::Rails::Matchers::RedirectTo
87
+ include RSpec::Rails::Matchers::RenderTemplate
86
88
 
87
89
  module ClassMethods
88
90
  def controller_class
@@ -1,5 +1,6 @@
1
- require 'webrat'
1
+ require 'action_view'
2
2
  require 'rspec/rails/view_assigns'
3
+ require 'webrat'
3
4
 
4
5
  module RSpec::Rails
5
6
  # Extends ActionView::TestCase::Behavior
@@ -51,7 +52,7 @@ module RSpec::Rails
51
52
  private
52
53
 
53
54
  def _controller_path
54
- running_example.example_group.describes.to_s.sub(/Helper/,'').underscore
55
+ example.example_group.describes.to_s.sub(/Helper/,'').underscore
55
56
  end
56
57
  end
57
58
 
@@ -1,3 +1,4 @@
1
+ require 'action_mailer'
1
2
  require 'webrat'
2
3
 
3
4
  module RSpec::Rails
@@ -2,6 +2,18 @@ require 'action_dispatch'
2
2
  require 'webrat'
3
3
 
4
4
  module RSpec::Rails
5
+ # Extends ActionDispatch::Integration::Runner to work with RSpec.
6
+ #
7
+ # == Matchers
8
+ #
9
+ # In addition to the stock matchers from rspec-expectations, request
10
+ # specs add these matchers, which delegate to rails' assertions:
11
+ #
12
+ # response.should render_template(*args)
13
+ # => delegates to assert_template(*args)
14
+ #
15
+ # response.should redirect_to(destination)
16
+ # => delegates to assert_redirected_to(destination)
5
17
  module RequestExampleGroup
6
18
  extend ActiveSupport::Concern
7
19
 
@@ -11,6 +23,9 @@ module RSpec::Rails
11
23
  include Webrat::Matchers
12
24
  include Webrat::Methods
13
25
  include RSpec::Matchers
26
+ include RSpec::Rails::Matchers::RedirectTo
27
+ include RSpec::Rails::Matchers::RenderTemplate
28
+ include ActionController::TemplateAssertions
14
29
 
15
30
  module InstanceMethods
16
31
  def app
@@ -0,0 +1,25 @@
1
+ require "action_dispatch/testing/assertions/routing"
2
+
3
+ module RSpec::Rails
4
+ module RoutingExampleGroup
5
+ extend ActiveSupport::Concern
6
+
7
+ include RSpec::Rails::TestUnitAssertionAdapter
8
+ include ActionDispatch::Assertions::RoutingAssertions
9
+ include RSpec::Rails::RoutingSpecMatchers
10
+
11
+ module InstanceMethods
12
+ attr_reader :routes
13
+ end
14
+
15
+ included do
16
+ before do
17
+ @routes = ::Rails.application.routes
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |c|
22
+ c.include self, :example_group => { :file_path => /\bspec\/routing\// }
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,6 @@
1
- require 'webrat'
1
+ require 'action_view'
2
2
  require 'rspec/rails/view_assigns'
3
+ require 'webrat'
3
4
 
4
5
  module RSpec::Rails
5
6
  # Extends ActionView::TestCase::Behavior
@@ -26,13 +27,9 @@ module RSpec::Rails
26
27
  include ActionView::TestCase::Behavior
27
28
  include RSpec::Rails::ViewAssigns
28
29
  include Webrat::Matchers
30
+ include RSpec::Rails::Matchers::RenderTemplate
29
31
 
30
32
  module InstanceMethods
31
- def response
32
- RSpec.deprecate("response", "rendered")
33
- rendered
34
- end
35
-
36
33
  # :call-seq:
37
34
  # render
38
35
  # render(:template => "widgets/new.html.erb")
@@ -52,6 +49,12 @@ module RSpec::Rails
52
49
  # end
53
50
  # end
54
51
  def render(options={}, local_assigns={}, &block)
52
+ # TODO - this is a temporary hack to achieve behaviour that is in rails edge
53
+ # as of http://github.com/rails/rails/commit/0e0df4b0c5df7fdd1daa5653c255c4737f5526fc,
54
+ # but is not part of the rails-3.0.0.beta4 release. This line can be removed as
55
+ # soon as either rails 3 beta5 or rc is released.
56
+ _assigns.each { |key, value| view.instance_variable_set("@#{key}", value) }
57
+
55
58
  options = {:template => _default_file_to_render} if Hash === options and options.empty?
56
59
  super(options, local_assigns, &block)
57
60
  end
@@ -77,10 +80,17 @@ module RSpec::Rails
77
80
  view
78
81
  end
79
82
 
83
+
84
+ # Deprecated. Use +rendered+ instead.
85
+ def response
86
+ RSpec.deprecate("response", "rendered")
87
+ rendered
88
+ end
89
+
80
90
  private
81
91
 
82
92
  def _default_file_to_render
83
- running_example.example_group.top_level_description
93
+ example.example_group.top_level_description
84
94
  end
85
95
 
86
96
  def _controller_path
@@ -91,6 +101,10 @@ module RSpec::Rails
91
101
  included do
92
102
  before do
93
103
  controller.controller_path = _controller_path
104
+ # this won't be necessary if/when
105
+ # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4903
106
+ # is accepted and released
107
+ @request ||= controller.request
94
108
  end
95
109
  end
96
110