evelpidon_test_helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/.rvmrc +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/Rakefile +1 -0
- data/evelpidon_test_helpers.gemspec +25 -0
- data/lib/evelpidon_test_helpers/action_controller.rb +19 -0
- data/lib/evelpidon_test_helpers/active_model.rb +120 -0
- data/lib/evelpidon_test_helpers/date.rb +14 -0
- data/lib/evelpidon_test_helpers/sunspot.rb +53 -0
- data/lib/evelpidon_test_helpers/version.rb +3 -0
- data/lib/evelpidon_test_helpers.rb +10 -0
- metadata +129 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create @evelpidon_test_helpers
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyrignt (c) 2010 Kriss 'nu7hatch' Kowalik
|
2
|
+
Copyright (c) 2009-2010 BehindLogic
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following 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 OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Evelpidon Test Helpers for Rails projects
|
2
|
+
|
3
|
+
Collection of various Test::Unit / ActiveSupport::Test helpers, mainly for Rails projects.
|
4
|
+
|
5
|
+
## Author(s)
|
6
|
+
|
7
|
+
* [Nikos Dimitrakopoulos](http://github.com/nikosd)
|
8
|
+
* [Panayotis Matsinopoulos](http://github.com/pmatsinopoulos)
|
9
|
+
* [Eric Cohen](http://github.com/eirc)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
You can install it easy using rubygems:
|
14
|
+
|
15
|
+
gem install evelpidon_test_helpers
|
16
|
+
|
17
|
+
Or using bundler
|
18
|
+
|
19
|
+
gem 'evelpidon_test_helpers'
|
20
|
+
|
21
|
+
## Features
|
22
|
+
|
23
|
+
* ActiveModel helpers
|
24
|
+
* ActionController helpers
|
25
|
+
* Generic helpers (for example for Date/Time objects)
|
26
|
+
* Sunspot mocking
|
27
|
+
|
28
|
+
## TODOs
|
29
|
+
|
30
|
+
* Generic documentation for usage
|
31
|
+
* Specific code documentation
|
32
|
+
* Tests (?)
|
33
|
+
|
34
|
+
## Note on Patches/Pull Requests
|
35
|
+
|
36
|
+
* Fork the project.
|
37
|
+
* Make your feature addition or bug fix.
|
38
|
+
* Add tests for it. This is important so I don't break it in a
|
39
|
+
future version unintentionally (not really...).
|
40
|
+
* Commit, do not mess with gemspec, version, or history.
|
41
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
42
|
+
* Send me a pull request. Bonus points for topic branches.
|
43
|
+
|
44
|
+
## Copyright
|
45
|
+
|
46
|
+
* Copyrignt (c) 2011 Fraudpointer.com
|
47
|
+
* Copyrignt (c) 2011 E-Travel S.A.
|
48
|
+
|
49
|
+
See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "evelpidon_test_helpers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "evelpidon_test_helpers"
|
7
|
+
s.version = EvelpidonTestHelpers::VERSION
|
8
|
+
s.authors = ["Nikos Dimitrakopoulos", "Panayotis Matsinopoulos", "Eric Cohen"]
|
9
|
+
s.email = ["n.dimitrakopoulos@pamediakopes.gr", "p.matsinopoulos@fraudpointer.com", "e.koen@pamediakopes.gr"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Various test helpers for Rails projects}
|
12
|
+
s.description = ""
|
13
|
+
|
14
|
+
s.rubyforge_project = "evelpidon_test_helpers"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "activemodel", ">=3.0"
|
23
|
+
s.add_development_dependency "actionpack", ">=3.0"
|
24
|
+
s.add_runtime_dependency "activesupport", ">=3.0"
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module EvelpidonTestHelpers
|
2
|
+
module ActionController
|
3
|
+
# Asserts that the controller assigned an instance variable with the given +instance_variable_name+
|
4
|
+
# and optionally checks that it's 'is equal with the given +expected_value+.
|
5
|
+
def assert_assigns(instance_variable_name, expected_value = nil)
|
6
|
+
assert_not_nil assigns(instance_variable_name), "#{instance_variable_name} was not assigned."
|
7
|
+
unless expected_value.nil?
|
8
|
+
assert_equal expected_value, assigns(instance_variable_name)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
module ActionController
|
16
|
+
class TestCase
|
17
|
+
include EvelpidonTestHelpers::ActionController
|
18
|
+
end
|
19
|
+
end if defined?(ActionController)
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module EvelpidonTestHelpers
|
2
|
+
# +ActiveModel+ related helpers
|
3
|
+
module ActiveModel
|
4
|
+
# Asserts that the given ++ActiveModel++ is "valid".
|
5
|
+
# If not, the error message is the full error messages.
|
6
|
+
def assert_valid(object, additional_message = nil)
|
7
|
+
is_valid = object.valid?
|
8
|
+
error_message = additional_message ?
|
9
|
+
"#{additional_message}\n\nErrors:\n\n#{object.errors.full_messages.join("\n")}\n\n" :
|
10
|
+
"Errors:\n#{object.errors.full_messages.join("\n")}\n\n"
|
11
|
+
assert is_valid, error_message
|
12
|
+
end
|
13
|
+
|
14
|
+
# Asserts that the given +attribute+ on the given +ActiveModel+ contains no errors.
|
15
|
+
# If not, the error message is the joint string of the errors on this +attribute+.
|
16
|
+
def assert_valid_attribute(object, attribute, message = "")
|
17
|
+
object.valid?
|
18
|
+
errors_on_attribute = object.errors[attribute].length
|
19
|
+
error_messages = object.errors[attribute].join(',')
|
20
|
+
message << "\nExpected zero errors on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
|
21
|
+
assert_equal 0, errors_on_attribute, message
|
22
|
+
end
|
23
|
+
|
24
|
+
# Asserts that the given ++ActiveModel++ is "invalid".
|
25
|
+
# The ++attributes_with_errors++ options should a hash of attributes to be specifically
|
26
|
+
# examined for having errors. For example : {:email => 1, :username => 2} (etc).
|
27
|
+
def assert_invalid(object, attributes_with_errors = {})
|
28
|
+
assert object.invalid?, "Expected #{object} to be invalid, but was actually valid"
|
29
|
+
|
30
|
+
attributes_with_errors.each do |attribute, expected_number_of_errors|
|
31
|
+
actual_errors_on_attribute = object.errors[attribute].length
|
32
|
+
error_message = "Expected #{expected_number_of_errors} errors on #{attribute}, but were actually #{actual_errors_on_attribute} : \n"
|
33
|
+
error_message << "#{object.errors[attribute].join("\n")}"
|
34
|
+
assert_equal expected_number_of_errors, actual_errors_on_attribute, error_message
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Asserts that the given +attribute+ on the given +ActiveModel+ contains at least one error.
|
39
|
+
def assert_invalid_attribute(object, attribute, message = "")
|
40
|
+
object.valid?
|
41
|
+
errors_on_attribute = object.errors[attribute].length
|
42
|
+
error_messages = object.errors[attribute].join(',')
|
43
|
+
message << "\nExpected at least one error on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
|
44
|
+
assert errors_on_attribute > 0, message
|
45
|
+
end
|
46
|
+
|
47
|
+
# Asserts that the given +attributes+ result to a valid +ActiveModel+ instance.
|
48
|
+
#
|
49
|
+
# This helper chooses the model name based on the current test name, so for example
|
50
|
+
# when the test that is running is named FooTest it will try to instantiate a new Foo
|
51
|
+
# model, update the attributes (by-passing mass assignment protection) and then call
|
52
|
+
# +assert_valid+ on it.
|
53
|
+
def assert_valid_model_attributes(attributes)
|
54
|
+
model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
|
55
|
+
instance = model.new
|
56
|
+
|
57
|
+
attributes.each do |attribute_name, attribute_value|
|
58
|
+
instance.send("#{attribute_name}=", attribute_value)
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_valid instance, instance.inspect
|
62
|
+
end
|
63
|
+
|
64
|
+
# Asserts that all the records for the current model under test are valid.
|
65
|
+
#
|
66
|
+
# This helper chooses the model name based on the current test name, so for example
|
67
|
+
# when the test that is running is named FooTest it will try to load all the records of Foo
|
68
|
+
# through Foo.all and then call +assert_valid+ on each one of them.
|
69
|
+
def assert_valid_fixtures
|
70
|
+
model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
|
71
|
+
model.all.each do |fixture|
|
72
|
+
assert_valid fixture, fixture.inspect
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Asserts that the given +attribute+ on the given +model+ cannot be assigned through
|
77
|
+
# mass-assignment (like +update_attributes).
|
78
|
+
#
|
79
|
+
# @param model [ActiveModel] A properly initialized instance of the class that we want to test.
|
80
|
+
# @param attribute [Symbol] The attribute that is protected
|
81
|
+
# @param value [Object] An optional value to use when trying to assign the attribute.
|
82
|
+
def assert_attribute_protected(model, attribute, value = "foo")
|
83
|
+
model.send("#{attribute}=", nil)
|
84
|
+
model.attributes = {attribute => value}
|
85
|
+
assert_nil model.send(attribute), "#{attribute} could be set through 'update_attributes' call"
|
86
|
+
|
87
|
+
model.send("#{attribute}=", value)
|
88
|
+
assert_equal value, model.send(attribute)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Asserts that the given +observer+ class will receive the +notification+.
|
92
|
+
#
|
93
|
+
# @param observer [Class] the observer class
|
94
|
+
# @param notification [Symbol] the notification
|
95
|
+
# @param options [Hash] extra options :
|
96
|
+
# * :object [Object, Mocha::ParameterMatchers::Base] the object that should be passed to the observer.
|
97
|
+
# * :times [Integer] the number of times that the notification should be sent to the observer.
|
98
|
+
#
|
99
|
+
def assert_observer_notified(observer, notification, options = {})
|
100
|
+
options.reverse_merge!(:times => 1, :object => anything)
|
101
|
+
observer.instance.expects(notification).with(options[:object]).times(options[:times])
|
102
|
+
end
|
103
|
+
|
104
|
+
# Asserts that the given +observer+ class will never receive the +notification+.
|
105
|
+
#
|
106
|
+
# @param observer [Class] the observer class
|
107
|
+
# @param notification [Symbol] the notification
|
108
|
+
# @param options [Hash] extra options
|
109
|
+
#
|
110
|
+
def assert_observer_not_notified(observer, notification, options = {})
|
111
|
+
observer.instance.expects(notification).never
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
module ActiveSupport
|
117
|
+
class TestCase
|
118
|
+
include EvelpidonTestHelpers::ActiveModel
|
119
|
+
end
|
120
|
+
end if defined?(ActiveModel)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module EvelpidonTestHelpers
|
2
|
+
module Date
|
3
|
+
# Asserts that the two ++Time++ or ++DateTime++ refer to same Date/Month/Year.
|
4
|
+
def assert_equal_date(expected, actual)
|
5
|
+
assert_equal expected.strftime("%j%Y"), actual.strftime("%j%Y")
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveSupport
|
11
|
+
class TestCase
|
12
|
+
include EvelpidonTestHelpers::Date
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module EvelpidonTestHelpers
|
2
|
+
##
|
3
|
+
# Stubbing out Solr
|
4
|
+
#
|
5
|
+
# Taken from : http://timcowlishaw.co.uk/post/3179661158/testing-sunspot-with-test-unit
|
6
|
+
module TestSunspot
|
7
|
+
class << self
|
8
|
+
attr_accessor :pid, :original_session, :stub_session, :server
|
9
|
+
|
10
|
+
def setup
|
11
|
+
TestSunspot.original_session = Sunspot.session
|
12
|
+
Sunspot.session = TestSunspot.stub_session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.included(klass)
|
17
|
+
klass.instance_eval do
|
18
|
+
def startup
|
19
|
+
Sunspot.session = TestSunspot.original_session
|
20
|
+
rd, wr = IO.pipe
|
21
|
+
pid = fork do
|
22
|
+
STDOUT.reopen(wr)
|
23
|
+
STDERR.reopen(wr)
|
24
|
+
TestSunspot.server ||= Sunspot::Rails::Server.new
|
25
|
+
begin
|
26
|
+
TestSunspot.server.run
|
27
|
+
ensure
|
28
|
+
wr.close
|
29
|
+
end
|
30
|
+
end
|
31
|
+
TestSunspot.pid = pid
|
32
|
+
ready = false
|
33
|
+
until ready do
|
34
|
+
ready = true if rd.gets =~ /Started\ SocketConnector/
|
35
|
+
sleep 0.5
|
36
|
+
end
|
37
|
+
rd.close
|
38
|
+
end
|
39
|
+
|
40
|
+
def shutdown
|
41
|
+
Sunspot.remove_all!
|
42
|
+
Process.kill("HUP",TestSunspot.pid)
|
43
|
+
Process.wait
|
44
|
+
Sunspot.session = TestSunspot.stub_session
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def teardown
|
49
|
+
Sunspot.remove_all!
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "evelpidon_test_helpers/version"
|
2
|
+
|
3
|
+
module EvelpidonTestHelpers
|
4
|
+
end
|
5
|
+
|
6
|
+
# TODO : Possibly stop requiring these here and let the gem user decide what he wants to use.
|
7
|
+
require "evelpidon_test_helpers/date"
|
8
|
+
require "evelpidon_test_helpers/active_model"
|
9
|
+
require "evelpidon_test_helpers/action_controller"
|
10
|
+
require "evelpidon_test_helpers/sunspot"
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evelpidon_test_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Nikos Dimitrakopoulos
|
14
|
+
- Panayotis Matsinopoulos
|
15
|
+
- Eric Cohen
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2011-10-13 00:00:00 +03:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: activemodel
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 7
|
32
|
+
segments:
|
33
|
+
- 3
|
34
|
+
- 0
|
35
|
+
version: "3.0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: actionpack
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 7
|
47
|
+
segments:
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
version: "3.0"
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: activesupport
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 7
|
62
|
+
segments:
|
63
|
+
- 3
|
64
|
+
- 0
|
65
|
+
version: "3.0"
|
66
|
+
type: :runtime
|
67
|
+
version_requirements: *id003
|
68
|
+
description: ""
|
69
|
+
email:
|
70
|
+
- n.dimitrakopoulos@pamediakopes.gr
|
71
|
+
- p.matsinopoulos@fraudpointer.com
|
72
|
+
- e.koen@pamediakopes.gr
|
73
|
+
executables: []
|
74
|
+
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files: []
|
78
|
+
|
79
|
+
files:
|
80
|
+
- .gitignore
|
81
|
+
- .rvmrc
|
82
|
+
- CHANGELOG.md
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- evelpidon_test_helpers.gemspec
|
88
|
+
- lib/evelpidon_test_helpers.rb
|
89
|
+
- lib/evelpidon_test_helpers/action_controller.rb
|
90
|
+
- lib/evelpidon_test_helpers/active_model.rb
|
91
|
+
- lib/evelpidon_test_helpers/date.rb
|
92
|
+
- lib/evelpidon_test_helpers/sunspot.rb
|
93
|
+
- lib/evelpidon_test_helpers/version.rb
|
94
|
+
has_rdoc: true
|
95
|
+
homepage: ""
|
96
|
+
licenses: []
|
97
|
+
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 3
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
requirements: []
|
122
|
+
|
123
|
+
rubyforge_project: evelpidon_test_helpers
|
124
|
+
rubygems_version: 1.6.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Various test helpers for Rails projects
|
128
|
+
test_files: []
|
129
|
+
|