technicalpickles-shoulda 2.0.6 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/README.rdoc +61 -11
  2. data/Rakefile +7 -5
  3. data/lib/shoulda.rb +7 -15
  4. data/lib/shoulda/action_controller.rb +28 -0
  5. data/lib/shoulda/action_controller/helpers.rb +47 -0
  6. data/lib/shoulda/action_controller/macros.rb +277 -0
  7. data/lib/shoulda/action_controller/matchers.rb +37 -0
  8. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  9. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  10. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  11. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  14. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +83 -0
  15. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  16. data/lib/shoulda/action_mailer.rb +1 -1
  17. data/lib/shoulda/action_mailer/assertions.rb +32 -33
  18. data/lib/shoulda/action_view.rb +10 -0
  19. data/lib/shoulda/action_view/macros.rb +56 -0
  20. data/lib/shoulda/active_record.rb +6 -2
  21. data/lib/shoulda/active_record/assertions.rb +62 -81
  22. data/lib/shoulda/active_record/helpers.rb +40 -0
  23. data/lib/shoulda/active_record/macros.rb +514 -640
  24. data/lib/shoulda/active_record/matchers.rb +42 -0
  25. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  26. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  27. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  28. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  29. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  30. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  31. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  32. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
  33. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  34. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  35. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  36. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  37. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  38. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  39. data/lib/shoulda/assertions.rb +50 -40
  40. data/lib/shoulda/autoload_macros.rb +46 -0
  41. data/lib/shoulda/context.rb +124 -126
  42. data/lib/shoulda/helpers.rb +5 -7
  43. data/lib/shoulda/macros.rb +63 -64
  44. data/lib/shoulda/private_helpers.rb +16 -18
  45. data/lib/shoulda/rails.rb +5 -11
  46. data/lib/shoulda/rspec.rb +11 -0
  47. data/lib/shoulda/tasks/list_tests.rake +6 -1
  48. data/lib/shoulda/test_unit.rb +19 -0
  49. data/rails/init.rb +7 -1
  50. data/test/README +2 -2
  51. data/test/fail_macros.rb +16 -16
  52. data/test/functional/posts_controller_test.rb +33 -24
  53. data/test/functional/users_controller_test.rb +0 -19
  54. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  55. data/test/matchers/active_record/allow_value_matcher_test.rb +41 -0
  56. data/test/matchers/active_record/association_matcher_test.rb +258 -0
  57. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  58. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  59. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  60. data/test/matchers/active_record/have_index_matcher_test.rb +74 -0
  61. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  62. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  63. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  64. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  65. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  66. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  67. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  68. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  69. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  70. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
  71. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  72. data/test/matchers/controller/route_matcher_test.rb +58 -0
  73. data/test/matchers/controller/set_session_matcher_test.rb +27 -0
  74. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  75. data/test/model_builder.rb +106 -0
  76. data/test/other/autoload_macro_test.rb +18 -0
  77. data/test/other/helpers_test.rb +58 -0
  78. data/test/other/private_helpers_test.rb +1 -1
  79. data/test/other/should_test.rb +16 -16
  80. data/test/rails_root/app/controllers/posts_controller.rb +6 -5
  81. data/test/rails_root/app/models/pets/dog.rb +10 -0
  82. data/test/rails_root/app/models/treat.rb +3 -0
  83. data/test/rails_root/app/models/user.rb +4 -3
  84. data/test/rails_root/app/views/layouts/posts.rhtml +2 -0
  85. data/test/rails_root/config/database.yml +1 -1
  86. data/test/rails_root/config/environment.rb +1 -1
  87. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  88. data/test/rails_root/db/migrate/001_create_users.rb +3 -2
  89. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  90. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  91. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  92. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  93. data/test/rspec_test.rb +207 -0
  94. data/test/test_helper.rb +3 -1
  95. data/test/unit/address_test.rb +1 -1
  96. data/test/unit/dog_test.rb +5 -2
  97. data/test/unit/post_test.rb +7 -3
  98. data/test/unit/product_test.rb +2 -2
  99. data/test/unit/tag_test.rb +2 -1
  100. data/test/unit/user_test.rb +27 -9
  101. metadata +82 -13
  102. data/lib/shoulda/controller.rb +0 -30
  103. data/lib/shoulda/controller/formats/html.rb +0 -201
  104. data/lib/shoulda/controller/formats/xml.rb +0 -170
  105. data/lib/shoulda/controller/helpers.rb +0 -64
  106. data/lib/shoulda/controller/macros.rb +0 -316
  107. data/lib/shoulda/controller/resource_options.rb +0 -236
  108. data/test/rails_root/app/models/dog.rb +0 -5
data/README.rdoc CHANGED
@@ -2,19 +2,20 @@
2
2
 
3
3
  Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It's fully compatible with your existing tests, and requires no retooling to use.
4
4
 
5
- Helpers:: #context and #should give you rSpec like test blocks.
5
+ Helpers:: #context and #should give you RSpec like test blocks.
6
6
  In addition, you get nested contexts and a much more readable syntax.
7
7
  Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
8
8
  They get you started quickly, and can help you ensure that your application is conforming to best practices.
9
9
  Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
10
+ Matchers:: Rspec-compatible matchers providing the same tests as Shoulda macros.
10
11
 
11
12
  = Usage
12
13
 
13
- === Context Helpers (ThoughtBot::Shoulda::Context)
14
+ === Context Helpers (Shoulda::Context)
14
15
 
15
16
  Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
16
17
 
17
- class UserTest << Test::Unit::TestCase
18
+ class UserTest < Test::Unit::TestCase
18
19
  context "A User instance" do
19
20
  setup do
20
21
  @user = User.find(:first)
@@ -43,7 +44,7 @@ Produces the following test methods:
43
44
 
44
45
  So readable!
45
46
 
46
- === ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord::Macros)
47
+ === ActiveRecord Tests (Shoulda::ActiveRecord::Macros)
47
48
 
48
49
  Quick macro tests for your ActiveRecord associations and validations:
49
50
 
@@ -53,10 +54,10 @@ Quick macro tests for your ActiveRecord associations and validations:
53
54
  should_belong_to :user
54
55
  should_have_many :tags, :through => :taggings
55
56
 
56
- should_require_unique_attributes :title
57
- should_require_attributes :body, :message => /wtf/
58
- should_require_attributes :title
59
- should_only_allow_numeric_values_for :user_id
57
+ should_validate_uniqueness_of :title
58
+ should_validate_presence_of :body, :message => /wtf/
59
+ should_validate_presence_of :title
60
+ should_validate_numericality_of :user_id
60
61
  end
61
62
 
62
63
  class UserTest < Test::Unit::TestCase
@@ -66,12 +67,12 @@ Quick macro tests for your ActiveRecord associations and validations:
66
67
  should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
67
68
  should_ensure_length_in_range :email, 1..100
68
69
  should_ensure_value_in_range :age, 1..100
69
- should_protect_attributes :password
70
+ should_not_allow_mass_assignment_of :password
70
71
  end
71
72
 
72
73
  Makes TDD so much easier.
73
74
 
74
- === Controller Tests (ThoughtBot::Shoulda::Controller::Macros)
75
+ === Controller Tests (Shoulda::Controller::Macros)
75
76
 
76
77
  Macros to test the most common controller patterns...
77
78
 
@@ -90,7 +91,7 @@ Macros to test the most common controller patterns...
90
91
  end
91
92
  end
92
93
 
93
- === Helpful Assertions (ThoughtBot::Shoulda::Assertions)
94
+ === Helpful Assertions (Shoulda::Assertions)
94
95
 
95
96
  More to come here, but have fun with what's there.
96
97
 
@@ -110,6 +111,55 @@ Any *.rb file under RAILS_ROOT/test/shoulda_macros/ or vendor/(plugins|gems)/gem
110
111
  end
111
112
  end
112
113
 
114
+ = Rails Installation (Test::Unit)
115
+
116
+ === As a Gem
117
+
118
+ Use this if you prefer to use versioned releases of shoulda. Specify the gem dependency in your config/environment.rb file:
119
+
120
+ Rails::Initializer.run do |config|
121
+ config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"
122
+ end
123
+
124
+ Then:
125
+
126
+ $ rake gems:install
127
+ $ rake gems:unpack
128
+
129
+ === As a Plugin
130
+
131
+ Use this if you prefer to use the edge version of shoulda:
132
+
133
+ $ script/plugin install git://github.com/thoughtbot/shoulda.git
134
+
135
+ === As a Plugin (using git submodules)
136
+
137
+ Use this if you prefer the idea of being able to easily switch between using edge or a tagged version of shoulda:
138
+
139
+ $ git submodule add git://github.com/thoughtbot/shoulda.git vendor/plugins/shoulda
140
+
141
+ = Rails Installation (RSpec)
142
+
143
+ If you're using Shoulda with RSpec, we recommend that you add config.gem lines
144
+ for RSpec and Shoulda in your config/environment/test.rb file, but do not ask
145
+ Rails to load the RSpec and Shoulda libraries:
146
+
147
+ config.gem 'rspec', :lib => false
148
+ config.gem 'rspec-rails', :lib => false
149
+ config.gem 'thoughtbot-shoulda',
150
+ :lib => false,
151
+ :source => 'http://gems.github.com'
152
+
153
+ Then require shoulda from your spec/spec_helper.rb file, before Spec::Runner is
154
+ configured:
155
+
156
+ # requires for RSpec
157
+ require 'shoulda'
158
+ Spec::Runner.configure do |config|
159
+ # ...
160
+
161
+ You should not need to require anything besides the top-level shoulda library.
162
+
113
163
  = Credits
114
164
 
115
165
  Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
data/Rakefile CHANGED
@@ -2,11 +2,13 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
  require 'rake/gempackagetask'
5
- require 'lib/shoulda/context'
5
+
6
+ $LOAD_PATH.unshift("lib")
7
+ require 'shoulda'
6
8
  load 'tasks/shoulda.rake'
7
9
 
8
10
  # Test::Unit::UI::VERBOSE
9
- test_files_pattern = 'test/{unit,functional,other}/**/*_test.rb'
11
+ test_files_pattern = 'test/{unit,functional,other,matchers}/**/*_test.rb'
10
12
  Rake::TestTask.new do |t|
11
13
  t.libs << 'lib'
12
14
  t.pattern = test_files_pattern
@@ -16,7 +18,7 @@ end
16
18
  Rake::RDocTask.new { |rdoc|
17
19
  rdoc.rdoc_dir = 'doc'
18
20
  rdoc.title = "Shoulda -- Making tests easy on the fingers and eyes"
19
- rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.options << '--line-numbers'
20
22
  rdoc.template = "#{ENV['template']}.rb" if ENV['template']
21
23
  rdoc.rdoc_files.include('README.rdoc', 'CONTRIBUTION_GUIDELINES.rdoc', 'lib/**/*.rb')
22
24
  }
@@ -38,7 +40,7 @@ task :default => ['test']
38
40
 
39
41
  spec = Gem::Specification.new do |s|
40
42
  s.name = "shoulda"
41
- s.version = Thoughtbot::Shoulda::VERSION
43
+ s.version = Shoulda::VERSION
42
44
  s.summary = "Making tests easy on the fingers and eyes"
43
45
  s.homepage = "http://thoughtbot.com/projects/shoulda"
44
46
  s.rubyforge_project = "shoulda"
@@ -48,7 +50,7 @@ spec = Gem::Specification.new do |s|
48
50
 
49
51
  s.has_rdoc = true
50
52
  s.extra_rdoc_files = ["README.rdoc", "CONTRIBUTION_GUIDELINES.rdoc"]
51
- s.rdoc_options = ["--line-numbers", "--inline-source", "--main", "README.rdoc"]
53
+ s.rdoc_options = ["--line-numbers", "--main", "README.rdoc"]
52
54
 
53
55
  s.authors = ["Tammer Saleh"]
54
56
  s.email = "tsaleh@thoughtbot.com"
data/lib/shoulda.rb CHANGED
@@ -1,17 +1,9 @@
1
- require 'shoulda/context'
2
- require 'shoulda/proc_extensions'
3
- require 'shoulda/assertions'
4
- require 'shoulda/macros'
5
- require 'shoulda/helpers'
6
- require 'shoulda/rails' if defined? RAILS_ROOT
1
+ module Shoulda
2
+ VERSION = "2.10.0"
3
+ end
7
4
 
8
- module Test # :nodoc: all
9
- module Unit
10
- class TestCase
11
- extend Thoughtbot::Shoulda
12
- include ThoughtBot::Shoulda::Assertions
13
- extend ThoughtBot::Shoulda::Macros
14
- include ThoughtBot::Shoulda::Helpers
15
- end
16
- end
5
+ if defined? Spec
6
+ require 'shoulda/rspec'
7
+ else
8
+ require 'shoulda/test_unit'
17
9
  end
@@ -0,0 +1,28 @@
1
+ require 'shoulda'
2
+ require 'shoulda/action_controller/helpers'
3
+ require 'shoulda/action_controller/matchers'
4
+ require 'shoulda/action_controller/macros'
5
+
6
+ module Test # :nodoc: all
7
+ module Unit
8
+ class TestCase
9
+ include Shoulda::ActionController::Matchers
10
+ include Shoulda::ActionController::Helpers
11
+ extend Shoulda::ActionController::Macros
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'shoulda/active_record/assertions'
17
+ require 'shoulda/action_mailer/assertions'
18
+
19
+ module ActionController #:nodoc: all
20
+ module Integration
21
+ class Session
22
+ include Shoulda::Assertions
23
+ include Shoulda::Helpers
24
+ include Shoulda::ActiveRecord::Assertions
25
+ include Shoulda::ActionMailer::Assertions
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ module Shoulda # :nodoc:
2
+ module ActionController # :nodoc:
3
+ module Helpers # :nodoc:
4
+ private # :enddoc:
5
+
6
+ SPECIAL_INSTANCE_VARIABLES = %w{
7
+ _cookies
8
+ _flash
9
+ _headers
10
+ _params
11
+ _request
12
+ _response
13
+ _session
14
+ action_name
15
+ before_filter_chain_aborted
16
+ cookies
17
+ flash
18
+ headers
19
+ ignore_missing_templates
20
+ logger
21
+ params
22
+ request
23
+ request_origin
24
+ response
25
+ session
26
+ template
27
+ template_class
28
+ template_root
29
+ url
30
+ variables_added
31
+ }.map(&:to_s)
32
+
33
+ def instantiate_variables_from_assigns(*names, &blk)
34
+ old = {}
35
+ names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
36
+ names.each do |name|
37
+ old[name] = instance_variable_get("@#{name}")
38
+ instance_variable_set("@#{name}", assigns(name.to_sym))
39
+ end
40
+ blk.call
41
+ names.each do |name|
42
+ instance_variable_set("@#{name}", old[name])
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,277 @@
1
+ module Shoulda # :nodoc:
2
+ module ActionController # :nodoc:
3
+ # = Macro test helpers for your controllers
4
+ #
5
+ # By using the macro helpers you can quickly and easily create concise and easy to read test suites.
6
+ #
7
+ # This code segment:
8
+ # context "on GET to :show for first record" do
9
+ # setup do
10
+ # get :show, :id => 1
11
+ # end
12
+ #
13
+ # should_assign_to :user
14
+ # should_respond_with :success
15
+ # should_render_template :show
16
+ # should_not_set_the_flash
17
+ #
18
+ # should "do something else really cool" do
19
+ # assert_equal 1, assigns(:user).id
20
+ # end
21
+ # end
22
+ #
23
+ # Would produce 5 tests for the +show+ action
24
+ module Macros
25
+ include Matchers
26
+
27
+ # Macro that creates a test asserting that the flash contains the given value.
28
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
29
+ #
30
+ # Example:
31
+ #
32
+ # should_set_the_flash_to "Thank you for placing this order."
33
+ # should_set_the_flash_to /created/i
34
+ # should_set_the_flash_to nil
35
+ def should_set_the_flash_to(val)
36
+ matcher = set_the_flash.to(val)
37
+ if val
38
+ should matcher.description do
39
+ assert_accepts matcher, @controller
40
+ end
41
+ else
42
+ should "not #{matcher.description}" do
43
+ assert_rejects matcher, @controller
44
+ end
45
+ end
46
+ end
47
+
48
+ # Macro that creates a test asserting that the flash is empty. Same as
49
+ # @should_set_the_flash_to nil@
50
+ def should_not_set_the_flash
51
+ should_set_the_flash_to nil
52
+ end
53
+
54
+ # Macro that creates a test asserting that filter_parameter_logging
55
+ # is set for the specified keys
56
+ #
57
+ # Example:
58
+ #
59
+ # should_filter_params :password, :ssn
60
+ def should_filter_params(*keys)
61
+ keys.each do |key|
62
+ matcher = filter_param(key)
63
+ should matcher.description do
64
+ assert_accepts matcher, @controller
65
+ end
66
+ end
67
+ end
68
+
69
+ # Macro that creates a test asserting that the controller assigned to
70
+ # each of the named instance variable(s).
71
+ #
72
+ # Options:
73
+ # * <tt>:class</tt> - The expected class of the instance variable being checked.
74
+ # * <tt>:equals</tt> - A string which is evaluated and compared for equality with
75
+ # the instance variable being checked.
76
+ #
77
+ # Example:
78
+ #
79
+ # should_assign_to :user, :posts
80
+ # should_assign_to :user, :class => User
81
+ # should_assign_to(:user) { @user }
82
+ def should_assign_to(*names, &block)
83
+ opts = names.extract_options!
84
+ if opts[:equals]
85
+ warn "[DEPRECATION] should_assign_to :var, :equals => 'val' " <<
86
+ "is deprecated. Use should_assign_to(:var) { 'val' } instead."
87
+ end
88
+ names.each do |name|
89
+ matcher = assign_to(name).with_kind_of(opts[:class])
90
+ test_name = matcher.description
91
+ test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
92
+ should test_name do
93
+ if opts[:equals]
94
+ instantiate_variables_from_assigns do
95
+ expected_value = eval(opts[:equals],
96
+ self.send(:binding),
97
+ __FILE__,
98
+ __LINE__)
99
+ matcher = matcher.with(expected_value)
100
+ end
101
+ elsif block
102
+ expected_value = instance_eval(&block)
103
+ matcher = matcher.with(expected_value)
104
+ end
105
+
106
+ assert_accepts matcher, @controller
107
+ end
108
+ end
109
+ end
110
+
111
+ # Macro that creates a test asserting that the controller did not assign to
112
+ # any of the named instance variable(s).
113
+ #
114
+ # Example:
115
+ #
116
+ # should_not_assign_to :user, :posts
117
+ def should_not_assign_to(*names)
118
+ names.each do |name|
119
+ matcher = assign_to(name)
120
+ should "not #{matcher.description}" do
121
+ assert_rejects matcher, @controller
122
+ end
123
+ end
124
+ end
125
+
126
+ # Macro that creates a test asserting that the controller responded with a 'response' status code.
127
+ # Example:
128
+ #
129
+ # should_respond_with :success
130
+ def should_respond_with(response)
131
+ should "respond with #{response}" do
132
+ matcher = respond_with(response)
133
+ assert_accepts matcher, @controller
134
+ end
135
+ end
136
+
137
+ # Macro that creates a test asserting that the response content type was 'content_type'.
138
+ # Example:
139
+ #
140
+ # should_respond_with_content_type 'application/rss+xml'
141
+ # should_respond_with_content_type :rss
142
+ # should_respond_with_content_type /rss/
143
+ def should_respond_with_content_type(content_type)
144
+ should "respond with content type of #{content_type}" do
145
+ matcher = respond_with_content_type(content_type)
146
+ assert_accepts matcher, @controller
147
+ end
148
+ end
149
+
150
+ # Macro that creates a test asserting that a value returned from the session is correct.
151
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
152
+ # set by the controller are available to the evaled string.
153
+ # Example:
154
+ #
155
+ # should_set_session(:user_id) { '@user.id' }
156
+ # should_set_session(:message) { "Free stuff" }
157
+ def should_set_session(key, expected = nil, &block)
158
+ matcher = set_session(key)
159
+ if expected
160
+ warn "[DEPRECATION] should_set_session :key, 'val' is deprecated. " <<
161
+ "Use should_set_session(:key) { 'val' } instead."
162
+ end
163
+ should matcher.description do
164
+ if expected
165
+ instantiate_variables_from_assigns do
166
+ expected_value = eval(expected,
167
+ self.send(:binding),
168
+ __FILE__,
169
+ __LINE__)
170
+ matcher = matcher.to(expected_value)
171
+ end
172
+ else
173
+ expected_value = instance_eval(&block)
174
+ matcher = matcher.to(expected_value)
175
+ end
176
+ assert_accepts matcher, @controller
177
+ end
178
+ end
179
+
180
+ # Deprecated. See should_set_session
181
+ def should_return_from_session(key, expected)
182
+ warn "[DEPRECATION] should_require_attributes is deprecated. " <<
183
+ "Use should_set_session instead."
184
+ should_set_session(key, expected)
185
+ end
186
+
187
+ # Macro that creates a test asserting that the controller rendered the given template.
188
+ # Example:
189
+ #
190
+ # should_render_template :new
191
+ def should_render_template(template)
192
+ should "render template #{template.inspect}" do
193
+ assert_template template.to_s
194
+ end
195
+ end
196
+
197
+ # Macro that creates a test asserting that the controller rendered with the given layout.
198
+ # Example:
199
+ #
200
+ # should_render_with_layout 'special'
201
+ def should_render_with_layout(expected_layout = 'application')
202
+ matcher = render_with_layout(expected_layout)
203
+ if expected_layout
204
+ should matcher.description do
205
+ assert_accepts matcher, @controller
206
+ end
207
+ else
208
+ should "render without layout" do
209
+ assert_rejects matcher, @controller
210
+ end
211
+ end
212
+ end
213
+
214
+ # Macro that creates a test asserting that the controller rendered without a layout.
215
+ # Same as @should_render_with_layout false@
216
+ def should_render_without_layout
217
+ should_render_with_layout nil
218
+ end
219
+
220
+ # Macro that creates a test asserting that the controller returned a redirect to the given path.
221
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
222
+ # set by the controller are available to the evaled string.
223
+ # Example:
224
+ #
225
+ # should_redirect_to("the user's profile") { user_url(@user) }
226
+ def should_redirect_to(description, &block)
227
+ unless block
228
+ warn "[DEPRECATION] should_redirect_to without a block is " <<
229
+ "deprecated. Use should_redirect_to('somewhere') { } instead."
230
+ end
231
+ should "redirect to #{description}" do
232
+ if block
233
+ url = instance_eval(&block)
234
+ else
235
+ instantiate_variables_from_assigns do
236
+ url = eval(description, self.send(:binding), __FILE__, __LINE__)
237
+ end
238
+ end
239
+ assert_redirected_to url
240
+ end
241
+ end
242
+
243
+ # Macro that creates a routing test. It tries to use the given HTTP
244
+ # +method+ on the given +path+, and asserts that it routes to the
245
+ # given +options+.
246
+ #
247
+ # If you don't specify a :controller, it will try to guess the controller
248
+ # based on the current test.
249
+ #
250
+ # +to_param+ is called on the +options+ given.
251
+ #
252
+ # Examples:
253
+ #
254
+ # should_route :get, "/posts", :controller => :posts, :action => :index
255
+ # should_route :get, "/posts/new", :action => :new
256
+ # should_route :post, "/posts", :action => :create
257
+ # should_route :get, "/posts/1", :action => :show, :id => 1
258
+ # should_route :edit, "/posts/1", :action => :show, :id => 1
259
+ # should_route :put, "/posts/1", :action => :update, :id => 1
260
+ # should_route :delete, "/posts/1", :action => :destroy, :id => 1
261
+ # should_route :get, "/users/1/posts/1",
262
+ # :action => :show, :id => 1, :user_id => 1
263
+ #
264
+ def should_route(method, path, options)
265
+ unless options[:controller]
266
+ options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
267
+ end
268
+
269
+ matcher = route(method, path).to(options)
270
+
271
+ should matcher.description do
272
+ assert_accepts matcher.in_context(self), self
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end