test_friendly 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +82 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/global.rb +43 -0
- data/lib/test_friendly.rb +146 -0
- data/lib/test_friendly_helper.rb +24 -0
- data/spec/models/user1.rb +10 -0
- data/spec/models/user10.rb +14 -0
- data/spec/models/user11.rb +26 -0
- data/spec/models/user2.rb +10 -0
- data/spec/models/user3.rb +10 -0
- data/spec/models/user4.rb +14 -0
- data/spec/models/user5.rb +14 -0
- data/spec/models/user6.rb +14 -0
- data/spec/models/user7.rb +10 -0
- data/spec/models/user8.rb +14 -0
- data/spec/models/user9.rb +14 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/test_friendly_spec.rb +159 -0
- data/test_friendly.gemspec +82 -0
- metadata +184 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.1.1)
|
5
|
+
activesupport (= 3.1.1)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
i18n (~> 0.6)
|
8
|
+
activesupport (3.1.1)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
builder (3.0.0)
|
11
|
+
columnize (0.3.6)
|
12
|
+
debugger (1.1.1)
|
13
|
+
columnize (>= 0.3.1)
|
14
|
+
debugger-linecache (~> 1.1)
|
15
|
+
debugger-ruby_core_source (~> 1.1)
|
16
|
+
debugger-linecache (1.1.1)
|
17
|
+
debugger-ruby_core_source (>= 1.1.1)
|
18
|
+
debugger-ruby_core_source (1.1.1)
|
19
|
+
diff-lcs (1.1.3)
|
20
|
+
git (1.2.5)
|
21
|
+
i18n (0.6.0)
|
22
|
+
jeweler (1.8.3)
|
23
|
+
bundler (~> 1.0)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rake
|
26
|
+
rdoc
|
27
|
+
json (1.6.6)
|
28
|
+
multi_json (1.0.4)
|
29
|
+
rake (0.9.2.2)
|
30
|
+
rdoc (3.12)
|
31
|
+
json (~> 1.4)
|
32
|
+
rspec (2.7.0)
|
33
|
+
rspec-core (~> 2.7.0)
|
34
|
+
rspec-expectations (~> 2.7.0)
|
35
|
+
rspec-mocks (~> 2.7.0)
|
36
|
+
rspec-core (2.7.1)
|
37
|
+
rspec-expectations (2.7.0)
|
38
|
+
diff-lcs (~> 1.1.2)
|
39
|
+
rspec-mocks (2.7.0)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
activemodel
|
46
|
+
activesupport
|
47
|
+
bundler
|
48
|
+
debugger
|
49
|
+
jeweler (~> 1.8.3)
|
50
|
+
rdoc
|
51
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Andriy Romanov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
= Test friendly
|
2
|
+
|
3
|
+
Test friendly gem provides functionality to turn off validations in test environment.
|
4
|
+
|
5
|
+
In order to turn off validations in test environment, class inherited from ActiveRecord::Base should have <tt>acts_as_test_friendly</tt> static method and <tt>test_friendly_validations</tt> block where validations should be inserted. Here is an example:
|
6
|
+
|
7
|
+
class User < ActiveRecord::Base
|
8
|
+
acts_as_test_friendly
|
9
|
+
|
10
|
+
attr_accessor :first_name, :last_name
|
11
|
+
|
12
|
+
test_friendly_validations do
|
13
|
+
validates_presence_of :first_name, :last_name
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
In order to turn validations on, <tt>User.force_validations</tt> method has to be used (usually in before validation hook). Also user can optionally turn on only specific validations:
|
19
|
+
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
acts_as_test_friendly
|
22
|
+
|
23
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
24
|
+
|
25
|
+
test_friendly_validations do
|
26
|
+
validates_presence_of :first_name, :last_name
|
27
|
+
end
|
28
|
+
|
29
|
+
test_friendly_validations(:additional) do
|
30
|
+
validates_presence_of :new_attribute
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
Here in order to turn on only additional validations, <tt>User.force_validations(:additional)</tt> construction has to be used. If tag is not specified - validation goes under :defaults tag. So, in order to turn on first and last name validation in example above, <tt>User.force_validations(:defaults)</tt> has to be used. For forcing all validations for all tags, <tt>User.force_validations(:all)</tt> should be used.
|
36
|
+
|
37
|
+
There is also drop validations functionality. It has same signature as force_validations, but different method is used. <tt>User.drop_validations</tt>
|
38
|
+
|
39
|
+
Test friendly gem is also supplied with functionality that lets turn on/off callbacks in test environment. Here is an example of class:
|
40
|
+
|
41
|
+
class User < ActiveRecord::Base
|
42
|
+
include AllMissingUserMethods
|
43
|
+
|
44
|
+
acts_as_test_friendly
|
45
|
+
|
46
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
47
|
+
|
48
|
+
test_friendly_callbacks do
|
49
|
+
after_save :send_notification_email
|
50
|
+
end
|
51
|
+
|
52
|
+
test_friendly_callbacks(:additional) do
|
53
|
+
before_save :create_order_instance
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
Callbacks work same way as validations, but have different methods signature (valdiations are substituted with callbacks). <tt>User.force_callbacks</tt>, <tt>User.force_callbacks(:additional)</tt>, <tt>User.force_callbacks(:all)</tt>, <tt>User.drop_callbacks</tt>, <tt>User.drop_callbacks(:additional)</tt>, <tt>User.drop_callbacks(:all)</tt> all can be used.
|
59
|
+
|
60
|
+
At the moment gem automatically turns off validations and callbacks in every Rspec test. If you are using different testing tool, there are some methods that can help you:
|
61
|
+
1. TestFriendly::Global.drop_validations(:all) - drops all validations
|
62
|
+
2. TestFriendly::Global.force_validations(:all) - forces all validations
|
63
|
+
3. TestFriendly::Global.drop_callbacks(:all) - drops all callbacks
|
64
|
+
4. TestFriendly::Global.force_callbacks(:all) - forces all callbacks
|
65
|
+
|
66
|
+
Also you can use specific tag as parameter as well as leave parameter blank (in this case :defaults tag will be used).
|
67
|
+
|
68
|
+
== Contributing to test_friendly
|
69
|
+
|
70
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
71
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
72
|
+
* Fork the project.
|
73
|
+
* Start a feature/bugfix branch.
|
74
|
+
* Commit and push until you are happy with your contribution.
|
75
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
76
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
77
|
+
|
78
|
+
== Copyright
|
79
|
+
|
80
|
+
Copyright (c) 2012 Andriy Romanov. See LICENSE.txt for
|
81
|
+
further details.
|
82
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "test_friendly"
|
18
|
+
gem.homepage = "http://github.com/romanoff/test_friendly"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{TODO: one-line summary of your gem}
|
21
|
+
gem.description = %Q{TODO: longer description of your gem}
|
22
|
+
gem.email = "aromanov@partnerpedia.com"
|
23
|
+
gem.authors = ["Andriy Romanov"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "test_friendly #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/lib/global.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module TestFriendly
|
2
|
+
|
3
|
+
class Global
|
4
|
+
def self.add_model(model)
|
5
|
+
@models ||= []
|
6
|
+
@models << model
|
7
|
+
@models.uniq!
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.force_validations(tag = :defaults)
|
11
|
+
@models ||= []
|
12
|
+
@models.each do |model|
|
13
|
+
model.force_validations(tag)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.force_callbacks(tag = :defaults)
|
18
|
+
@models ||= []
|
19
|
+
@models.each do |model|
|
20
|
+
model.force_callbacks(tag)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.drop_validations(tag = :defaults)
|
25
|
+
@models ||= []
|
26
|
+
@models.each do |model|
|
27
|
+
model.drop_validations(tag)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.drop_callbacks(tag = :defaults)
|
32
|
+
@models ||= []
|
33
|
+
@models.each do |model|
|
34
|
+
model.drop_callbacks(tag)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.callbacks_on?
|
39
|
+
Rails.env != 'test'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'global')
|
2
|
+
require File.join(File.dirname(__FILE__), 'test_friendly_helper')
|
3
|
+
|
4
|
+
module TestFriendly
|
5
|
+
|
6
|
+
ModelCallback = Struct.new(:callback, :hash, :runner_symbol, :type)
|
7
|
+
|
8
|
+
VALIDATION = 1
|
9
|
+
CALLBACK = 2
|
10
|
+
|
11
|
+
def __define_runner(symbol)
|
12
|
+
@runner_symbols << symbol unless @runner_symbols.include?(symbol)
|
13
|
+
super(symbol)
|
14
|
+
end
|
15
|
+
|
16
|
+
def acts_as_test_friendly
|
17
|
+
@test_friendly = true
|
18
|
+
@model_callbacks = []
|
19
|
+
@runner_symbols = []
|
20
|
+
Global.add_model(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_friendly?
|
24
|
+
!!@test_friendly
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_friendly_validations(tag = :defaults, &block)
|
28
|
+
acts_as_test_friendly if !test_friendly?
|
29
|
+
helper = TestFriendlyHelper.get_helper_for(tag, VALIDATION)
|
30
|
+
helper.unprocessed_procs << block
|
31
|
+
if Global.callbacks_on?
|
32
|
+
execute_callback_blocks(tag, VALIDATION)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_friendly_callbacks(tag = :defaults, &block)
|
37
|
+
acts_as_test_friendly if !test_friendly?
|
38
|
+
helper = TestFriendlyHelper.get_helper_for(tag, CALLBACK)
|
39
|
+
helper.unprocessed_procs << block
|
40
|
+
if Global.callbacks_on?
|
41
|
+
execute_callback_blocks(tag, CALLBACK)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def force_callbacks(tag = :defaults, type = CALLBACK)
|
46
|
+
acts_as_test_friendly if !test_friendly?
|
47
|
+
callbacks_added = execute_callback_blocks(tag, type)
|
48
|
+
helper = TestFriendlyHelper.get_helper_for(tag, type)
|
49
|
+
if tag == :all || (!callbacks_added && !helper.tagged_callbacks.empty?)
|
50
|
+
hashes = callbacks_hashes
|
51
|
+
used_runners = []
|
52
|
+
@model_callbacks.each do |model_callback|
|
53
|
+
if !hashes.include?(model_callback.hash) &&
|
54
|
+
((tag == :all && model_callback.type == type) || helper.tagged_callbacks.include?(model_callback.hash))
|
55
|
+
if !used_runners.include?(model_callback.runner_symbol)
|
56
|
+
used_runners << model_callback.runner_symbol
|
57
|
+
end
|
58
|
+
self.send("_#{model_callback.runner_symbol}_callbacks").push(model_callback.callback)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
used_runners.each do |runner_symbol|
|
62
|
+
self.__define_runner(runner_symbol)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def drop_callbacks(tag = :defaults, type = CALLBACK)
|
68
|
+
acts_as_test_friendly if !test_friendly?
|
69
|
+
helper = TestFriendlyHelper.get_helper_for(tag, type)
|
70
|
+
used_runners = []
|
71
|
+
@runner_symbols.each do |runner_symbol|
|
72
|
+
self.send("_#{runner_symbol}_callbacks").reject! { |callback|
|
73
|
+
model_callback = @model_callbacks.find{|mc| mc.hash == callback.hash}
|
74
|
+
value = ((tag == :all && model_callback && model_callback.type == type) ||
|
75
|
+
helper.tagged_callbacks.include?(callback.hash))
|
76
|
+
if value && !used_runners.include?(runner_symbol)
|
77
|
+
used_runners << runner_symbol
|
78
|
+
end
|
79
|
+
value
|
80
|
+
}
|
81
|
+
end
|
82
|
+
used_runners.each do |runner_symbol|
|
83
|
+
self.__define_runner(runner_symbol)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def force_validations(tag = :defaults)
|
88
|
+
force_callbacks(tag, VALIDATION)
|
89
|
+
end
|
90
|
+
|
91
|
+
def drop_validations(tag = :defaults)
|
92
|
+
drop_callbacks(tag, VALIDATION)
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def execute_callback_blocks(tag, type)
|
98
|
+
helper = TestFriendlyHelper.get_helper_for(tag, type)
|
99
|
+
return false if helper.unprocessed_procs.empty?
|
100
|
+
before = callbacks_hashes
|
101
|
+
helper.unprocessed_procs.each do |proc|
|
102
|
+
proc.call
|
103
|
+
end
|
104
|
+
after = callbacks_hashes
|
105
|
+
diff = after - before
|
106
|
+
helper.tagged_callbacks << diff
|
107
|
+
helper.optimize_tagged_callbacks
|
108
|
+
add_model_callbacks(type)
|
109
|
+
helper.unprocessed_procs = []
|
110
|
+
!diff.empty?
|
111
|
+
end
|
112
|
+
|
113
|
+
def callbacks_hashes
|
114
|
+
hashes = []
|
115
|
+
@runner_symbols.each do |runner_symbol|
|
116
|
+
hashes << self.send("_#{runner_symbol}_callbacks").map(&:hash)
|
117
|
+
end
|
118
|
+
hashes.flatten
|
119
|
+
end
|
120
|
+
|
121
|
+
def add_model_callbacks(type)
|
122
|
+
callbacks_hashes = @model_callbacks.map(&:hash)
|
123
|
+
@runner_symbols.each do |runner_symbol|
|
124
|
+
callbacks = self.send("_#{runner_symbol}_callbacks")
|
125
|
+
callbacks.each do |callback|
|
126
|
+
if !callbacks_hashes.include?(callback.hash)
|
127
|
+
@model_callbacks << ModelCallback.new(callback, callback.hash, runner_symbol, type)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
if defined?(ActiveRecord::Base)
|
136
|
+
ActiveRecord::Base.extend TestFriendly
|
137
|
+
end
|
138
|
+
|
139
|
+
if defined?(RSpec) && !TestFriendly::Global.callbacks_on?
|
140
|
+
RSpec.configure do |config|
|
141
|
+
config.before(:each) do
|
142
|
+
TestFriendly::Global.drop_validations(:all)
|
143
|
+
TestFriendly::Global.drop_callbacks(:all)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class TestFriendlyHelper
|
2
|
+
|
3
|
+
attr_accessor :unprocessed_procs, :tagged_callbacks
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@unprocessed_procs = []
|
7
|
+
@tagged_callbacks = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def optimize_tagged_callbacks
|
11
|
+
@tagged_callbacks.flatten!
|
12
|
+
@tagged_callbacks.uniq!
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.get_helper_for(tag, type)
|
16
|
+
@helpers ||= {}
|
17
|
+
@helpers[type] ||= {}
|
18
|
+
if !@helpers[type][tag]
|
19
|
+
@helpers[type][tag] = TestFriendlyHelper.new
|
20
|
+
end
|
21
|
+
@helpers[type][tag]
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User10< ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations do
|
7
|
+
validates_presence_of :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :new_attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class User11 < ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :executed_actions
|
5
|
+
|
6
|
+
test_friendly_callbacks do
|
7
|
+
set_callback :save, :before do |object|
|
8
|
+
object.set_executed('before_save')
|
9
|
+
end
|
10
|
+
set_callback :save, :after do |object|
|
11
|
+
object.set_executed('after_save')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def save
|
16
|
+
run_callbacks :save do
|
17
|
+
set_executed('save')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_executed(action)
|
22
|
+
@executed_actions ||= []
|
23
|
+
@executed_actions << action
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User4< ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations do
|
7
|
+
validates_presence_of :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :new_attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User5< ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations do
|
7
|
+
validates_presence_of :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :new_attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User6< ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations do
|
7
|
+
validates_presence_of :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :new_attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User8 < ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations(:additional) do
|
7
|
+
validates_presence_of :new_attribute
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :first_name
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class User9< ActiveRecord::Base
|
2
|
+
acts_as_test_friendly
|
3
|
+
|
4
|
+
attr_accessor :first_name, :last_name, :new_attribute
|
5
|
+
|
6
|
+
test_friendly_validations do
|
7
|
+
validates_presence_of :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test_friendly_validations(:additional) do
|
11
|
+
validates_presence_of :new_attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'test_friendly'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support'
|
3
|
+
|
4
|
+
class ActiveRecord
|
5
|
+
class Base;
|
6
|
+
include ::ActiveModel::Validations
|
7
|
+
include ::ActiveSupport::Callbacks
|
8
|
+
define_callbacks :save
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Rails
|
13
|
+
def self.env
|
14
|
+
'test'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
19
|
+
|
20
|
+
class User
|
21
|
+
extend TestFriendly
|
22
|
+
end
|
23
|
+
|
24
|
+
class Person < ActiveRecord::Base
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "TestFriendly" do
|
28
|
+
|
29
|
+
it "should be able to enable acts_as_test_friendly ability" do
|
30
|
+
User.should_not be_test_friendly
|
31
|
+
User.acts_as_test_friendly
|
32
|
+
User.should be_test_friendly
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be included in ActiveRecord::Base if it's present" do
|
36
|
+
Person.acts_as_test_friendly
|
37
|
+
Person.should be_test_friendly
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be not adding validations if Rails.env is test" do
|
41
|
+
require 'models/user1'
|
42
|
+
user = User1.new
|
43
|
+
user.should be_valid
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be able to turn validations on" do
|
47
|
+
require 'models/user2'
|
48
|
+
User2.force_validations
|
49
|
+
user = User2.new
|
50
|
+
user.should_not be_valid
|
51
|
+
user.first_name = 'First name'
|
52
|
+
user.last_name = 'Last name'
|
53
|
+
user.should be_valid
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should be able to drop validations" do
|
57
|
+
Rails.stub(:env => 'development')
|
58
|
+
require 'models/user3'
|
59
|
+
user = User3.new
|
60
|
+
user.should_not be_valid
|
61
|
+
User3.drop_validations
|
62
|
+
user.should be_valid
|
63
|
+
User3.force_validations
|
64
|
+
user.should_not be_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be able to turn on validations using tag" do
|
68
|
+
require 'models/user4'
|
69
|
+
User4.force_validations(:additional)
|
70
|
+
user = User4.new
|
71
|
+
user.new_attribute = 'some value'
|
72
|
+
user.should be_valid
|
73
|
+
User4.force_validations
|
74
|
+
user.should_not be_valid
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should be able to drop validations using tag" do
|
78
|
+
Rails.stub(:env => 'development')
|
79
|
+
require 'models/user5'
|
80
|
+
user = User5.new
|
81
|
+
user.first_name = 'Vasja'
|
82
|
+
user.last_name = 'Pupkin'
|
83
|
+
user.should_not be_valid
|
84
|
+
User5.drop_validations(:additional)
|
85
|
+
user.should be_valid
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be able to drop and recover all validations" do
|
89
|
+
Rails.stub(:env => 'development')
|
90
|
+
require 'models/user6'
|
91
|
+
User6.drop_validations(:all)
|
92
|
+
user = User6.new
|
93
|
+
user.should be_valid
|
94
|
+
User6.force_validations(:all)
|
95
|
+
user.first_name = 'first_name'
|
96
|
+
user.last_name = 'last_name'
|
97
|
+
user.should_not be_valid
|
98
|
+
user.new_attribute = 'some attribute'
|
99
|
+
user.should be_valid
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should not add number of callbacks if validations were forced 2 times" do
|
103
|
+
Rails.stub(:env => 'development')
|
104
|
+
require 'models/user7'
|
105
|
+
User7.force_validations
|
106
|
+
User7.force_validations
|
107
|
+
User7._validate_callbacks.length == 1
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be able to add different validations using same tag" do
|
111
|
+
Rails.stub(:env => 'development')
|
112
|
+
require 'models/user8'
|
113
|
+
User8._validate_callbacks.length == 2
|
114
|
+
User8.drop_validations(:additional)
|
115
|
+
User8._validate_callbacks.length == 0
|
116
|
+
User8.force_validations(:additional)
|
117
|
+
User8._validate_callbacks.length == 2
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should be able to force and drop all validations for all models" do
|
121
|
+
Rails.stub(:env => 'development')
|
122
|
+
require 'models/user9'
|
123
|
+
require 'models/user10'
|
124
|
+
user1 = User9.new
|
125
|
+
user2 = User10.new
|
126
|
+
user1.should_not be_valid
|
127
|
+
user2.should_not be_valid
|
128
|
+
TestFriendly::Global.drop_validations(:all)
|
129
|
+
user1.should be_valid
|
130
|
+
user2.should be_valid
|
131
|
+
TestFriendly::Global.force_validations(:all)
|
132
|
+
user1.should_not be_valid
|
133
|
+
user2.should_not be_valid
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should be able to force/drop callbacks" do
|
137
|
+
require 'models/user11'
|
138
|
+
user = User11.new
|
139
|
+
user.save
|
140
|
+
user.executed_actions.should =~ ['save']
|
141
|
+
User11.force_callbacks
|
142
|
+
user.executed_actions = []
|
143
|
+
user.save
|
144
|
+
user.executed_actions.should =~ ['save', 'before_save', 'after_save']
|
145
|
+
TestFriendly::Global.drop_callbacks(:all)
|
146
|
+
user.executed_actions = []
|
147
|
+
user.save
|
148
|
+
user.executed_actions.should =~ ['save']
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should not require acts_as_test_friendly to force validatons" do
|
152
|
+
require 'models/user12'
|
153
|
+
user = User12.new
|
154
|
+
user.should be_valid
|
155
|
+
User12.force_validations
|
156
|
+
user.should_not be_valid
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "test_friendly"
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andriy Romanov"]
|
12
|
+
s.date = "2012-05-03"
|
13
|
+
s.description = "Gem for turning off validations and callbacks in test environment"
|
14
|
+
s.email = "aromanov@partnerpedia.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/global.rb",
|
29
|
+
"lib/test_friendly.rb",
|
30
|
+
"lib/test_friendly_helper.rb",
|
31
|
+
"spec/models/user1.rb",
|
32
|
+
"spec/models/user10.rb",
|
33
|
+
"spec/models/user11.rb",
|
34
|
+
"spec/models/user2.rb",
|
35
|
+
"spec/models/user3.rb",
|
36
|
+
"spec/models/user4.rb",
|
37
|
+
"spec/models/user5.rb",
|
38
|
+
"spec/models/user6.rb",
|
39
|
+
"spec/models/user7.rb",
|
40
|
+
"spec/models/user8.rb",
|
41
|
+
"spec/models/user9.rb",
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/test_friendly_spec.rb",
|
44
|
+
"test_friendly.gemspec"
|
45
|
+
]
|
46
|
+
s.homepage = "http://github.com/romanoff/test_friendly"
|
47
|
+
s.licenses = ["MIT"]
|
48
|
+
s.require_paths = ["lib"]
|
49
|
+
s.rubygems_version = "1.8.23"
|
50
|
+
s.summary = "Gem for turning off validations and callbacks in test environment"
|
51
|
+
|
52
|
+
if s.respond_to? :specification_version then
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
57
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
58
|
+
s.add_runtime_dependency(%q<debugger>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
65
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
66
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
67
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
68
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
69
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
74
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
75
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
76
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
77
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
78
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
79
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_friendly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andriy Romanov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: debugger
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rdoc
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: jeweler
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.8.3
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.8.3
|
126
|
+
description: Gem for turning off validations and callbacks in test environment
|
127
|
+
email: aromanov@partnerpedia.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files:
|
131
|
+
- LICENSE.txt
|
132
|
+
- README.rdoc
|
133
|
+
files:
|
134
|
+
- .document
|
135
|
+
- .rspec
|
136
|
+
- Gemfile
|
137
|
+
- Gemfile.lock
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.rdoc
|
140
|
+
- Rakefile
|
141
|
+
- VERSION
|
142
|
+
- lib/global.rb
|
143
|
+
- lib/test_friendly.rb
|
144
|
+
- lib/test_friendly_helper.rb
|
145
|
+
- spec/models/user1.rb
|
146
|
+
- spec/models/user10.rb
|
147
|
+
- spec/models/user11.rb
|
148
|
+
- spec/models/user2.rb
|
149
|
+
- spec/models/user3.rb
|
150
|
+
- spec/models/user4.rb
|
151
|
+
- spec/models/user5.rb
|
152
|
+
- spec/models/user6.rb
|
153
|
+
- spec/models/user7.rb
|
154
|
+
- spec/models/user8.rb
|
155
|
+
- spec/models/user9.rb
|
156
|
+
- spec/spec_helper.rb
|
157
|
+
- spec/test_friendly_spec.rb
|
158
|
+
- test_friendly.gemspec
|
159
|
+
homepage: http://github.com/romanoff/test_friendly
|
160
|
+
licenses:
|
161
|
+
- MIT
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
none: false
|
168
|
+
requirements:
|
169
|
+
- - ! '>='
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ! '>='
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 1.8.23
|
181
|
+
signing_key:
|
182
|
+
specification_version: 3
|
183
|
+
summary: Gem for turning off validations and callbacks in test environment
|
184
|
+
test_files: []
|