shoulda-matchers 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +116 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +70 -0
- data/Rakefile +50 -0
- data/lib/shoulda-matchers.rb +8 -0
- data/lib/shoulda/matchers/action_controller.rb +38 -0
- data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +114 -0
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +50 -0
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +62 -0
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +54 -0
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +99 -0
- data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +74 -0
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +85 -0
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +93 -0
- data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +98 -0
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +94 -0
- data/lib/shoulda/matchers/action_mailer.rb +22 -0
- data/lib/shoulda/matchers/action_mailer/have_sent_email.rb +115 -0
- data/lib/shoulda/matchers/active_record.rb +42 -0
- data/lib/shoulda/matchers/active_record/allow_mass_assignment_of_matcher.rb +83 -0
- data/lib/shoulda/matchers/active_record/allow_value_matcher.rb +110 -0
- data/lib/shoulda/matchers/active_record/association_matcher.rb +226 -0
- data/lib/shoulda/matchers/active_record/ensure_inclusion_of_matcher.rb +87 -0
- data/lib/shoulda/matchers/active_record/ensure_length_of_matcher.rb +141 -0
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +169 -0
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +112 -0
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +59 -0
- data/lib/shoulda/matchers/active_record/helpers.rb +34 -0
- data/lib/shoulda/matchers/active_record/validate_acceptance_of_matcher.rb +41 -0
- data/lib/shoulda/matchers/active_record/validate_format_of_matcher.rb +65 -0
- data/lib/shoulda/matchers/active_record/validate_numericality_of_matcher.rb +39 -0
- data/lib/shoulda/matchers/active_record/validate_presence_of_matcher.rb +60 -0
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +148 -0
- data/lib/shoulda/matchers/active_record/validation_matcher.rb +56 -0
- data/lib/shoulda/matchers/integrations/rspec.rb +23 -0
- data/lib/shoulda/matchers/integrations/test_unit.rb +41 -0
- data/lib/shoulda/matchers/version.rb +5 -0
- metadata +113 -0
@@ -0,0 +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.
|
2
|
+
|
3
|
+
* Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/shoulda/tree/master].
|
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.
|
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.
|
10
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.3)
|
6
|
+
actionpack (= 3.0.3)
|
7
|
+
mail (~> 2.2.9)
|
8
|
+
actionpack (3.0.3)
|
9
|
+
activemodel (= 3.0.3)
|
10
|
+
activesupport (= 3.0.3)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.4)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.13)
|
16
|
+
rack-test (~> 0.5.6)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.3)
|
19
|
+
activesupport (= 3.0.3)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.4)
|
22
|
+
activerecord (3.0.3)
|
23
|
+
activemodel (= 3.0.3)
|
24
|
+
activesupport (= 3.0.3)
|
25
|
+
arel (~> 2.0.2)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.3)
|
28
|
+
activemodel (= 3.0.3)
|
29
|
+
activesupport (= 3.0.3)
|
30
|
+
activesupport (3.0.3)
|
31
|
+
arel (2.0.6)
|
32
|
+
aruba (0.2.7)
|
33
|
+
background_process
|
34
|
+
cucumber (~> 0.10.0)
|
35
|
+
background_process (1.2)
|
36
|
+
builder (2.1.2)
|
37
|
+
columnize (0.3.2)
|
38
|
+
cucumber (0.10.0)
|
39
|
+
builder (>= 2.1.2)
|
40
|
+
diff-lcs (~> 1.1.2)
|
41
|
+
gherkin (~> 2.3.2)
|
42
|
+
json (~> 1.4.6)
|
43
|
+
term-ansicolor (~> 1.0.5)
|
44
|
+
diff-lcs (1.1.2)
|
45
|
+
erubis (2.6.6)
|
46
|
+
abstract (>= 1.0.0)
|
47
|
+
gherkin (2.3.2)
|
48
|
+
json (~> 1.4.6)
|
49
|
+
term-ansicolor (~> 1.0.5)
|
50
|
+
i18n (0.5.0)
|
51
|
+
json (1.4.6)
|
52
|
+
linecache (0.43)
|
53
|
+
mail (2.2.12)
|
54
|
+
activesupport (>= 2.3.6)
|
55
|
+
i18n (>= 0.4.0)
|
56
|
+
mime-types (~> 1.16)
|
57
|
+
treetop (~> 1.4.8)
|
58
|
+
mime-types (1.16)
|
59
|
+
mocha (0.9.10)
|
60
|
+
rake
|
61
|
+
polyglot (0.3.1)
|
62
|
+
rack (1.2.1)
|
63
|
+
rack-mount (0.6.13)
|
64
|
+
rack (>= 1.0.0)
|
65
|
+
rack-test (0.5.6)
|
66
|
+
rack (>= 1.0)
|
67
|
+
rails (3.0.3)
|
68
|
+
actionmailer (= 3.0.3)
|
69
|
+
actionpack (= 3.0.3)
|
70
|
+
activerecord (= 3.0.3)
|
71
|
+
activeresource (= 3.0.3)
|
72
|
+
activesupport (= 3.0.3)
|
73
|
+
bundler (~> 1.0)
|
74
|
+
railties (= 3.0.3)
|
75
|
+
railties (3.0.3)
|
76
|
+
actionpack (= 3.0.3)
|
77
|
+
activesupport (= 3.0.3)
|
78
|
+
rake (>= 0.8.7)
|
79
|
+
thor (~> 0.14.4)
|
80
|
+
rake (0.8.7)
|
81
|
+
rspec (2.3.0)
|
82
|
+
rspec-core (~> 2.3.0)
|
83
|
+
rspec-expectations (~> 2.3.0)
|
84
|
+
rspec-mocks (~> 2.3.0)
|
85
|
+
rspec-core (2.3.0)
|
86
|
+
rspec-expectations (2.3.0)
|
87
|
+
diff-lcs (~> 1.1.2)
|
88
|
+
rspec-mocks (2.3.0)
|
89
|
+
rspec-rails (2.3.0)
|
90
|
+
actionpack (~> 3.0)
|
91
|
+
activesupport (~> 3.0)
|
92
|
+
railties (~> 3.0)
|
93
|
+
rspec (~> 2.3.0)
|
94
|
+
ruby-debug (0.10.4)
|
95
|
+
columnize (>= 0.1)
|
96
|
+
ruby-debug-base (~> 0.10.4.0)
|
97
|
+
ruby-debug-base (0.10.4)
|
98
|
+
linecache (>= 0.3)
|
99
|
+
sqlite3-ruby (1.3.2)
|
100
|
+
term-ansicolor (1.0.5)
|
101
|
+
thor (0.14.6)
|
102
|
+
treetop (1.4.9)
|
103
|
+
polyglot (>= 0.3.1)
|
104
|
+
tzinfo (0.3.23)
|
105
|
+
|
106
|
+
PLATFORMS
|
107
|
+
ruby
|
108
|
+
|
109
|
+
DEPENDENCIES
|
110
|
+
aruba
|
111
|
+
cucumber
|
112
|
+
mocha
|
113
|
+
rails (= 3.0.3)
|
114
|
+
rspec-rails
|
115
|
+
ruby-debug
|
116
|
+
sqlite3-ruby
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2007, Tammer Saleh, Thoughtbot, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= shoulda-matchers
|
2
|
+
|
3
|
+
{Official Documentation}[http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames]
|
4
|
+
|
5
|
+
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
|
+
|
8
|
+
Refer to the {shoulda}[https://github.com/thoughtbot/shoulda] gem if you want to know more
|
9
|
+
about using shoulda with Test::Unit.
|
10
|
+
|
11
|
+
=== ActiveRecord Matchers
|
12
|
+
|
13
|
+
Matchers to test associations and validations:
|
14
|
+
|
15
|
+
describe Post do
|
16
|
+
it { should belong_to(:user) }
|
17
|
+
it { should have_many(:tags).through(:taggings) }
|
18
|
+
|
19
|
+
it { should validate_uniqueness_of(:title) }
|
20
|
+
it { should validate_presence_of(:body).with_message(/wtf/) }
|
21
|
+
it { should validate_presence_of(:title) }
|
22
|
+
it { should validate_numericality_of(:user_id) }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe User do
|
26
|
+
it { should have_many(:posts) }
|
27
|
+
|
28
|
+
it { should_not allow_value("blah").for(:email) }
|
29
|
+
it { should allow_value("a@b.com").for(:email) }
|
30
|
+
it { should ensure_inclusion_of(:age).in_range(1..100) }
|
31
|
+
it { should_not allow_mass_assignment_of(:password) }
|
32
|
+
end
|
33
|
+
|
34
|
+
=== ActionController Matchers
|
35
|
+
|
36
|
+
Matchers to test common patterns:
|
37
|
+
|
38
|
+
describe PostsController, "#show" do
|
39
|
+
context "for a fictional user" do
|
40
|
+
before do
|
41
|
+
get :show, :id => 1
|
42
|
+
end
|
43
|
+
|
44
|
+
it { should assign_to(:user) }
|
45
|
+
it { should respond_with(:success) }
|
46
|
+
it { should render_template(:show) }
|
47
|
+
it { should_not set_the_flash }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
= Installation
|
52
|
+
|
53
|
+
In Rails 3 and Bundler, add the following to your Gemfile:
|
54
|
+
|
55
|
+
group :test do
|
56
|
+
gem "shoulda-matchers"
|
57
|
+
gem "rspec-rails"
|
58
|
+
end
|
59
|
+
|
60
|
+
Shoulda will automatically include matchers into the appropriate example groups.
|
61
|
+
|
62
|
+
= Credits
|
63
|
+
|
64
|
+
Shoulda is maintained and funded by {thoughtbot}[http://thoughtbot.com/community].
|
65
|
+
Thank you to all the {contributors}[https://github.com/thoughtbot/shoulda-matchers/contributors].
|
66
|
+
|
67
|
+
= License
|
68
|
+
|
69
|
+
Shoulda is Copyright © 2006-2010 thoughtbot, inc.
|
70
|
+
It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
require 'cucumber/rake/task'
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift("lib")
|
10
|
+
require 'shoulda/matchers/version'
|
11
|
+
|
12
|
+
Rake::RDocTask.new { |rdoc|
|
13
|
+
rdoc.rdoc_dir = 'doc'
|
14
|
+
rdoc.title = "Shoulda -- Making tests easy on the fingers and eyes"
|
15
|
+
rdoc.options << '--line-numbers'
|
16
|
+
rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
17
|
+
rdoc.rdoc_files.include('README.rdoc', 'CONTRIBUTION_GUIDELINES.rdoc', 'lib/**/*.rb')
|
18
|
+
}
|
19
|
+
|
20
|
+
RSpec::Core::RakeTask.new do |t|
|
21
|
+
t.pattern = "spec/**/*_spec.rb"
|
22
|
+
t.rspec_opts = '--color --format progress'
|
23
|
+
t.verbose = false
|
24
|
+
end
|
25
|
+
|
26
|
+
desc "Run code-coverage analysis using rcov"
|
27
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
28
|
+
t.rspec_opts = '--color --format progress'
|
29
|
+
t.rcov = true
|
30
|
+
t.rcov_opts = %{--exclude osx\/objc,spec,gems\/ --failure-threshold 100}
|
31
|
+
t.pattern = "spec/**/*_spec.rb"
|
32
|
+
end
|
33
|
+
|
34
|
+
eval("$specification = begin; #{IO.read('shoulda-matchers.gemspec')}; end")
|
35
|
+
Rake::GemPackageTask.new $specification do |pkg|
|
36
|
+
pkg.need_tar = true
|
37
|
+
pkg.need_zip = true
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Clean files generated by rake tasks"
|
41
|
+
task :clobber => [:clobber_rdoc, :clobber_package]
|
42
|
+
|
43
|
+
Cucumber::Rake::Task.new do |t|
|
44
|
+
t.fork = true
|
45
|
+
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
|
46
|
+
end
|
47
|
+
|
48
|
+
desc 'Default: run specs and cucumber features'
|
49
|
+
task :default => [:spec, :cucumber]
|
50
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'shoulda/matchers/action_controller/assign_to_matcher'
|
2
|
+
require 'shoulda/matchers/action_controller/filter_param_matcher'
|
3
|
+
require 'shoulda/matchers/action_controller/set_the_flash_matcher'
|
4
|
+
require 'shoulda/matchers/action_controller/render_with_layout_matcher'
|
5
|
+
require 'shoulda/matchers/action_controller/respond_with_matcher'
|
6
|
+
require 'shoulda/matchers/action_controller/respond_with_content_type_matcher'
|
7
|
+
require 'shoulda/matchers/action_controller/set_session_matcher'
|
8
|
+
require 'shoulda/matchers/action_controller/route_matcher'
|
9
|
+
require 'shoulda/matchers/action_controller/redirect_to_matcher'
|
10
|
+
require 'shoulda/matchers/action_controller/render_template_matcher'
|
11
|
+
|
12
|
+
module Shoulda
|
13
|
+
module Matchers
|
14
|
+
# By using the matchers you can quickly and easily create concise and
|
15
|
+
# easy to read test suites.
|
16
|
+
#
|
17
|
+
# This code segment:
|
18
|
+
#
|
19
|
+
# describe UsersController, "on GET to show with a valid id" do
|
20
|
+
# before(:each) do
|
21
|
+
# get :show, :id => User.first.to_param
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# it { should assign_to(:user) }
|
25
|
+
# it { should respond_with(:success) }
|
26
|
+
# it { should render_template(:show) }
|
27
|
+
# it { should not_set_the_flash) }
|
28
|
+
#
|
29
|
+
# it "should do something else really cool" do
|
30
|
+
# assigns[:user].id.should == 1
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# Would produce 5 tests for the show action
|
35
|
+
module ActionController
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module Shoulda # :nodoc:
|
2
|
+
module Matchers
|
3
|
+
module ActionController # :nodoc:
|
4
|
+
|
5
|
+
# Ensures that the controller assigned to the named instance variable.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
# * <tt>with_kind_of</tt> - The expected class of the instance variable
|
9
|
+
# being checked.
|
10
|
+
# * <tt>with</tt> - The value that should be assigned.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# it { should assign_to(:user) }
|
15
|
+
# it { should_not assign_to(:user) }
|
16
|
+
# it { should assign_to(:user).with_kind_of(User) }
|
17
|
+
# it { should assign_to(:user).with(@user) }
|
18
|
+
def assign_to(variable)
|
19
|
+
AssignToMatcher.new(variable)
|
20
|
+
end
|
21
|
+
|
22
|
+
class AssignToMatcher # :nodoc:
|
23
|
+
|
24
|
+
def initialize(variable)
|
25
|
+
@variable = variable.to_s
|
26
|
+
@check_value = false
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_kind_of(expected_class)
|
30
|
+
@expected_class = expected_class
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def with(expected_value = nil, &block)
|
35
|
+
@check_value = true
|
36
|
+
@expected_value = expected_value
|
37
|
+
@expectation_block = block
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def matches?(controller)
|
42
|
+
@controller = controller
|
43
|
+
@expected_value = @context.instance_eval(&@expectation_block) if @expectation_block
|
44
|
+
assigned_value? && kind_of_expected_class? && equal_to_expected_value?
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :failure_message, :negative_failure_message
|
48
|
+
|
49
|
+
def description
|
50
|
+
description = "assign @#{@variable}"
|
51
|
+
description << " with a kind of #{@expected_class}" if @expected_class
|
52
|
+
description
|
53
|
+
end
|
54
|
+
|
55
|
+
def in_context(context)
|
56
|
+
@context = context
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def assigned_value?
|
63
|
+
if !@controller.instance_variables.include?("@#{@variable}")
|
64
|
+
@failure_message =
|
65
|
+
"Expected action to assign a value for @#{@variable}"
|
66
|
+
false
|
67
|
+
else
|
68
|
+
@negative_failure_message =
|
69
|
+
"Didn't expect action to assign a value for @#{@variable}, " <<
|
70
|
+
"but it was assigned to #{assigned_value.inspect}"
|
71
|
+
true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def kind_of_expected_class?
|
76
|
+
return true unless @expected_class
|
77
|
+
if assigned_value.kind_of?(@expected_class)
|
78
|
+
@negative_failure_message =
|
79
|
+
"Didn't expect action to assign a kind of #{@expected_class} " <<
|
80
|
+
"for #{@variable}, but got one anyway"
|
81
|
+
true
|
82
|
+
else
|
83
|
+
@failure_message =
|
84
|
+
"Expected action to assign a kind of #{@expected_class} " <<
|
85
|
+
"for #{@variable}, but got #{@variable.inspect} " <<
|
86
|
+
"(#{@variable.class.name})"
|
87
|
+
false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def equal_to_expected_value?
|
92
|
+
return true unless @check_value
|
93
|
+
if @expected_value == assigned_value
|
94
|
+
@negative_failure_message =
|
95
|
+
"Didn't expect action to assign #{@expected_value.inspect} " <<
|
96
|
+
"for #{@variable}, but got it anyway"
|
97
|
+
true
|
98
|
+
else
|
99
|
+
@failure_message =
|
100
|
+
"Expected action to assign #{@expected_value.inspect} " <<
|
101
|
+
"for #{@variable}, but got #{assigned_value.inspect}"
|
102
|
+
false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def assigned_value
|
107
|
+
@controller.instance_variable_get("@#{@variable}")
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|