on-test-spec 0.2.4
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.yml +5 -0
- data/dot.autotest +93 -0
- data/lib/test/spec/add_allow_switch.rb +53 -0
- data/lib/test/spec/rails/controller_helpers.rb +11 -0
- data/lib/test/spec/rails/expectations.rb +192 -0
- data/lib/test/spec/rails/macros/authorization.rb +80 -0
- data/lib/test/spec/rails/macros/response.rb +57 -0
- data/lib/test/spec/rails/macros.rb +79 -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 +37 -0
- data/lib/test/spec/rails.rb +89 -0
- data/lib/test/spec/share.rb +48 -0
- data/on-test-spec.gemspec +74 -0
- data/test/add_allow_switch_test.rb +100 -0
- data/test/controller_helpers_test.rb +34 -0
- data/test/expectations_test.rb +459 -0
- data/test/macros/authorization_test.rb +102 -0
- data/test/macros/base_test.rb +78 -0
- data/test/macros/response_test.rb +82 -0
- data/test/rails_test.rb +100 -0
- data/test/share_test.rb +50 -0
- data/test/test_helper.rb +6 -0
- data/test/test_spec_ext_test.rb +26 -0
- metadata +95 -0
@@ -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
|
+
ActionController::TestCase.send(:include, Test::Spec::Rails::ResponseHelpers)
|
@@ -0,0 +1,37 @@
|
|
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
|
24
|
+
|
25
|
+
module Test
|
26
|
+
module Spec
|
27
|
+
module ExpectationExt
|
28
|
+
# Returns the current test case instance. Use this to call assert methods on.
|
29
|
+
def test_case
|
30
|
+
$TEST_SPEC_TESTCASE
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Should.send(:include, ExpectationExt)
|
35
|
+
ShouldNot.send(:include, ExpectationExt)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
|
3
|
+
require 'active_support/test_case'
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/test_case'
|
7
|
+
|
8
|
+
require 'action_controller'
|
9
|
+
require 'action_controller/test_case'
|
10
|
+
|
11
|
+
require 'action_view'
|
12
|
+
require 'action_view/test_case'
|
13
|
+
|
14
|
+
%w(test_spec_ext spec_responder expectations).each { |lib| require "test/spec/rails/#{lib}" }
|
15
|
+
Dir[File.dirname(__FILE__) + '/rails/**/*_helpers.rb'].each { |lib| require lib }
|
16
|
+
|
17
|
+
module Test
|
18
|
+
module Spec
|
19
|
+
module Rails
|
20
|
+
def self.extract_test_case_args(args)
|
21
|
+
name = args.map { |a| a.to_s }.join(' ')
|
22
|
+
class_to_test = args.find { |a| a.is_a?(Module) }
|
23
|
+
superclass = test_case_for_class(class_to_test)
|
24
|
+
[name, class_to_test, superclass]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.test_case_for_class(klass)
|
28
|
+
if klass
|
29
|
+
if klass.ancestors.include?(ActiveRecord::Base)
|
30
|
+
ActiveRecord::TestCase
|
31
|
+
elsif klass.ancestors.include?(ActionController::Base)
|
32
|
+
ActionController::TestCase
|
33
|
+
elsif !klass.is_a?(Class) && klass.to_s.ends_with?('Helper')
|
34
|
+
ActionView::TestCase
|
35
|
+
end
|
36
|
+
end || ActiveSupport::TestCase
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module Kernel
|
43
|
+
alias :context_before_on_test_spec :context
|
44
|
+
alias :xcontext_before_on_test_spec :xcontext
|
45
|
+
|
46
|
+
# Creates a new test case.
|
47
|
+
#
|
48
|
+
# The description of the test case, can consist from strings and/or the class
|
49
|
+
# that's to be tested.
|
50
|
+
#
|
51
|
+
# If the class inherits from ActiveRecord::Base, ActiveRecord::TestCase will
|
52
|
+
# be used as the test case superclass. In the case of a class which inherits
|
53
|
+
# from ActionController::Base, ActionController::TestCase will be used. And
|
54
|
+
# when given a module which name ends with “Helper”, ActionView::TestCase
|
55
|
+
# will be used. In the latter two cases, the test case will be setup for the
|
56
|
+
# class that's to be tested.
|
57
|
+
#
|
58
|
+
# In all other cases the test case superclass will be ActiveSupport::TestCase.
|
59
|
+
#
|
60
|
+
# Examples:
|
61
|
+
#
|
62
|
+
# describe Member do # "Member"
|
63
|
+
# ...
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# describe 'On a', MembersController do # "On a MembersController"
|
67
|
+
# ...
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# describe 'The', MembersHelper, ', concerning dates' do # "The MembersHelper, concerning dates"
|
71
|
+
# ...
|
72
|
+
# end
|
73
|
+
def context(*args, &block)
|
74
|
+
name, class_to_test, superclass = Test::Spec::Rails.extract_test_case_args(args)
|
75
|
+
spec = context_before_on_test_spec(name, superclass) { tests class_to_test if respond_to?(:tests) }
|
76
|
+
spec.testcase.class_eval(&block)
|
77
|
+
spec
|
78
|
+
end
|
79
|
+
|
80
|
+
def xcontext(*args, &block)
|
81
|
+
name, _, superclass = Test::Spec::Rails.extract_test_case_args(args)
|
82
|
+
xcontext_before_on_test_spec(name, superclass, &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
private :context, :xcontext
|
86
|
+
|
87
|
+
alias :describe :context
|
88
|
+
alias :xdescribe :xcontext
|
89
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
$shared_specs = {}
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
# Stores the passed in block for inclusion in test cases.
|
5
|
+
#
|
6
|
+
# share "User" do
|
7
|
+
# it "should authenticate" do
|
8
|
+
# "me".should == "me"
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# describe "User, in general" do
|
13
|
+
# shared_specs_for 'User'
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# describe "User, in another case" do
|
17
|
+
# shared_specs_for 'User'
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# 2 tests, 2 assertions, 0 failures, 0 errors
|
21
|
+
def share(name, &specs_block)
|
22
|
+
$shared_specs[name] = specs_block
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module SharedSpecsInclusionHelper
|
27
|
+
# Include the specified shared specs in this test case.
|
28
|
+
#
|
29
|
+
# share "User" do
|
30
|
+
# it "should authenticate" do
|
31
|
+
# "me".should == "me"
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# describe "User, in general" do
|
36
|
+
# shared_specs_for 'User'
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# describe "User, in another case" do
|
40
|
+
# shared_specs_for 'User'
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# 2 tests, 2 assertions, 0 failures, 0 errors
|
44
|
+
def shared_specs_for(name)
|
45
|
+
self.class_eval &$shared_specs[name]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
Test::Unit::TestCase.send(:extend, SharedSpecsInclusionHelper)
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
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 = %q{on-test-spec}
|
8
|
+
s.version = "0.2.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Manfred Stienstra", "Eloy Duran", "Cristi Balan"]
|
12
|
+
s.date = %q{2009-09-24}
|
13
|
+
s.description = %q{Rails plugin to make testing Rails on test/spec easier.}
|
14
|
+
s.email = %q{eloy.de.enige@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"dot.autotest",
|
26
|
+
"lib/test/spec/add_allow_switch.rb",
|
27
|
+
"lib/test/spec/rails.rb",
|
28
|
+
"lib/test/spec/rails/controller_helpers.rb",
|
29
|
+
"lib/test/spec/rails/expectations.rb",
|
30
|
+
"lib/test/spec/rails/macros.rb",
|
31
|
+
"lib/test/spec/rails/macros/authorization.rb",
|
32
|
+
"lib/test/spec/rails/request_helpers.rb",
|
33
|
+
"lib/test/spec/rails/response_helpers.rb",
|
34
|
+
"lib/test/spec/rails/spec_responder.rb",
|
35
|
+
"lib/test/spec/rails/test_spec_ext.rb",
|
36
|
+
"lib/test/spec/share.rb",
|
37
|
+
"on-test-spec.gemspec",
|
38
|
+
"test/add_allow_switch_test.rb",
|
39
|
+
"test/controller_helpers_test.rb",
|
40
|
+
"test/expectations_test.rb",
|
41
|
+
"test/macros/authorization_test.rb",
|
42
|
+
"test/macros/base_test.rb",
|
43
|
+
"test/rails_test.rb",
|
44
|
+
"test/share_test.rb",
|
45
|
+
"test/test_helper.rb",
|
46
|
+
"test/test_spec_ext_test.rb"
|
47
|
+
]
|
48
|
+
s.homepage = %q{http://github.com/Fingertips/on-test-spec}
|
49
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
50
|
+
s.require_paths = ["lib"]
|
51
|
+
s.rubygems_version = %q{1.3.5}
|
52
|
+
s.summary = %q{Rails plugin to make testing Rails on test/spec easier.}
|
53
|
+
s.test_files = [
|
54
|
+
"test/add_allow_switch_test.rb",
|
55
|
+
"test/controller_helpers_test.rb",
|
56
|
+
"test/expectations_test.rb",
|
57
|
+
"test/macros/authorization_test.rb",
|
58
|
+
"test/macros/base_test.rb",
|
59
|
+
"test/rails_test.rb",
|
60
|
+
"test/share_test.rb",
|
61
|
+
"test/test_helper.rb",
|
62
|
+
"test/test_spec_ext_test.rb"
|
63
|
+
]
|
64
|
+
|
65
|
+
if s.respond_to? :specification_version then
|
66
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
67
|
+
s.specification_version = 3
|
68
|
+
|
69
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
70
|
+
else
|
71
|
+
end
|
72
|
+
else
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
require 'test/spec/add_allow_switch'
|
3
|
+
|
4
|
+
module Factory
|
5
|
+
def self.run
|
6
|
+
true
|
7
|
+
end
|
8
|
+
end
|
9
|
+
Factory.add_allow_switch :run, :default => true
|
10
|
+
|
11
|
+
describe "Factory with an allow switch on run" do
|
12
|
+
it "should alias the original method" do
|
13
|
+
Factory.respond_to?(:original_run, include_private=true).should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should define a getter and setter" do
|
17
|
+
Factory.should.respond_to(:allow_run)
|
18
|
+
Factory.should.respond_to(:allow_run=)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should switch off" do
|
22
|
+
Factory.allow_run = false
|
23
|
+
lambda {
|
24
|
+
Factory.run
|
25
|
+
}.should.raise(RuntimeError)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should switch on" do
|
29
|
+
Factory.allow_run = true
|
30
|
+
lambda {
|
31
|
+
Factory.run.should == true
|
32
|
+
}.should.not.raise
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Bunny
|
37
|
+
def hop
|
38
|
+
'Hop hop!'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
Bunny.add_allow_switch :hop
|
42
|
+
|
43
|
+
describe "Bunny with an allow switch on hop" do
|
44
|
+
before do
|
45
|
+
@bunny = Bunny.new
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should alias the original method" do
|
49
|
+
@bunny.respond_to?(:original_hop).should == true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should define a getter and setter" do
|
53
|
+
Bunny.should.respond_to(:allow_hop)
|
54
|
+
Bunny.should.respond_to(:allow_hop=)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should switch off" do
|
58
|
+
Bunny.allow_hop = false
|
59
|
+
lambda {
|
60
|
+
@bunny.hop
|
61
|
+
}.should.raise(RuntimeError)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should switch on" do
|
65
|
+
Bunny.allow_hop = true
|
66
|
+
lambda {
|
67
|
+
@bunny.hop.should == 'Hop hop!'
|
68
|
+
}.should.not.raise
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
Kernel.add_allow_switch :system
|
74
|
+
|
75
|
+
describe "Kernel with an allow switch on system" do
|
76
|
+
SILENT_COMMANT = 'ls > /dev/null'
|
77
|
+
|
78
|
+
it "should alias the original method" do
|
79
|
+
Kernel.respond_to?(:original_system, include_private=true).should == true
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should define a getter and setter" do
|
83
|
+
Factory.should.respond_to(:allow_system)
|
84
|
+
Factory.should.respond_to(:allow_system=)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should switch off" do
|
88
|
+
Kernel.allow_system = false
|
89
|
+
lambda {
|
90
|
+
Kernel.system(SILENT_COMMANT)
|
91
|
+
}.should.raise(RuntimeError)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should switch on" do
|
95
|
+
Kernel.allow_system = true
|
96
|
+
lambda {
|
97
|
+
Kernel.system(SILENT_COMMANT)
|
98
|
+
}.should.not.raise
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
require 'test/spec/rails/request_helpers'
|
3
|
+
require 'test/spec/rails/response_helpers'
|
4
|
+
require 'test/spec/rails/controller_helpers'
|
5
|
+
|
6
|
+
class ActionControllerClass < ActionController::Base; end
|
7
|
+
|
8
|
+
describe ActionControllerClass, "response helpers for a controller test" do
|
9
|
+
it "should map #controller to the controller instance" do
|
10
|
+
controller.should == @controller
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should map #status to the response status" do
|
14
|
+
expects(:assert_response).with(:success, 'the message')
|
15
|
+
status.should.be :success, 'the message'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should map #template to the response template" do
|
19
|
+
expects(:assert_template).with('show', 'the message')
|
20
|
+
template.should.be 'show', 'the message'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should map #layout to the response layout" do
|
24
|
+
@response.stubs(:layout).returns('layouts/application')
|
25
|
+
expects(:assert_equal).with('application', 'application', 'the message')
|
26
|
+
layout.should.be 'application', 'the message'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ActionControllerClass, "request helpers for a controller test" do
|
31
|
+
it "should make @request available as an accessor" do
|
32
|
+
request.should.be @request
|
33
|
+
end
|
34
|
+
end
|