Fingertips-on-test-spec 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README +12 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/dot.autotest +93 -0
- data/lib/test/spec/add_allow_switch.rb +53 -0
- data/lib/test/spec/rails/controller_helpers.rb +40 -0
- data/lib/test/spec/rails/expectations.rb +196 -0
- data/lib/test/spec/rails/macros/authorization.rb +65 -0
- data/lib/test/spec/rails/macros.rb +78 -0
- data/lib/test/spec/rails/request_helpers.rb +11 -0
- data/lib/test/spec/rails/response_helpers.rb +42 -0
- data/lib/test/spec/rails/spec_responder.rb +12 -0
- data/lib/test/spec/rails/test_spec_ext.rb +23 -0
- data/lib/test/spec/rails.rb +41 -0
- data/lib/test/spec/share.rb +48 -0
- data/on-test-spec.gemspec +69 -0
- data/test/add_allow_switch_test.rb +104 -0
- data/test/expectations_test.rb +467 -0
- data/test/macros/authorization_test.rb +80 -0
- data/test/macros/base_test.rb +78 -0
- data/test/share_test.rb +52 -0
- data/test/test_helper.rb +8 -0
- metadata +85 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008, Fingertips
|
2
|
+
- Manfred Stienstra <manfred@fngtps.com>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
6
|
+
in the Software without restriction, including without limitation the rights
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
12
|
+
all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
On Test Spec
|
2
|
+
------------
|
3
|
+
|
4
|
+
Makes testing Rails apps with test/spec easier and more fun. Created with a
|
5
|
+
half eye on the test_spec_on_rails plugin, thanks!
|
6
|
+
|
7
|
+
Autotest
|
8
|
+
--------
|
9
|
+
|
10
|
+
To use autotest with test/spec on Rails, copy the "dot.autotest" sample file to
|
11
|
+
your project's root directory and name it ".autotest". Edit the file if you
|
12
|
+
need any custom mappings.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
desc "By default run all tests"
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.test_files = FileList['test/**/*_test.rb']
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |s|
|
15
|
+
s.name = "on-test-spec"
|
16
|
+
s.homepage = "http://github.com/Fingertips/on-test-spec"
|
17
|
+
s.email = "eloy.de.enige@gmail.com"
|
18
|
+
s.authors = ["Manfred Stienstra", "Eloy Duran", "Cristi Balan"]
|
19
|
+
s.summary = s.description = "Rails plugin to make testing Rails on test/spec easier."
|
20
|
+
end
|
21
|
+
rescue LoadError
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'jewelry_portfolio/tasks'
|
26
|
+
JewelryPortfolio::Tasks.new do |p|
|
27
|
+
p.account = 'Fingertips'
|
28
|
+
end
|
29
|
+
rescue LoadError
|
30
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.2
|
data/dot.autotest
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
Autotest.add_hook :initialize do |at|
|
2
|
+
at.unit_diff = 'cat'
|
3
|
+
at.failed_results_re = /^\s+\d+\) (?:Failure|Error):\n(.*?)\((.*?)\)\n\[([^:]*):.*\]/
|
4
|
+
|
5
|
+
at.add_exception %r%^\./(?:db|doc|log|public|script|tmp|vendor|data|content|config)%
|
6
|
+
at.add_exception %r%\.svn%
|
7
|
+
|
8
|
+
at.clear_mappings
|
9
|
+
|
10
|
+
# Add your custom mappings:
|
11
|
+
#
|
12
|
+
# at.add_mapping %r%^app/(concerns)/(.*)\.rb$% do |_, match|
|
13
|
+
# "test/unit/#{match[1]}/#{match[2]}_test.rb"
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# at.add_mapping %r%^test/unit/(concerns)/.*rb$% do |filename, _|
|
17
|
+
# filename
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# at.add_mapping %r%^lib/(.*).rb% do |_, match|
|
21
|
+
# sqwat = match[1].gsub('/', '_')
|
22
|
+
# "test/lib/#{sqwat}_test.rb"
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# at.add_mapping %r%^test/lib/.*rb$% do |filename, _|
|
26
|
+
# filename
|
27
|
+
# end
|
28
|
+
|
29
|
+
# Standard Rails
|
30
|
+
|
31
|
+
at.add_mapping %r%^test/fixtures/(.*)s.yml% do |_, m|
|
32
|
+
["test/unit/#{m[1]}_test.rb",
|
33
|
+
"test/controllers/#{m[1]}_controller_test.rb",
|
34
|
+
"test/views/#{m[1]}_view_test.rb",
|
35
|
+
"test/functional/#{m[1]}_controller_test.rb"]
|
36
|
+
end
|
37
|
+
|
38
|
+
at.add_mapping %r%^test/(unit|integration|controllers|views|functional|helpers)/.*rb$% do |filename, _|
|
39
|
+
filename
|
40
|
+
end
|
41
|
+
|
42
|
+
at.add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
|
43
|
+
"test/unit/#{m[1]}_test.rb"
|
44
|
+
end
|
45
|
+
|
46
|
+
at.add_mapping %r%^app/helpers/application_helper.rb% do
|
47
|
+
["test/helpers/applications_helper_test.rb"] + at.files_matching(%r%^test/(views|functional)/.*_test\.rb$%)
|
48
|
+
end
|
49
|
+
|
50
|
+
at.add_mapping %r%^app/helpers/(.*)_helper.rb% do |_, m|
|
51
|
+
if m[1] == "application" then
|
52
|
+
at.files_matching %r%^test/(views|functional)/.*_test\.rb$%
|
53
|
+
else
|
54
|
+
["test/helpers/#{m[1]}_helper_test.rb",
|
55
|
+
"test/views/#{m[1]}_view_test.rb",
|
56
|
+
"test/functional/#{m[1]}_controller_test.rb"]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
at.add_mapping %r%^app/views/(.*)/% do |_, m|
|
61
|
+
["test/views/#{m[1]}_view_test.rb",
|
62
|
+
"test/functional/#{m[1]}_controller_test.rb"]
|
63
|
+
end
|
64
|
+
|
65
|
+
at.add_mapping %r%^app/controllers/(.*)\.rb$% do |_, m|
|
66
|
+
if m[1] == "application" then
|
67
|
+
at.files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
|
68
|
+
else
|
69
|
+
["test/controllers/#{m[1]}_test.rb",
|
70
|
+
"test/functional/#{m[1]}_test.rb"]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
at.add_mapping %r%^app/views/layouts/% do
|
75
|
+
"test/views/layouts_view_test.rb"
|
76
|
+
end
|
77
|
+
|
78
|
+
at.add_mapping %r%^config/routes.rb$% do
|
79
|
+
at.files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
|
80
|
+
end
|
81
|
+
|
82
|
+
at.add_mapping %r%^test/test_helper.rb|config/((boot|environment(s/test)?).rb|database.yml)% do
|
83
|
+
at.files_matching %r%^test/(unit|controllers|views|functional)/.*_test\.rb$%
|
84
|
+
end
|
85
|
+
|
86
|
+
class << at
|
87
|
+
def consolidate_failures(failed)
|
88
|
+
failed.inject(new_hash_of_arrays) do |filters, (method, klass, filename)|
|
89
|
+
filters[File.expand_path(filename)] << method; filters
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
class Class
|
4
|
+
def add_allow_switch(method, options={})
|
5
|
+
default = options[:default] || false
|
6
|
+
|
7
|
+
class_eval do
|
8
|
+
cattr_accessor "allow_#{method}"
|
9
|
+
self.send("allow_#{method}=", default)
|
10
|
+
|
11
|
+
alias_method "original_#{method}", method
|
12
|
+
|
13
|
+
eval %{
|
14
|
+
def #{method}(*args)
|
15
|
+
if allow_#{method}
|
16
|
+
original_#{method}(*args)
|
17
|
+
else
|
18
|
+
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
}, binding, __FILE__, __LINE__
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Module
|
27
|
+
def add_allow_switch(method, options={})
|
28
|
+
default = options[:default] || false
|
29
|
+
|
30
|
+
mattr_accessor "allow_#{method}"
|
31
|
+
send("allow_#{method}=", default)
|
32
|
+
|
33
|
+
unless respond_to?(:__metaclass___)
|
34
|
+
def __metaclass__
|
35
|
+
class << self; self; end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
__metaclass__.class_eval do
|
40
|
+
alias_method "original_#{method}", method
|
41
|
+
|
42
|
+
eval %{
|
43
|
+
def #{method}(*args)
|
44
|
+
if allow_#{method}
|
45
|
+
original_#{method}(*args)
|
46
|
+
else
|
47
|
+
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
|
48
|
+
end
|
49
|
+
end
|
50
|
+
}, binding, __FILE__, __LINE__
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'action_controller/test_case'
|
2
|
+
|
3
|
+
module Test
|
4
|
+
module Spec
|
5
|
+
module Rails
|
6
|
+
module Controller
|
7
|
+
module ClassMethods
|
8
|
+
# Sets up the test environment before every functional test
|
9
|
+
def tests(controller_class)
|
10
|
+
setups << lambda { setup_request_environment(controller_class) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module InstanceMethods
|
14
|
+
include ActionController::TestProcess
|
15
|
+
include ActionController::TestCase::Assertions
|
16
|
+
|
17
|
+
attr_reader :controller
|
18
|
+
|
19
|
+
# Sets up the test environment for functional tests
|
20
|
+
def setup_request_environment(controller_class)
|
21
|
+
controller_class.class_eval do
|
22
|
+
def rescue_action(e)
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@controller = controller_class.new
|
27
|
+
@controller.request = @request = ActionController::TestRequest.new
|
28
|
+
@response = ActionController::TestResponse.new
|
29
|
+
|
30
|
+
@controller.params = {}
|
31
|
+
@controller.send(:initialize_current_url)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ActiveSupport::TestCase.send(:extend, Test::Spec::Rails::Controller::ClassMethods)
|
40
|
+
ActiveSupport::TestCase.send(:include, Test::Spec::Rails::Controller::InstanceMethods)
|
@@ -0,0 +1,196 @@
|
|
1
|
+
module Test
|
2
|
+
module Spec
|
3
|
+
module Rails
|
4
|
+
module Helpers
|
5
|
+
def self.inspect_records(records)
|
6
|
+
"[#{records.map { |record| "#{record.class}[#{record.id}]" }.join(', ')}]"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ShouldExpectations
|
11
|
+
include ActiveSupport::Testing::Assertions
|
12
|
+
|
13
|
+
# Test that we were redirected somewhere:
|
14
|
+
# should.redirect
|
15
|
+
#
|
16
|
+
# Test that we were redirected to a specific url:
|
17
|
+
# should.redirect :controller => 'foo', :action => 'bar'
|
18
|
+
# or:
|
19
|
+
# should.redirect_to :controller => 'foo', :action => 'bar', :secure => true
|
20
|
+
def redirect(*args)
|
21
|
+
if args.empty?
|
22
|
+
assert_response @object.response.redirected_to, :redirect
|
23
|
+
elsif args.length == 1 and args.first.is_a?(String)
|
24
|
+
assert_equal args.first, @object.response.redirected_to
|
25
|
+
else
|
26
|
+
options = args.extract_options!
|
27
|
+
if secure = options.delete(:secure)
|
28
|
+
unless secure == true or secure == false
|
29
|
+
raise ArgumentError, ":secure option should be a boolean"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@object.instance_eval { assert_redirected_to *args }
|
34
|
+
if secure == true
|
35
|
+
assert @object.response.redirected_to.starts_with?('https:')
|
36
|
+
elsif secure == false
|
37
|
+
assert @object.response.redirected_to.starts_with?('http:')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
alias :redirect_to :redirect
|
42
|
+
|
43
|
+
# Tests whether a redirect back to the HTTP_REFERER was send.
|
44
|
+
#
|
45
|
+
# lambda { delete :destroy, :id => 1 }.should.redirect_back_to(articles_url)
|
46
|
+
# lambda { delete :destroy, :id => 1 }.should.redirect_back_to(:action => :index)
|
47
|
+
def redirect_back_to(url_options)
|
48
|
+
test_case = eval("self", @object.binding)
|
49
|
+
url = test_case.controller.url_for(url_options)
|
50
|
+
test_case.controller.request.env["HTTP_REFERER"] = url
|
51
|
+
|
52
|
+
block_result = @object.call
|
53
|
+
test_case.should.redirect_to(url)
|
54
|
+
|
55
|
+
block_result
|
56
|
+
end
|
57
|
+
|
58
|
+
# Test that the object is valid
|
59
|
+
def validate
|
60
|
+
assert_valid @object
|
61
|
+
end
|
62
|
+
|
63
|
+
# Tests whether the evaluation of the expression changes.
|
64
|
+
#
|
65
|
+
# lambda { Norm.create }.should.differ('Norm.count')
|
66
|
+
# lambda { Norm.create; Norm.create }.should.differ('Norm.count', +2)
|
67
|
+
# lambda { Norm.create; Option.create }.should.differ('Norm.count', +2, 'Option.count', +1)
|
68
|
+
#
|
69
|
+
# norm = lambda { Norm.create }.should.differ('Norm.count')
|
70
|
+
# norm.name.should == 'Latency'
|
71
|
+
def differ(*expected)
|
72
|
+
block_binding = @object.send(:binding)
|
73
|
+
before = expected.in_groups_of(2).map do |expression, _|
|
74
|
+
eval(expression, block_binding)
|
75
|
+
end
|
76
|
+
|
77
|
+
block_result = @object.call
|
78
|
+
|
79
|
+
expected.in_groups_of(2).each_with_index do |(expression, difference), index|
|
80
|
+
difference = 1 if difference.nil?
|
81
|
+
error = "#{expression.inspect} didn't change by #{difference}"
|
82
|
+
assert_equal(before[index] + difference, eval(expression, block_binding), error)
|
83
|
+
end
|
84
|
+
|
85
|
+
block_result
|
86
|
+
end
|
87
|
+
alias change differ
|
88
|
+
|
89
|
+
# Tests whether certain pages are cached.
|
90
|
+
#
|
91
|
+
# lambda { get :index }.should.cache_pages(posts_path)
|
92
|
+
# lambda { get :show, :id => post }.should.cache_pages(post_path(post), formatted_posts_path(:js, post))
|
93
|
+
def cache_pages(*pages, &block)
|
94
|
+
if block
|
95
|
+
block.call
|
96
|
+
else
|
97
|
+
@object.call
|
98
|
+
end
|
99
|
+
cache_dir = ActionController::Base.page_cache_directory
|
100
|
+
files = Dir.glob("#{cache_dir}/**/*").map do |filename|
|
101
|
+
filename[cache_dir.length..-1]
|
102
|
+
end
|
103
|
+
assert pages.all? { |page| files.include?(page) }
|
104
|
+
end
|
105
|
+
|
106
|
+
# Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)
|
107
|
+
def dom_equal(expected)
|
108
|
+
assert_dom_equal expected, @object
|
109
|
+
end
|
110
|
+
|
111
|
+
# Tests if the array of records is the same, order may vary
|
112
|
+
def equal_set(expected)
|
113
|
+
message = "#{Helpers.inspect_records(@object)} does not have the same records as #{Helpers.inspect_records(expected)}"
|
114
|
+
|
115
|
+
left = @object.map(&:id).sort
|
116
|
+
right = expected.map(&:id).sort
|
117
|
+
|
118
|
+
assert(left == right, message)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Tests if the array of records is the same, order must be the same
|
122
|
+
def equal_list(expected)
|
123
|
+
message = "#{Helpers.inspect_records(@object)} does not have the same records as #{Helpers.inspect_records(expected)}"
|
124
|
+
|
125
|
+
left = @object.map(&:id)
|
126
|
+
right = expected.map(&:id)
|
127
|
+
|
128
|
+
assert(left == right, message)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
module ShouldNotExpectations
|
133
|
+
include ActiveSupport::Testing::Assertions
|
134
|
+
|
135
|
+
# Test that an object is not valid
|
136
|
+
def validate
|
137
|
+
assert !@object.valid?
|
138
|
+
end
|
139
|
+
|
140
|
+
# Tests that the evaluation of the expression shouldn't change
|
141
|
+
#
|
142
|
+
# lambda { Norm.new }.should.not.differ('Norm.count')
|
143
|
+
# lambda { Norm.new }.should.not.differ('Norm.count', 'Option.count')
|
144
|
+
#
|
145
|
+
# norm = lambda { Norm.new }.should.not.differ('Norm.count')
|
146
|
+
# norm.token.should.match /(\d\w){4}/
|
147
|
+
def differ(*expected)
|
148
|
+
block_binding = @object.send(:binding)
|
149
|
+
before = expected.map do |expression|
|
150
|
+
eval(expression, block_binding)
|
151
|
+
end
|
152
|
+
|
153
|
+
block_result = @object.call
|
154
|
+
|
155
|
+
expected.each_with_index do |expression, index|
|
156
|
+
difference = eval(expression, block_binding) - before[index]
|
157
|
+
error = "#{expression.inspect} changed by #{difference}, expected no change"
|
158
|
+
assert_equal(0, difference, error)
|
159
|
+
end
|
160
|
+
|
161
|
+
block_result
|
162
|
+
|
163
|
+
end
|
164
|
+
alias change differ
|
165
|
+
|
166
|
+
# Test that two HTML strings are not equivalent
|
167
|
+
def dom_equal(expected)
|
168
|
+
assert_dom_not_equal expected, @object
|
169
|
+
end
|
170
|
+
|
171
|
+
# Tests if the array of records is not the same, order may vary
|
172
|
+
def equal_set(expected)
|
173
|
+
message = "#{Helpers.inspect_records(@object)} has the same records as #{Helpers.inspect_records(expected)}"
|
174
|
+
|
175
|
+
left = @object.map(&:id).sort
|
176
|
+
right = expected.map(&:id).sort
|
177
|
+
|
178
|
+
assert(left != right, message)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Tests if the array of records is not the same, order may vary
|
182
|
+
def equal_list(expected)
|
183
|
+
message = "#{Helpers.inspect_records(@object)} has the same records as #{Helpers.inspect_records(expected)}"
|
184
|
+
|
185
|
+
left = @object.map(&:id)
|
186
|
+
right = expected.map(&:id)
|
187
|
+
|
188
|
+
assert(left != right, message)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
Test::Spec::Should.send(:include, Test::Spec::Rails::ShouldExpectations)
|
196
|
+
Test::Spec::ShouldNot.send(:include, Test::Spec::Rails::ShouldNotExpectations)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Test
|
2
|
+
module Spec
|
3
|
+
module Rails
|
4
|
+
module Macros
|
5
|
+
class Should
|
6
|
+
# Generates a test which tests authorization code. It assumes a method called <code>access_denied?</code>
|
7
|
+
# on the test case. The <code>access_denied?</code> method should return true when access is denied
|
8
|
+
# (ie. a 403 status code) and false in other cases.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# should.disallow.get :index
|
12
|
+
def disallow
|
13
|
+
Test::Spec::Rails::Macros::Authorization::TestGenerator.new(test_case,
|
14
|
+
:access_denied?,
|
15
|
+
'Expected access to be denied'
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Generates a test which tests authorization code. It assumes a method called <code>access_denied?</code>
|
20
|
+
# on the test case. The <code>login_required?</code> method should return true when the visitor was
|
21
|
+
# asked for credentials (ie. a 401 status code or a redirect to a login page) and false in other cases.
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
# should.require_login.get :index
|
25
|
+
def require_login
|
26
|
+
Test::Spec::Rails::Macros::Authorization::TestGenerator.new(test_case,
|
27
|
+
:login_required?,
|
28
|
+
'Expected login to be required'
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Authorization
|
34
|
+
class TestGenerator < Proxy
|
35
|
+
attr_accessor :validation_method, :message
|
36
|
+
|
37
|
+
def initialize(test_case, validation_method, message)
|
38
|
+
self.validation_method = validation_method
|
39
|
+
self.message = message
|
40
|
+
|
41
|
+
super(test_case)
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(verb, action, params={})
|
45
|
+
if [:get, :post, :put, :delete, :options].include?(verb.to_sym)
|
46
|
+
description = "should disallow #{verb.to_s.upcase} on `#{action}'"
|
47
|
+
description << " #{params.inspect}" unless params.blank?
|
48
|
+
|
49
|
+
validation_method = self.validation_method
|
50
|
+
message = self.message
|
51
|
+
|
52
|
+
test_case.it description do
|
53
|
+
send(verb, action, immediate_values(params))
|
54
|
+
send(validation_method).should.messaging(message) == true
|
55
|
+
end
|
56
|
+
else
|
57
|
+
super
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Test
|
2
|
+
module Spec
|
3
|
+
module Rails
|
4
|
+
module Macros
|
5
|
+
# Base class for all the proxy classes defined in the macros
|
6
|
+
class Proxy
|
7
|
+
attr_accessor :test_case
|
8
|
+
|
9
|
+
def initialize(test_case)
|
10
|
+
self.test_case = test_case
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Macros define methods on the Should class if they want to be called from the test case.
|
15
|
+
class Should < Proxy
|
16
|
+
end
|
17
|
+
|
18
|
+
# Stores expression to be evaluated later in the correct binding
|
19
|
+
class LazyValue
|
20
|
+
attr_accessor :value
|
21
|
+
def initialize(value)
|
22
|
+
self.value = value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
# Returns an instance of the Should class, this allows you to call macros from the test
|
28
|
+
# case is a nice way:
|
29
|
+
#
|
30
|
+
# should.disallow.get :index
|
31
|
+
def should
|
32
|
+
Test::Spec::Rails::Macros::Should.new(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns true when the passed name is a known table, we assume known tables also have fixtures
|
36
|
+
def known_fixture?(name)
|
37
|
+
respond_to?(:fixture_table_names) && fixture_table_names.include?(name.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Filter calls to fixture methods so we can use them in the definitions
|
41
|
+
def method_missing(method, *arguments, &block)
|
42
|
+
if known_fixture?(method)
|
43
|
+
arguments = arguments.map { |a| a.inspect }
|
44
|
+
Test::Spec::Rails::Macros::LazyValue.new("#{method}(#{arguments.join(', ')})")
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module InstanceMethods
|
52
|
+
# Interpret the non-immediate values in params and replace them
|
53
|
+
def immediate_values(params)
|
54
|
+
result = {}
|
55
|
+
params.each do |key, value|
|
56
|
+
result[key] = case value
|
57
|
+
when Hash
|
58
|
+
immediate_values(value)
|
59
|
+
when Test::Spec::Rails::Macros::LazyValue
|
60
|
+
eval(value.value).to_param
|
61
|
+
when Proc
|
62
|
+
value.call
|
63
|
+
else
|
64
|
+
value
|
65
|
+
end
|
66
|
+
end
|
67
|
+
result
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
Test::Spec::TestCase::ClassMethods.send(:include, Test::Spec::Rails::Macros::ClassMethods)
|
76
|
+
Test::Spec::TestCase::InstanceMethods.send(:include, Test::Spec::Rails::Macros::InstanceMethods)
|
77
|
+
|
78
|
+
require 'test/spec/rails/macros/authorization'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Test
|
2
|
+
module Spec
|
3
|
+
module Rails
|
4
|
+
class Status < SpecResponder
|
5
|
+
def should_equal(status, message=nil)
|
6
|
+
@test_case.send(:assert_response, status, message)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Template < SpecResponder
|
11
|
+
def should_equal(template, message=nil)
|
12
|
+
@test_case.send(:assert_template, template, message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Layout < SpecResponder
|
17
|
+
def should_equal(layout, message=nil)
|
18
|
+
rendered_layout = @test_case.response.layout.gsub(/layouts\//, '')
|
19
|
+
@test_case.send(:assert_equal, layout, rendered_layout, message)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ResponseHelpers
|
24
|
+
attr_reader :response
|
25
|
+
|
26
|
+
def status
|
27
|
+
Test::Spec::Rails::Status.new(self)
|
28
|
+
end
|
29
|
+
|
30
|
+
def template
|
31
|
+
Test::Spec::Rails::Template.new(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
def layout
|
35
|
+
Test::Spec::Rails::Layout.new(self)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ActiveSupport::TestCase.send(:include, Test::Spec::Rails::ResponseHelpers)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Test::Spec::Should
|
2
|
+
alias :_test_spec_equal :equal
|
3
|
+
def equal(*args)
|
4
|
+
@object.respond_to?(:should_equal) ? @object.should_equal(*args) : _test_spec_equal(*args)
|
5
|
+
end
|
6
|
+
|
7
|
+
alias :_test_spec_be :be
|
8
|
+
def be(*args)
|
9
|
+
@object.respond_to?(:should_equal) ? @object.should_equal(*args) : _test_spec_be(*args)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Test::Spec::ShouldNot
|
14
|
+
alias :_test_spec_equal :equal
|
15
|
+
def equal(*args)
|
16
|
+
@object.respond_to?(:should_not_equal) ? @object.should_not_equal(*args) : _test_spec_equal(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
alias :_test_spec_be :be
|
20
|
+
def be(*args)
|
21
|
+
@object.respond_to?(:should_not_equal) ? @object.should_not_equal(*args) : _test_spec_be(*args)
|
22
|
+
end
|
23
|
+
end
|