yetanothernguyen-shoulda-matchers 1.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/Appraisals +12 -0
  2. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  3. data/Gemfile +5 -0
  4. data/MIT-LICENSE +22 -0
  5. data/README.rdoc +78 -0
  6. data/Rakefile +55 -0
  7. data/lib/shoulda-matchers.rb +1 -0
  8. data/lib/shoulda/matchers.rb +8 -0
  9. data/lib/shoulda/matchers/action_controller.rb +38 -0
  10. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +114 -0
  11. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +50 -0
  12. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +62 -0
  13. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +54 -0
  14. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +99 -0
  15. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +74 -0
  16. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +85 -0
  17. data/lib/shoulda/matchers/action_controller/route_matcher.rb +93 -0
  18. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +98 -0
  19. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +94 -0
  20. data/lib/shoulda/matchers/action_mailer.rb +22 -0
  21. data/lib/shoulda/matchers/action_mailer/have_sent_email.rb +166 -0
  22. data/lib/shoulda/matchers/active_model.rb +33 -0
  23. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +83 -0
  24. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +110 -0
  25. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +87 -0
  26. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +141 -0
  27. data/lib/shoulda/matchers/active_model/helpers.rb +29 -0
  28. data/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb +41 -0
  29. data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +65 -0
  30. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +39 -0
  31. data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +61 -0
  32. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +148 -0
  33. data/lib/shoulda/matchers/active_model/validation_matcher.rb +56 -0
  34. data/lib/shoulda/matchers/active_record.rb +24 -0
  35. data/lib/shoulda/matchers/active_record/association_matcher.rb +226 -0
  36. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +169 -0
  37. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +112 -0
  38. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +59 -0
  39. data/lib/shoulda/matchers/assertion_error.rb +11 -0
  40. data/lib/shoulda/matchers/integrations/rspec.rb +38 -0
  41. data/lib/shoulda/matchers/integrations/test_unit.rb +54 -0
  42. data/lib/shoulda/matchers/version.rb +5 -0
  43. metadata +169 -0
@@ -0,0 +1,12 @@
1
+ appraise "3.0.3" do
2
+ gem "rails", "3.0.3"
3
+ gem "rake", "~> 0.8.7"
4
+ end
5
+
6
+ appraise "3.1.0.rc1" do
7
+ gem "rails", "3.1.0.rc1"
8
+ gem 'sass'
9
+ gem 'coffee-script'
10
+ gem 'uglifier'
11
+ gem 'jquery-rails'
12
+ end
@@ -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
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rake', '~> 0.8.7'
5
+
@@ -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.
@@ -0,0 +1,78 @@
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:
14
+
15
+ describe Post do
16
+ it { should belong_to(:user) }
17
+ it { should have_many(:tags).through(:taggings) }
18
+ end
19
+
20
+ describe User do
21
+ it { should have_many(:posts) }
22
+ end
23
+
24
+ === ActiveModel Matchers
25
+
26
+ Matchers to test validations and mass assignments:
27
+
28
+ describe Post do
29
+ it { should validate_uniqueness_of(:title) }
30
+ it { should validate_presence_of(:body).with_message(/wtf/) }
31
+ it { should validate_presence_of(:title) }
32
+ it { should validate_numericality_of(:user_id) }
33
+ end
34
+
35
+ describe User do
36
+ it { should_not allow_value("blah").for(:email) }
37
+ it { should allow_value("a@b.com").for(:email) }
38
+ it { should ensure_inclusion_of(:age).in_range(1..100) }
39
+ it { should_not allow_mass_assignment_of(:password) }
40
+ end
41
+
42
+ === ActionController Matchers
43
+
44
+ Matchers to test common patterns:
45
+
46
+ describe PostsController, "#show" do
47
+ context "for a fictional user" do
48
+ before do
49
+ get :show, :id => 1
50
+ end
51
+
52
+ it { should assign_to(:user) }
53
+ it { should respond_with(:success) }
54
+ it { should render_template(:show) }
55
+ it { should_not set_the_flash }
56
+ end
57
+ end
58
+
59
+ = Installation
60
+
61
+ In Rails 3 and Bundler, add the following to your Gemfile:
62
+
63
+ group :test do
64
+ gem "rspec-rails"
65
+ gem "shoulda-matchers"
66
+ end
67
+
68
+ Shoulda will automatically include matchers into the appropriate example groups.
69
+
70
+ = Credits
71
+
72
+ Shoulda is maintained and funded by {thoughtbot}[http://thoughtbot.com/community].
73
+ Thank you to all the {contributors}[https://github.com/thoughtbot/shoulda-matchers/contributors].
74
+
75
+ = License
76
+
77
+ Shoulda is Copyright © 2006-2010 thoughtbot, inc.
78
+ It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
@@ -0,0 +1,55 @@
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
+ require 'appraisal'
9
+
10
+ $LOAD_PATH.unshift("lib")
11
+ require 'shoulda/matchers/version'
12
+
13
+ Rake::RDocTask.new { |rdoc|
14
+ rdoc.rdoc_dir = 'doc'
15
+ rdoc.title = "Shoulda -- Making tests easy on the fingers and eyes"
16
+ rdoc.options << '--line-numbers'
17
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
18
+ rdoc.rdoc_files.include('README.rdoc', 'CONTRIBUTION_GUIDELINES.rdoc', 'lib/**/*.rb')
19
+ }
20
+
21
+ RSpec::Core::RakeTask.new do |t|
22
+ t.pattern = "spec/**/*_spec.rb"
23
+ t.rspec_opts = '--color --format progress'
24
+ t.verbose = false
25
+ end
26
+
27
+ desc "Run code-coverage analysis using rcov"
28
+ RSpec::Core::RakeTask.new(:coverage) do |t|
29
+ t.rspec_opts = '--color --format progress'
30
+ t.rcov = true
31
+ t.rcov_opts = %{--exclude osx\/objc,spec,gems\/ --failure-threshold 100}
32
+ t.pattern = "spec/**/*_spec.rb"
33
+ end
34
+
35
+ eval("$specification = begin; #{IO.read('shoulda-matchers.gemspec')}; end")
36
+ Rake::GemPackageTask.new $specification do |pkg|
37
+ pkg.need_tar = true
38
+ pkg.need_zip = true
39
+ end
40
+
41
+ desc "Clean files generated by rake tasks"
42
+ task :clobber => [:clobber_rdoc, :clobber_package]
43
+
44
+ Cucumber::Rake::Task.new do |t|
45
+ t.fork = false
46
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
47
+ end
48
+
49
+ desc 'Default: run specs and cucumber features'
50
+ task :default => [:all]
51
+
52
+ desc 'Test the plugin under all supported Rails versions.'
53
+ task :all => ["appraisal:cleanup", "appraisal:install"] do |t|
54
+ exec('rake appraisal spec cucumber')
55
+ end
@@ -0,0 +1 @@
1
+ require 'shoulda/matchers'
@@ -0,0 +1,8 @@
1
+ require 'shoulda/matchers/version'
2
+ require 'shoulda/matchers/assertion_error'
3
+
4
+ if defined?(RSpec)
5
+ require 'shoulda/matchers/integrations/rspec'
6
+ else
7
+ require 'shoulda/matchers/integrations/test_unit'
8
+ end
@@ -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.map(&:to_s).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 #{assigned_value.inspect} " <<
86
+ "(#{assigned_value.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
@@ -0,0 +1,50 @@
1
+ module Shoulda # :nodoc:
2
+ module Matchers
3
+ module ActionController # :nodoc:
4
+
5
+ # Ensures that filter_parameter_logging is set for the specified key.
6
+ #
7
+ # Example:
8
+ #
9
+ # it { should filter_param(:password) }
10
+ def filter_param(key)
11
+ FilterParamMatcher.new(key)
12
+ end
13
+
14
+ class FilterParamMatcher # :nodoc:
15
+
16
+ def initialize(key)
17
+ @key = key.to_s
18
+ end
19
+
20
+ def matches?(controller)
21
+ @controller = controller
22
+ filters_key?
23
+ end
24
+
25
+ def failure_message
26
+ "Expected #{@key} to be filtered; filtered keys: #{filtered_keys.join(', ')}"
27
+ end
28
+
29
+ def negative_failure_message
30
+ "Did not expect #{@key} to be filtered"
31
+ end
32
+
33
+ def description
34
+ "filter #{@key}"
35
+ end
36
+
37
+ private
38
+
39
+ def filters_key?
40
+ filtered_keys.include?(@key)
41
+ end
42
+
43
+ def filtered_keys
44
+ Rails.application.config.filter_parameters.map { |filter| filter.to_s }
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,62 @@
1
+ module Shoulda # :nodoc:
2
+ module Matchers
3
+ module ActionController # :nodoc:
4
+
5
+ # Ensures a controller redirected to the given url.
6
+ #
7
+ # Example:
8
+ #
9
+ # it { should redirect_to('http://somewhere.com') }
10
+ # it { should redirect_to(users_path) }
11
+ def redirect_to(url_or_description, &block)
12
+ RedirectToMatcher.new(url_or_description, self, &block)
13
+ end
14
+
15
+ class RedirectToMatcher # :nodoc:
16
+
17
+ def initialize(url_or_description, context, &block)
18
+ if block
19
+ @url_block = block
20
+ @location = url_or_description
21
+ else
22
+ @url = url_or_description
23
+ @location = @url
24
+ end
25
+ @context = context
26
+ end
27
+
28
+ def in_context(context)
29
+ @context = context
30
+ self
31
+ end
32
+
33
+ def matches?(controller)
34
+ @controller = controller
35
+ redirects_to_url?
36
+ end
37
+
38
+ attr_reader :failure_message, :negative_failure_message
39
+
40
+ def description
41
+ "redirect to #{@location}"
42
+ end
43
+
44
+ private
45
+
46
+ def redirects_to_url?
47
+ @url = @context.instance_eval(&@url_block) if @url_block
48
+ begin
49
+ @context.send(:assert_redirected_to, @url)
50
+ @negative_failure_message = "Didn't expect to redirect to #{@url}"
51
+ true
52
+ rescue Shoulda::Matchers::AssertionError => error
53
+ @failure_message = error.message
54
+ false
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
62
+ end