technicalpickles-shoulda 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ module ThoughtBot # :nodoc:
3
3
  module ActiveRecord # :nodoc:
4
4
  DEFAULT_ERROR_MESSAGES =
5
5
  if Object.const_defined?(:I18n)
6
- I18n.translate('active_record.error_messages')
6
+ I18n.translate('activerecord.errors.messages')
7
7
  else
8
8
  ::ActiveRecord::Errors.default_error_messages
9
9
  end
@@ -28,8 +28,9 @@ module ThoughtBot # :nodoc:
28
28
  # For all of these helpers, the last parameter may be a hash of options.
29
29
  #
30
30
  module Macros
31
+ # <b>DEPRECATED:</b> Use <tt>fixtures :all</tt> instead
32
+ #
31
33
  # Loads all fixture files (<tt>test/fixtures/*.yml</tt>)
32
- # Deprecated: Use <tt>fixtures :all</tt> instead
33
34
  def load_all_fixtures
34
35
  warn "[DEPRECATION] load_all_fixtures is deprecated. Use `fixtures :all` instead."
35
36
  fixtures :all
@@ -43,7 +44,7 @@ module ThoughtBot # :nodoc:
43
44
  #
44
45
  # Options:
45
46
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
46
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:blank]</tt>
47
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:blank]</tt>
47
48
  #
48
49
  # Example:
49
50
  # should_require_attributes :name, :phone_number
@@ -65,7 +66,7 @@ module ThoughtBot # :nodoc:
65
66
  #
66
67
  # Options:
67
68
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
68
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:taken]</tt>
69
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:taken]</tt>
69
70
  # * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
70
71
  #
71
72
  # Examples:
@@ -163,7 +164,7 @@ module ThoughtBot # :nodoc:
163
164
  #
164
165
  # Options:
165
166
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
166
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:invalid]</tt>
167
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:invalid]</tt>
167
168
  #
168
169
  # Example:
169
170
  # should_not_allow_values_for :isbn, "bad 1", "bad 2"
@@ -206,9 +207,9 @@ module ThoughtBot # :nodoc:
206
207
  #
207
208
  # Options:
208
209
  # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
209
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_short] % range.first</tt>
210
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_short] % range.first</tt>
210
211
  # * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
211
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_long] % range.last</tt>
212
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_long] % range.last</tt>
212
213
  #
213
214
  # Example:
214
215
  # should_ensure_length_in_range :password, (6..20)
@@ -258,7 +259,7 @@ module ThoughtBot # :nodoc:
258
259
  #
259
260
  # Options:
260
261
  # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
261
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_short] % min_length</tt>
262
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:too_short] % min_length</tt>
262
263
  #
263
264
  # Example:
264
265
  # should_ensure_length_at_least :name, 3
@@ -289,7 +290,7 @@ module ThoughtBot # :nodoc:
289
290
  #
290
291
  # Options:
291
292
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
292
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:wrong_length] % length</tt>
293
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:wrong_length] % length</tt>
293
294
  #
294
295
  # Example:
295
296
  # should_ensure_length_is :ssn, 9
@@ -324,9 +325,9 @@ module ThoughtBot # :nodoc:
324
325
  #
325
326
  # Options:
326
327
  # * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
327
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:inclusion]</tt>
328
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:inclusion]</tt>
328
329
  # * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
329
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:inclusion]</tt>
330
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:inclusion]</tt>
330
331
  #
331
332
  # Example:
332
333
  # should_ensure_value_in_range :age, (0..100)
@@ -369,7 +370,7 @@ module ThoughtBot # :nodoc:
369
370
  #
370
371
  # Options:
371
372
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
372
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:not_a_number]</tt>
373
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:not_a_number]</tt>
373
374
  #
374
375
  # Example:
375
376
  # should_only_allow_numeric_values_for :age
@@ -602,7 +603,7 @@ module ThoughtBot # :nodoc:
602
603
  # should_have_index :age
603
604
  #
604
605
  def should_have_indices(*columns)
605
- table = model_class.name.tableize
606
+ table = model_class.table_name
606
607
  indices = ::ActiveRecord::Base.connection.indexes(table).map(&:columns)
607
608
 
608
609
  columns.each do |column|
@@ -623,7 +624,7 @@ module ThoughtBot # :nodoc:
623
624
  #
624
625
  # Options:
625
626
  # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
626
- # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:accepted]</tt>
627
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages')[:accepted]</tt>
627
628
  #
628
629
  # Example:
629
630
  # should_require_acceptance_of :eula
@@ -1,6 +1,6 @@
1
1
  module Thoughtbot # :nodoc:
2
2
  module Shoulda
3
- VERSION = '2.0.1'
3
+ VERSION = '2.0.2'
4
4
 
5
5
  class << self
6
6
  attr_accessor :contexts
@@ -73,12 +73,12 @@ module Thoughtbot # :nodoc:
73
73
 
74
74
  # == Before statements
75
75
  #
76
- # Before statements are simply should statements that run after the current
76
+ # Before statements are should statements that run before the current
77
77
  # context's setup. These are especially useful when setting expectations.
78
78
  #
79
79
  # === Example:
80
80
  #
81
- # class UserControllerTest << Test::Unit::TestCase
81
+ # class UserControllerTest < Test::Unit::TestCase
82
82
  # context "the index action" do
83
83
  # setup do
84
84
  # @users = [Factory(:user)]
@@ -86,14 +86,11 @@ module Thoughtbot # :nodoc:
86
86
  # end
87
87
  #
88
88
  # context "on GET" do
89
- # setup do
90
- # get :index
91
- # end
89
+ # setup { get :index }
92
90
  #
93
- # # normal should statement
94
91
  # should_respond_with :success
95
92
  #
96
- # # runs before get :index
93
+ # # runs before "get :index"
97
94
  # before_should "find all users" do
98
95
  # User.expects(:find).with(:all).returns(@users)
99
96
  # end
@@ -26,7 +26,10 @@ module ThoughtBot # :nodoc:
26
26
  # Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your
27
27
  # controller responds restfully to a variety of requested formats.
28
28
  module Macros
29
- # :section: should_be_restful
29
+ # <b>DEPRECATED:</b> Please see
30
+ # http://thoughtbot.lighthouseapp.com/projects/5807/tickets/78 for more
31
+ # information.
32
+ #
30
33
  # Generates a full suite of tests for a restful controller.
31
34
  #
32
35
  # The following definition will generate tests for the +index+, +show+, +new+,
@@ -52,6 +55,8 @@ module ThoughtBot # :nodoc:
52
55
  # is used to configure the tests for the details of your resources.
53
56
  #
54
57
  def should_be_restful(&blk) # :yields: resource
58
+ warn "[DEPRECATION] should_be_restful is deprecated. Please see http://thoughtbot.lighthouseapp.com/projects/5807/tickets/78 for more information."
59
+
55
60
  resource = ResourceOptions.new
56
61
  blk.call(resource)
57
62
  resource.normalize!(self)
@@ -95,17 +100,56 @@ module ThoughtBot # :nodoc:
95
100
  def should_not_set_the_flash
96
101
  should_set_the_flash_to nil
97
102
  end
103
+
104
+ # Macro that creates a test asserting that filter_parameter_logging
105
+ # is set for the specified keys
106
+ #
107
+ # Example:
108
+ #
109
+ # should_filter_params :password, :ssn
110
+ def should_filter_params(*keys)
111
+ keys.each do |key|
112
+ should "filter #{key}" do
113
+ assert @controller.respond_to?(:filter_parameters),
114
+ "The key #{key} is not filtered"
115
+ filtered = @controller.send(:filter_parameters, {key.to_s => key.to_s})
116
+ assert_equal '[FILTERED]', filtered[key.to_s],
117
+ "The key #{key} is not filtered"
118
+ end
119
+ end
120
+ end
98
121
 
99
122
  # Macro that creates a test asserting that the controller assigned to
100
123
  # each of the named instance variable(s).
101
124
  #
125
+ # Options:
126
+ # * <tt>:class</tt> - The expected class of the instance variable being checked.
127
+ # * <tt>:equals</tt> - A string which is evaluated and compared for equality with
128
+ # the instance variable being checked.
129
+ #
102
130
  # Example:
103
131
  #
104
132
  # should_assign_to :user, :posts
133
+ # should_assign_to :user, :class => User
134
+ # should_assign_to :user, :equals => '@user'
105
135
  def should_assign_to(*names)
136
+ opts = names.extract_options!
106
137
  names.each do |name|
107
- should "assign @#{name}" do
108
- assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
138
+ test_name = "assign @#{name}"
139
+ test_name << " as class #{opts[:class]}" if opts[:class]
140
+ test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
141
+ should test_name do
142
+ assigned_value = assigns(name.to_sym)
143
+ assert assigned_value, "The action isn't assigning to @#{name}"
144
+ assert_kind_of opts[:class], assigned_value if opts[:class]
145
+ if opts[:equals]
146
+ instantiate_variables_from_assigns do
147
+ expected_value = eval(opts[:equals], self.send(:binding), __FILE__, __LINE__)
148
+ assert_equal expected_value, assigned_value,
149
+ "Instance variable @#{name} expected to be #{expected_value}" +
150
+ " but was #{assigned_value}"
151
+ end
152
+ end
109
153
  end
110
154
  end
111
155
  end
@@ -138,6 +182,8 @@ module ThoughtBot # :nodoc:
138
182
  # Example:
139
183
  #
140
184
  # should_respond_with_content_type 'application/rss+xml'
185
+ # should_respond_with_content_type :rss
186
+ # should_respond_with_content_type /rss/
141
187
  def should_respond_with_content_type(content_type)
142
188
  should "respond with content type of #{content_type}" do
143
189
  content_type = Mime::EXTENSION_LOOKUP[content_type.to_s].to_s if content_type.is_a? Symbol
@@ -183,13 +229,13 @@ module ThoughtBot # :nodoc:
183
229
  if expected_layout
184
230
  should "render with #{expected_layout} layout" do
185
231
  response_layout = @response.layout.blank? ? "" : @response.layout.split('/').last
186
- assert_equal expected_layout,
187
- response_layout,
232
+ assert_equal expected_layout,
233
+ response_layout,
188
234
  "Expected to render with layout #{expected_layout} but was rendered with #{response_layout}"
189
235
  end
190
236
  else
191
237
  should "render without layout" do
192
- assert_nil @response.layout,
238
+ assert_nil @response.layout,
193
239
  "Expected no layout, but was rendered using #{@response.layout}"
194
240
  end
195
241
  end
@@ -222,7 +268,7 @@ module ThoughtBot # :nodoc:
222
268
  assert_select "form", true, "The template doesn't contain a <form> element"
223
269
  end
224
270
  end
225
-
271
+
226
272
  # Macro that creates a routing test. It tries to use the given HTTP
227
273
  # +method+ on the given +path+, and asserts that it routes to the
228
274
  # given +options+.
@@ -234,13 +280,16 @@ module ThoughtBot # :nodoc:
234
280
  #
235
281
  # Examples:
236
282
  #
237
- # should_route :get, '/posts', :action => :index
238
- # should_route :post, '/posts', :controller => :posts, :action => :create
239
- # should_route :get, '/posts/1', :action => :show, :id => 1
240
- # should_route :put, '/posts/1', :action => :update, :id => "1"
241
- # should_route :delete, '/posts/1', :action => :destroy, :id => 1
242
- # should_route :get, '/posts/new', :action => :new
243
- #
283
+ # should_route :get, "/posts", :controller => :posts, :action => :index
284
+ # should_route :get, "/posts/new", :action => :new
285
+ # should_route :post, "/posts", :action => :create
286
+ # should_route :get, "/posts/1", :action => :show, :id => 1
287
+ # should_route :edit, "/posts/1", :action => :show, :id => 1
288
+ # should_route :put, "/posts/1", :action => :update, :id => 1
289
+ # should_route :delete, "/posts/1", :action => :destroy, :id => 1
290
+ # should_route :get, "/users/1/posts/1",
291
+ # :action => :show, :id => 1, :user_id => 1
292
+ #
244
293
  def should_route(method, path, options)
245
294
  unless options[:controller]
246
295
  options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
@@ -12,10 +12,7 @@ module ThoughtBot # :nodoc:
12
12
  # Example:
13
13
  #
14
14
  # context "Creating a post"
15
- # setup do
16
- # Post.create
17
- # end
18
- #
15
+ # setup { Post.create }
19
16
  # should_change "Post.count", :by => 1
20
17
  # end
21
18
  #
@@ -57,10 +54,7 @@ module ThoughtBot # :nodoc:
57
54
  # Example:
58
55
  #
59
56
  # context "Updating a post"
60
- # setup do
61
- # @post.update_attributes(:title => "new")
62
- # end
63
- #
57
+ # setup { @post.update_attributes(:title => "new") }
64
58
  # should_not_change "Post.count"
65
59
  # end
66
60
  def should_not_change(expression)
data/lib/shoulda.rb CHANGED
@@ -3,6 +3,7 @@ require 'shoulda/proc_extensions'
3
3
  require 'shoulda/assertions'
4
4
  require 'shoulda/macros'
5
5
  require 'shoulda/helpers'
6
+ require 'shoulda/rails' if defined? RAILS_ROOT
6
7
 
7
8
  module Test # :nodoc: all
8
9
  module Unit
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'shoulda/rails'
@@ -0,0 +1,34 @@
1
+ module Thoughtbot
2
+ module Shoulda
3
+ class << self
4
+ attr_accessor :expected_exceptions
5
+ end
6
+
7
+ # Enables the core shoulda test suite to test for failure scenarios. For
8
+ # example, to ensure that a set of test macros should fail, do this:
9
+ #
10
+ # should_fail do
11
+ # should_require_attributes :comments
12
+ # should_protect_attributes :name
13
+ # end
14
+ def should_fail(&block)
15
+ context "should fail when trying to run:" do
16
+ Shoulda.expected_exceptions = [Test::Unit::AssertionFailedError]
17
+ yield block
18
+ Shoulda.expected_exceptions = nil
19
+ end
20
+ end
21
+
22
+ class Context
23
+ # alias_method_chain hack to allow the should_fail macro to work
24
+ def should_with_failure_scenario(name, options = {}, &block)
25
+ if Shoulda.expected_exceptions
26
+ expected_exceptions = Shoulda.expected_exceptions
27
+ failure_block = lambda { assert_raise(*expected_exceptions, &block.bind(self)) }
28
+ end
29
+ should_without_failure_scenario(name, options, &(failure_block || block))
30
+ end
31
+ alias_method_chain :should, :failure_scenario
32
+ end
33
+ end
34
+ end
@@ -64,7 +64,14 @@ class PostsControllerTest < Test::Unit::TestCase
64
64
  get :index, :user_id => users(:first)
65
65
  end
66
66
  should_respond_with :success
67
- should_assign_to :user, :posts
67
+ should_assign_to :user, :class => User, :equals => 'users(:first)'
68
+ should_fail do
69
+ should_assign_to :user, :class => Post
70
+ end
71
+ should_fail do
72
+ should_assign_to :user, :equals => 'posts(:first)'
73
+ end
74
+ should_assign_to :posts
68
75
  should_not_assign_to :foo, :bar
69
76
  end
70
77
 
@@ -13,6 +13,8 @@ class UsersControllerTest < Test::Unit::TestCase
13
13
  @response = ActionController::TestResponse.new
14
14
  @user = User.find(:first)
15
15
  end
16
+
17
+ should_filter_params :ssn
16
18
 
17
19
  should_be_restful do |resource|
18
20
  resource.identifier = :id
@@ -1,4 +1,7 @@
1
1
  class UsersController < ApplicationController
2
+
3
+ filter_parameter_logging :ssn
4
+
2
5
  # GET /users
3
6
  # GET /users.xml
4
7
  def index
@@ -0,0 +1 @@
1
+ <html><%= yield %></html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: technicalpickles-shoulda
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tammer Saleh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-14 00:00:00 -07:00
12
+ date: 2008-09-20 00:00:00 -07:00
13
13
  default_executable: convert_to_should_syntax
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -53,9 +53,6 @@ files:
53
53
  - lib/shoulda/controller/helpers.rb
54
54
  - lib/shoulda/controller/macros.rb
55
55
  - lib/shoulda/controller/resource_options.rb
56
- - lib/shoulda/controller/routing
57
- - lib/shoulda/controller/routing/macros.rb
58
- - lib/shoulda/controller/routing.rb
59
56
  - lib/shoulda/controller.rb
60
57
  - lib/shoulda/helpers.rb
61
58
  - lib/shoulda/macros.rb
@@ -67,6 +64,9 @@ files:
67
64
  - lib/shoulda/tasks/yaml_to_shoulda.rake
68
65
  - lib/shoulda/tasks.rb
69
66
  - lib/shoulda.rb
67
+ - rails
68
+ - rails/init.rb
69
+ - test/fail_macros.rb
70
70
  - test/fixtures
71
71
  - test/fixtures/addresses.yml
72
72
  - test/fixtures/friendships.yml
@@ -108,6 +108,7 @@ files:
108
108
  - test/rails_root/app/views/layouts
109
109
  - test/rails_root/app/views/layouts/posts.rhtml
110
110
  - test/rails_root/app/views/layouts/users.rhtml
111
+ - test/rails_root/app/views/layouts/wide.html.erb
111
112
  - test/rails_root/app/views/posts
112
113
  - test/rails_root/app/views/posts/edit.rhtml
113
114
  - test/rails_root/app/views/posts/index.rhtml