shoulda 2.11.0 → 2.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +5 -5
  2. data/README.rdoc +72 -89
  3. data/Rakefile +30 -13
  4. data/lib/shoulda.rb +5 -3
  5. data/lib/shoulda/action_controller/matchers.rb +5 -5
  6. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +1 -1
  7. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +9 -9
  8. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +12 -12
  9. data/lib/shoulda/action_controller/matchers/route_matcher.rb +1 -1
  10. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +17 -26
  11. data/lib/shoulda/active_record/helpers.rb +2 -2
  12. data/lib/shoulda/active_record/macros.rb +2 -2
  13. data/lib/shoulda/active_record/matchers/association_matcher.rb +8 -8
  14. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +1 -1
  15. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +2 -2
  16. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +11 -11
  17. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +8 -8
  18. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +2 -4
  19. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +3 -3
  20. data/lib/shoulda/active_record/matchers/validation_matcher.rb +0 -1
  21. data/lib/shoulda/assertions.rb +2 -2
  22. data/lib/shoulda/autoload_macros.rb +20 -20
  23. data/lib/shoulda/context.rb +2 -2
  24. data/lib/shoulda/{rspec.rb → integrations/rspec.rb} +0 -0
  25. data/lib/shoulda/integrations/rspec2.rb +22 -0
  26. data/lib/shoulda/{test_unit.rb → integrations/test_unit.rb} +0 -0
  27. data/lib/shoulda/macros.rb +32 -2
  28. data/lib/shoulda/tasks/yaml_to_shoulda.rake +11 -11
  29. data/lib/shoulda/version.rb +1 -1
  30. data/rails/init.rb +2 -2
  31. data/test/matchers/action_mailer/have_sent_email_test.rb +27 -10
  32. data/test/other/context_test.rb +22 -22
  33. data/test/other/convert_to_should_syntax_test.rb +1 -1
  34. data/test/other/private_helpers_test.rb +1 -1
  35. data/test/other/should_test.rb +12 -12
  36. data/test/rails2_model_builder.rb +4 -4
  37. data/test/rails2_root/log/test.log +5852 -0
  38. data/test/rails3_model_builder.rb +4 -4
  39. data/test/rails3_root/Gemfile +1 -1
  40. data/test/rails3_root/db/test.sqlite3 +0 -0
  41. data/test/rails3_root/log/test.log +5056 -0
  42. data/test/unit/flea_test.rb +1 -1
  43. metadata +5 -4
@@ -1,10 +1,10 @@
1
- We're using GitHub[http://github.com/thoughtbot/shoulda/tree/master], and we've been getting any combination of github pull requests, tickets, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes.
1
+ We're using GitHub[http://github.com/thoughtbot/shoulda/tree/master], and we've been getting any combination of github pull requests, tickets, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes.
2
2
 
3
3
  * Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/shoulda/tree/master].
4
4
  * We prefer git branches over patches, but we can take either.
5
- * If you're using git, please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
5
+ * If you're using git, please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
6
6
  * If you're submitting patches, please cut each fix or feature into a separate patch.
7
- * There should be an issue[http://github.com/thoughtbot/shoulda/issues] for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
8
- * Please <b>don't send pull requests</b> Just update the issue with the url for your fix (or attach the patch) when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox.
9
- * Contributions without tests won't be accepted. The file <tt>/test/README</tt> explains the testing system pretty thoroughly.
7
+ * There should be an issue[http://github.com/thoughtbot/shoulda/issues] for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
8
+ * Please <b>don't send pull requests</b> Just update the issue with the url for your fix (or attach the patch) when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox.
9
+ * Contributions without tests won't be accepted. The file <tt>/test/README</tt> explains the testing system pretty thoroughly.
10
10
 
data/README.rdoc CHANGED
@@ -1,16 +1,64 @@
1
1
  = Shoulda - Making tests easy on the fingers and eyes
2
2
 
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.
3
+ Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of matchers, test helpers, and assertions. It's fully compatible with your existing tests in Test::Unit or RSpec, and requires no retooling to use.
4
4
 
5
- Helpers:: #context and #should give you RSpec like test blocks.
5
+ Matchers:: Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.
6
+ These tests would otherwise be much longer, more complex, and error-prone.
7
+ Helpers:: #context and #should give you RSpec like test blocks in Test::Unit.
6
8
  In addition, you get nested contexts and a much more readable syntax.
7
- Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
8
- They get you started quickly, and can help you ensure that your application is conforming to best practices.
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.
9
+ Assertions:: Many common Rails testing idioms have been distilled into a set of useful assertions.
11
10
 
12
11
  = Usage
13
12
 
13
+ === ActiveRecord Tests (Shoulda::ActiveRecord::Matchers)
14
+
15
+ Test your ActiveRecord associations and validations with these powerful matchers:
16
+
17
+ class PostTest < Test::Unit::TestCase
18
+ should belong_to(:user)
19
+ should have_many(:tags).through(:taggings)
20
+
21
+ should validate_uniqueness_of(:title)
22
+ should validate_presence_of(:body).with_message(/wtf/)
23
+ should validate_presence_of(:title)
24
+ should validate_numericality_of(:user_id)
25
+ end
26
+
27
+ class UserTest < Test::Unit::TestCase
28
+ should have_many(:posts)
29
+
30
+ should_not allow_value("blah").for(:email)
31
+ should_not allow_value("b lah").for(:email)
32
+ should allow_value("a@b.com").for(:email)
33
+ should allow_value("asdf@asdf.com").for(:email)
34
+ should ensure_inclusion_of(:email).in_range(1..100)
35
+ should ensure_inclusion_of(:age).in_range(1..100)
36
+ should_not allow_mass_assignment_of(:password)
37
+ end
38
+
39
+ Makes TDD so much easier.
40
+
41
+ === Controller Tests (Shoulda::Controller::Matchers)
42
+
43
+ Matchers to test the most common controller patterns...
44
+
45
+ class PostsControllerTest < ActionController::TestCase
46
+ context "on GET to :show for first record" do
47
+ setup do
48
+ get :show, :id => 1
49
+ end
50
+
51
+ should assign_to(:user)
52
+ should respond_with(:success)
53
+ should render_template(:show)
54
+ should_not set_the_flash
55
+
56
+ should "do something else really cool" do
57
+ assert_equal 1, assigns(:user).id
58
+ end
59
+ end
60
+ end
61
+
14
62
  === Context Helpers (Shoulda::Context)
15
63
 
16
64
  Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
@@ -44,55 +92,6 @@ Produces the following test methods:
44
92
 
45
93
  So readable!
46
94
 
47
- === ActiveRecord Tests (Shoulda::ActiveRecord::Macros)
48
-
49
- Quick macro tests for your ActiveRecord associations and validations:
50
-
51
- class PostTest < Test::Unit::TestCase
52
- fixtures :all
53
-
54
- should_belong_to :user
55
- should_have_many :tags, :through => :taggings
56
-
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
61
- end
62
-
63
- class UserTest < Test::Unit::TestCase
64
- should_have_many :posts
65
-
66
- should_not_allow_values_for :email, "blah", "b lah"
67
- should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
68
- should_ensure_length_in_range :email, 1..100
69
- should_ensure_value_in_range :age, 1..100
70
- should_not_allow_mass_assignment_of :password
71
- end
72
-
73
- Makes TDD so much easier.
74
-
75
- === Controller Tests (Shoulda::Controller::Macros)
76
-
77
- Macros to test the most common controller patterns...
78
-
79
- class PostsControllerTest < ActionController::TestCase
80
- context "on GET to :show for first record" do
81
- setup do
82
- get :show, :id => 1
83
- end
84
-
85
- should_assign_to :user
86
- should_respond_with :success
87
- should_render_template :show
88
- should_not_set_the_flash
89
-
90
- should "do something else really cool" do
91
- assert_equal 1, assigns(:user).id
92
- end
93
- end
94
- end
95
-
96
95
  === Helpful Assertions (Shoulda::Assertions)
97
96
 
98
97
  More to come here, but have fun with what's there.
@@ -101,26 +100,12 @@ More to come here, but have fun with what's there.
101
100
  assert_contains(['a', '1'], /\d/)
102
101
  assert_contains(['a', '1'], 'a')
103
102
 
104
- === 3rd Party and Application Specific Macros
105
-
106
- Any *.rb file under RAILS_ROOT/test/shoulda_macros/ or vendor/(plugins|gems)/gem_name/shoulda_macros/ will be automatically required when you run your tests. This allows you to distribute macros with your plugins, or to organize the macros inside your application. Remember to add your macro to Test::Unit::TestCase in the macro file:
107
-
108
- # test/shoulda_macros/security.rb
109
- class Test::Unit::TestCase
110
- def self.should_be_denied(opts = {})
111
- should_set_the_flash_to(opts[:flash] || /Please log in/i)
112
- should_redirect_to(opts[:redirect] || 'login_url')
113
- end
114
- end
115
-
116
103
  = Rails Installation (Test::Unit)
117
104
 
118
- === As a Gem
119
-
120
- Use this if you prefer to use versioned releases of shoulda. Specify the gem dependency in your config/environment.rb file:
105
+ Specify the gem dependency in your config/environment.rb file:
121
106
 
122
107
  Rails::Initializer.run do |config|
123
- config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"
108
+ config.gem "shoulda", :lib => "shoulda"
124
109
  end
125
110
 
126
111
  Then:
@@ -128,29 +113,15 @@ Then:
128
113
  $ rake gems:install
129
114
  $ rake gems:unpack
130
115
 
131
- === As a Plugin
132
-
133
- Use this if you prefer to use the edge version of shoulda:
134
-
135
- $ script/plugin install git://github.com/thoughtbot/shoulda.git
136
-
137
- === As a Plugin (using git submodules)
138
-
139
- Use this if you prefer the idea of being able to easily switch between using edge or a tagged version of shoulda:
140
-
141
- $ git submodule add git://github.com/thoughtbot/shoulda.git vendor/plugins/shoulda
142
-
143
116
  = Rails Installation (RSpec)
144
117
 
145
118
  If you're using Shoulda with RSpec, we recommend that you add config.gem lines
146
119
  for RSpec and Shoulda in your config/environment/test.rb file, but do not ask
147
120
  Rails to load the RSpec and Shoulda libraries:
148
121
 
149
- config.gem 'rspec', :lib => false
122
+ config.gem 'rspec', :lib => false
150
123
  config.gem 'rspec-rails', :lib => false
151
- config.gem 'thoughtbot-shoulda',
152
- :lib => false,
153
- :source => 'http://gems.github.com'
124
+ config.gem 'shoulda', :lib => false
154
125
 
155
126
  Then require shoulda from your spec/spec_helper.rb file, before Spec::Runner is
156
127
  configured:
@@ -162,10 +133,22 @@ configured:
162
133
 
163
134
  You should not need to require anything besides the top-level shoulda library.
164
135
 
136
+ = Rails 3 Installation (RSpec)
137
+
138
+ With Rails 3 and Bundler, requiring Shoulda is as easy as adding it to your Gemfile:
139
+
140
+ group :test do
141
+ gem "shoulda"
142
+ gem "rspec-rails", "2.0.0.beta.12"
143
+ end
144
+
145
+ Shoulda will automatically include matchers into the appropriate example
146
+ groups.
147
+
165
148
  = Credits
166
149
 
167
- Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
150
+ Shoulda is maintained and funded by {thougthbot}[http://thoughtbot.com/community]
168
151
 
169
152
  = License
170
153
 
171
- Shoulda is Copyright © 2006-2008 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
154
+ Shoulda is Copyright © 2006-2010 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
data/Rakefile CHANGED
@@ -1,10 +1,12 @@
1
+ require 'rubygems'
1
2
  require 'rake'
2
3
  require 'rake/testtask'
3
4
  require 'rake/rdoctask'
4
5
  require 'rake/gempackagetask'
6
+ require 'cucumber/rake/task'
5
7
 
6
8
  $LOAD_PATH.unshift("lib")
7
- require 'shoulda'
9
+ require 'shoulda/version'
8
10
  load 'tasks/shoulda.rake'
9
11
 
10
12
  # Test::Unit::UI::VERBOSE
@@ -30,18 +32,6 @@ task :coverage do
30
32
  system "rcov --rails --sort coverage -Ilib #{files.join(' ')}"
31
33
  end
32
34
 
33
- desc 'Update documentation on website'
34
- task :sync_docs => 'rdoc' do
35
- `rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/shoulda`
36
- end
37
-
38
- desc 'Default: run tests for all supported versions of Rails'
39
- task :default do
40
- %w(2.3.8 3.0.0.beta3).each do |version|
41
- system("RAILS_VERSION=#{version} rake -s test;")
42
- end
43
- end
44
-
45
35
  eval("$specification = begin; #{IO.read('shoulda.gemspec')}; end")
46
36
  Rake::GemPackageTask.new $specification do |pkg|
47
37
  pkg.need_tar = true
@@ -51,3 +41,30 @@ end
51
41
  desc "Clean files generated by rake tasks"
52
42
  task :clobber => [:clobber_rdoc, :clobber_package]
53
43
 
44
+ namespace :cucumber do
45
+ Cucumber::Rake::Task.new(:rails2, "Run the cucumber features in Rails 2") do |t|
46
+ t.fork = true
47
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
48
+ t.profile = 'rails2'
49
+ end
50
+
51
+ Cucumber::Rake::Task.new(:rails3, "Run the cucumber features in Rails 3") do |t|
52
+ t.fork = true
53
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
54
+ t.profile = 'rails3'
55
+ end
56
+ end
57
+
58
+ desc "Run the cucumber features in both Rails 2 and 3"
59
+ task :cucumber => ["cucumber:rails2", "cucumber:rails3"]
60
+
61
+ desc 'run tests for all supported versions of Rails'
62
+ task :test_all do
63
+ %w(2.3.8 3.0.0.beta4).each do |version|
64
+ system("RAILS_VERSION=#{version} rake -s test;")
65
+ end
66
+ end
67
+
68
+ desc 'Default: run test and cucumber features for support versions'
69
+ task :default => [:test_all, :cucumber]
70
+
data/lib/shoulda.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'shoulda/version'
2
2
 
3
- if defined? Spec
4
- require 'shoulda/rspec'
3
+ if defined?(RSpec)
4
+ require 'shoulda/integrations/rspec2'
5
+ elsif defined?(Spec)
6
+ require 'shoulda/integrations/rspec'
5
7
  else
6
- require 'shoulda/test_unit'
8
+ require 'shoulda/integrations/test_unit'
7
9
  end
@@ -14,24 +14,24 @@ module Shoulda # :nodoc:
14
14
 
15
15
  # By using the macro helpers you can quickly and easily create concise and
16
16
  # easy to read test suites.
17
- #
17
+ #
18
18
  # This code segment:
19
- #
19
+ #
20
20
  # describe UsersController, "on GET to show with a valid id" do
21
21
  # before(:each) do
22
22
  # get :show, :id => User.first.to_param
23
23
  # end
24
- #
24
+ #
25
25
  # it { should assign_to(:user) }
26
26
  # it { should respond_with(:success) }
27
27
  # it { should render_template(:show) }
28
28
  # it { should not_set_the_flash) }
29
- #
29
+ #
30
30
  # it "should do something else really cool" do
31
31
  # assigns[:user].id.should == 1
32
32
  # end
33
33
  # end
34
- #
34
+ #
35
35
  # Would produce 5 tests for the show action
36
36
  module Matchers
37
37
  end
@@ -63,7 +63,7 @@ module Shoulda # :nodoc:
63
63
  "Expected action to assign a value for @#{@variable}"
64
64
  false
65
65
  else
66
- @negative_failure_message =
66
+ @negative_failure_message =
67
67
  "Didn't expect action to assign a value for @#{@variable}, " <<
68
68
  "but it was assigned to #{assigned_value.inspect}"
69
69
  true
@@ -34,7 +34,7 @@ module Shoulda # :nodoc:
34
34
  def description
35
35
  "respond with content type of #{@content_type}"
36
36
  end
37
-
37
+
38
38
  def matches?(controller)
39
39
  @controller = controller
40
40
  if @content_type.is_a?(Regexp)
@@ -43,32 +43,32 @@ module Shoulda # :nodoc:
43
43
  response_content_type == @content_type
44
44
  end
45
45
  end
46
-
46
+
47
47
  def failure_message
48
48
  "Expected #{expectation}"
49
49
  end
50
-
50
+
51
51
  def negative_failure_message
52
52
  "Did not expect #{expectation}"
53
53
  end
54
-
54
+
55
55
  protected
56
-
56
+
57
57
  def response_content_type
58
58
  @controller.response.content_type.to_s
59
59
  end
60
-
60
+
61
61
  def lookup_by_extension(extension)
62
62
  Mime::Type.lookup_by_extension(extension.to_s).to_s
63
63
  end
64
-
64
+
65
65
  def expectation
66
66
  "content type to be #{@content_type}, " <<
67
67
  "but was #{response_content_type}"
68
68
  end
69
-
69
+
70
70
  end
71
-
71
+
72
72
  end
73
73
  end
74
74
  end
@@ -24,16 +24,16 @@ module Shoulda # :nodoc:
24
24
  def initialize(status)
25
25
  @status = symbol_to_status_code(status)
26
26
  end
27
-
27
+
28
28
  def matches?(controller)
29
29
  @controller = controller
30
30
  correct_status_code? || correct_status_code_range?
31
31
  end
32
-
32
+
33
33
  def failure_message
34
34
  "Expected #{expectation}"
35
35
  end
36
-
36
+
37
37
  def negative_failure_message
38
38
  "Did not expect #{expectation}"
39
39
  end
@@ -41,29 +41,29 @@ module Shoulda # :nodoc:
41
41
  def description
42
42
  "respond with #{@status}"
43
43
  end
44
-
44
+
45
45
  protected
46
-
46
+
47
47
  def correct_status_code?
48
48
  response_code == @status
49
49
  end
50
-
50
+
51
51
  def correct_status_code_range?
52
52
  @status.is_a?(Range) &&
53
53
  @status.include?(response_code)
54
54
  end
55
-
55
+
56
56
  def response_code
57
57
  @controller.response.response_code
58
58
  end
59
-
59
+
60
60
  def symbol_to_status_code(potential_symbol)
61
61
  case potential_symbol
62
62
  when :success then 200
63
63
  when :redirect then 300..399
64
64
  when :missing then 404
65
65
  when :error then 500..599
66
- when Symbol
66
+ when Symbol
67
67
  if defined?(::Rack::Utils::SYMBOL_TO_STATUS_CODE)
68
68
  ::Rack::Utils::SYMBOL_TO_STATUS_CODE[potential_symbol]
69
69
  else
@@ -73,13 +73,13 @@ module Shoulda # :nodoc:
73
73
  potential_symbol
74
74
  end
75
75
  end
76
-
76
+
77
77
  def expectation
78
78
  "response to be a #{@status}, but was #{response_code}"
79
79
  end
80
-
80
+
81
81
  end
82
-
82
+
83
83
  end
84
84
  end
85
85
  end