rspec_validation_expectations 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ 5/5/09 - Added scaffolding for future tests [Matthew Bass]
2
+
3
+ 4/16/09 - Added it_should_protect expectation [Matthew Bass]
4
+
5
+ 8/14/08 - Initial import [Matthew Bass]
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2007-2008 Matthew Bass (http://matthewbass.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4
+ and associated documentation files (the "Software"), to deal in the Software without
5
+ restriction, including without limitation the rights to use, copy, modify, merge, publish,
6
+ distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or
10
+ substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,71 @@
1
+ = rspec_validation_expectations
2
+
3
+ Adds several handy expectations for testing ActiveRecord model validations and associations.
4
+
5
+ == Dependencies
6
+
7
+ Requires the validation_reflection plugin:
8
+
9
+ script/plugin install git://github.com/redinger/validation_reflection.git
10
+
11
+ == Installation
12
+
13
+ Install the gem directly:
14
+
15
+ sudo gem install pelargir-rspec_validation_expectations --source=http://gems.github.com
16
+
17
+ Or install the gem in your Rails project:
18
+
19
+ script/plugin install git://github.com/pelargir/rspec_validation_expectations.git
20
+
21
+ Or clone the project:
22
+
23
+ git clone git://github.com/pelargir/rspec_validation_expectations.git
24
+
25
+ == Usage
26
+
27
+ Ensure that validations and assocations are properly defined in your models:
28
+
29
+ describe User do
30
+ it_should_validate_presence_of :first_name, :last_name, :email
31
+ it_should_validate_numericality_of :zip
32
+ it_should_validate_uniqueness_of :email
33
+ it_should_validate_inclusion_of :gender, :in => %w(Male Female)
34
+
35
+ # tests that User.count increases by 1
36
+ it_should_be_createable :with => {:first_name => 'Ed', :last_name => 'The Duck', :email => 'a@b.com'}
37
+
38
+ it_should_belong_to :employer
39
+ it_should_have_many :friends, :romans, :countrymen
40
+ it_should_have_one :account
41
+ it_should_have_and_belong_to_many :comments
42
+
43
+ # tests that the attribute is protected
44
+ it_should_protect :email
45
+ end
46
+
47
+ Which gives you:
48
+
49
+ ruby script/spec --format s spec/models/user_spec.rb:
50
+
51
+ User
52
+ - should validate presence of first name
53
+ - should validate presence of last name
54
+ - should validate presence of email
55
+ - should validate numericality of zip
56
+ - should validate uniqueness of email
57
+ - should validate inclusion of gender as one of Male or Female
58
+ - should be creatable
59
+ - should belong to employer
60
+ - should have many friends
61
+ - should have many romans
62
+ - should have many countrymen
63
+ - should have one account
64
+ - should have and belong to many comments
65
+ - should protect email
66
+
67
+ == Resources
68
+
69
+ Repository: http://github.com/pelargir/rspec_validation_expectations/
70
+ Blog: http://matthewbass.com
71
+ Author: Matthew Bass
@@ -0,0 +1,13 @@
1
+ require "rake"
2
+ require "spec/rake/spectask"
3
+
4
+ task :default => :spec
5
+
6
+ Spec::Rake::SpecTask.new do |spec|
7
+ spec.spec_files = FileList["spec/**/*_spec.rb"]
8
+ spec.spec_opts << "--color"
9
+ spec.libs += ["lib", "spec"]
10
+ spec.rcov = true
11
+ spec.rcov_opts = ["--exclude", "spec/**/*"]
12
+ spec.rcov_dir = "rcov"
13
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rspec_validation_expectations'
@@ -0,0 +1,5 @@
1
+ require 'spec/rails/matchers/associations'
2
+
3
+ require 'rspec_validation_expectations/validation_expectations'
4
+ require 'rspec_validation_expectations/association_expectations'
5
+ require 'rspec_validation_expectations/other_expectations'
@@ -0,0 +1,35 @@
1
+ def it_should_belong_to *one_or_more_associations
2
+ model_name = described_type
3
+ one_or_more_associations.each do |association|
4
+ it "should belong to #{association}" do
5
+ model_name.should belong_to(association)
6
+ end
7
+ end
8
+ end
9
+
10
+ def it_should_have_many *one_or_more_associations
11
+ model_name = described_type
12
+ one_or_more_associations.each do |association|
13
+ it "should have many #{association}" do
14
+ model_name.should have_many(association)
15
+ end
16
+ end
17
+ end
18
+
19
+ def it_should_have_and_belong_to_many *one_or_more_associations
20
+ model_name = described_type
21
+ one_or_more_associations.each do |association|
22
+ it "should have and belong to many #{association}" do
23
+ model_name.should have_and_belong_to_many(association)
24
+ end
25
+ end
26
+ end
27
+
28
+ def it_should_have_one *one_or_more_associations
29
+ model_name = described_type
30
+ one_or_more_associations.each do |association|
31
+ it "should have one #{association}" do
32
+ model_name.should have_one(association)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ def it_should_protect(*one_or_more_attrs)
2
+ model_class = described_type
3
+ one_or_more_attrs.each do |attr|
4
+ it "should protect attribute #{attr}" do
5
+ model_class.protected_attributes.should include(attr.to_s)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ module ValidationExpectations
2
+
3
+ BasicValidations = [:presence, :acceptance, :numericality, :format, :length, :confirmation, :uniqueness]
4
+ InListValidations = [:exclusion, :inclusion]
5
+
6
+ BasicValidations.each do |method|
7
+ define_method("it_should_validate_#{method}_of") do |*one_or_more_fields|
8
+ options = one_or_more_fields.last.is_a?(Hash) ? one_or_more_fields.pop : {}
9
+ model_name = described_type
10
+ one_or_more_fields.each do |field|
11
+ it "should validate #{method} of #{field.to_s.humanize.downcase}" do
12
+ validations = model_name.reflect_on_all_validations
13
+ validations = validations.select { |e| e.macro == "validates_#{method}_of".to_sym }
14
+ validations.collect(&:name).should include(field)
15
+ validations.collect(&:options).should include(options)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ InListValidations.each do |method|
22
+ define_method("it_should_validate_#{method}_of") do |*one_or_more_fields|
23
+ options = one_or_more_fields.last.is_a?(Hash) ? one_or_more_fields.pop : {}
24
+ model_name = described_type
25
+ one_or_more_fields.each do |field|
26
+ it "should validate #{method} of #{field.to_s.humanize.downcase} as one of #{options[:in].to_sentence(:words_connector => 'or', :last_word_connector => true)}" do
27
+ validations = model_name.reflect_on_all_validations
28
+ validation = validations.detect { |v| v.macro == "validates_#{method}_of".to_sym && v.name == field }
29
+ validation.options[:in].sort.should == options[:in].sort unless validation.nil?
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ include ValidationExpectations
38
+
39
+ def it_should_be_createable *args
40
+ model_name = described_type
41
+ attributes = args.last.is_a?(Hash) ? args.last[:with] : {}
42
+
43
+ it "should be creatable" do
44
+ lambda { model_name.create attributes }.should change(model_name, :count).by(1)
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ # Copied from Josh Knowles' excellent Rspec on Rails Matchers
2
+ # (http://github.com/joshknowles/rspec-on-rails-matchers/tree/master)
3
+
4
+ module Spec
5
+ module Example
6
+ class ExampleGroup
7
+ def belong_to(association)
8
+ return simple_matcher("model to belong to #{association}") do |model|
9
+ model = model.class if model.is_a? ActiveRecord::Base
10
+ model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
11
+ end
12
+ end
13
+
14
+ def have_many(association)
15
+ return simple_matcher("model to have many #{association}") do |model|
16
+ model = model.class if model.is_a? ActiveRecord::Base
17
+ model.reflect_on_all_associations(:has_many).find { |a| a.name == association }
18
+ end
19
+ end
20
+
21
+ def have_one(association)
22
+ return simple_matcher("model to have one #{association}") do |model|
23
+ model = model.class if model.is_a? ActiveRecord::Base
24
+ model.reflect_on_all_associations(:has_one).find { |a| a.name == association }
25
+ end
26
+ end
27
+
28
+ def have_and_belong_to_many(association)
29
+ return simple_matcher("model to have and belong to many #{association}") do |model|
30
+ model = model.class if model.is_a? ActiveRecord::Base
31
+ model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe TestClass do
4
+
5
+ it_should_belong_to :user_group
6
+ it_should_have_one :address
7
+ it_should_have_many :projects
8
+ it_should_have_and_belong_to_many :groups
9
+
10
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe TestClass do
4
+
5
+ it_should_protect :admin
6
+
7
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ describe TestClass do
4
+
5
+ it_should_validate_presence_of :first_name, :last_name
6
+ it_should_validate_numericality_of :age
7
+ it_should_validate_uniqueness_of :email
8
+ it_should_validate_inclusion_of :level, :in => TestClass::Levels
9
+ it_should_validate_exclusion_of :username, :in => TestClass::BadUsernames
10
+ it_should_validate_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/
11
+ it_should_validate_length_of :password, :in => 6..20
12
+ it_should_validate_confirmation_of :email
13
+ it_should_validate_acceptance_of :terms_of_service
14
+ #it_should_be_createable
15
+
16
+ end
@@ -0,0 +1,8 @@
1
+ require "spec"
2
+ require "rspec_validation_expectations"
3
+
4
+ require "active_support"
5
+ require "active_record"
6
+ require "validation_reflection"
7
+
8
+ require "test_class"
@@ -0,0 +1,23 @@
1
+ class TestClass < ActiveRecord::Base
2
+
3
+ Levels = %w(beginner intermediate expert)
4
+ BadUsernames = %w(user admin)
5
+
6
+ belongs_to :user_group
7
+ has_one :address
8
+ has_many :projects
9
+ has_and_belongs_to_many :groups
10
+
11
+ attr_protected :admin
12
+
13
+ validates_presence_of :first_name, :last_name
14
+ validates_numericality_of :age
15
+ validates_uniqueness_of :email
16
+ validates_inclusion_of :level, :in => Levels
17
+ validates_exclusion_of :username, :in => BadUsernames
18
+ validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/
19
+ validates_length_of :password, :in => 6..20
20
+ validates_confirmation_of :email
21
+ validates_acceptance_of :terms_of_service
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_validation_expectations
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.5"
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Bass
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-14 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.5
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: validation_reflection
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.3.5
44
+ version:
45
+ description: Adds several handy expectations for testing ActiveRecord model validations, similar to what Shoulda provides.
46
+ email: pelargir@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README
53
+ files:
54
+ - CHANGELOG
55
+ - MIT-LICENSE
56
+ - Rakefile
57
+ - README
58
+ - init.rb
59
+ - lib/rspec_validation_expectations/association_expectations.rb
60
+ - lib/rspec_validation_expectations/other_expectations.rb
61
+ - lib/rspec_validation_expectations/validation_expectations.rb
62
+ - lib/rspec_validation_expectations.rb
63
+ - lib/spec/rails/matchers/associations.rb
64
+ - spec/rspec_validation_expectations/association_expectations_spec.rb
65
+ - spec/rspec_validation_expectations/other_expectations_spec.rb
66
+ - spec/rspec_validation_expectations/validation_expectations_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/test_class.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/pelargir/rspec_validation_expectations
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --main
76
+ - README
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: rspec_validation_expectations
94
+ rubygems_version: 1.3.5
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Adds expectations for testing ActiveRecord model validations.
98
+ test_files: []
99
+