dan-manges-unit_controller 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.markdown +38 -0
- data/Rakefile +11 -23
- data/lib/unit_controller.rb +2 -2
- data/lib/unit_controller/capture_render.rb +23 -5
- data/lib/unit_controller/test_case_extension.rb +0 -3
- data/test/sample_controller.rb +11 -1
- data/test/test_helper.rb +34 -4
- data/test/unit_controller_test.rb +24 -8
- metadata +10 -14
- data/README +0 -14
- data/lib/unit_controller/no_view_rendering.rb +0 -7
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Dan Manges
|
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.markdown
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
UnitController
|
2
|
+
==============
|
3
|
+
|
4
|
+
UnitController stubs view rendering to help with unit testing controller.
|
5
|
+
|
6
|
+
Loading UnitController:
|
7
|
+
|
8
|
+
gem "unit_controller"
|
9
|
+
require "unit_controller"
|
10
|
+
|
11
|
+
In the setup method for your test case:
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@controller = SampleController.new
|
15
|
+
@request = ActionController::TestRequest.new
|
16
|
+
@response = ActionController::TestResponse.new
|
17
|
+
@controller.do_not_render_view
|
18
|
+
end
|
19
|
+
|
20
|
+
Development
|
21
|
+
-----------
|
22
|
+
|
23
|
+
Active development occurs on the [GitHub](http://github.com/dan-manges/unit-controller). Changes are also pushed to the Rubyforge git repository.
|
24
|
+
|
25
|
+
For bugs/patches/etc please use the [Rubyforge tracker](http://rubyforge.org/tracker/?group_id=5086)
|
26
|
+
|
27
|
+
Maintainer
|
28
|
+
----------
|
29
|
+
[Dan Manges](http://www.dcmanges.com)
|
30
|
+
|
31
|
+
Contributors
|
32
|
+
------------
|
33
|
+
|
34
|
+
* David Vollbracht
|
35
|
+
|
36
|
+
License
|
37
|
+
-------
|
38
|
+
Released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,48 +1,29 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
3
|
require 'rake/gempackagetask'
|
5
4
|
require 'rake/contrib/sshpublisher'
|
6
5
|
|
7
6
|
desc "Default: run tests"
|
8
|
-
task :default => :
|
7
|
+
task :default => "test:multi_verbose"
|
9
8
|
|
10
9
|
Rake::TestTask.new(:test) do |t|
|
11
|
-
t.libs << 'lib'
|
12
10
|
t.pattern = 'test/**/*_test.rb'
|
13
11
|
t.verbose = true
|
14
12
|
end
|
15
13
|
|
16
|
-
desc "Generate documentation"
|
17
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
-
rdoc.rdoc_dir = "doc"
|
19
|
-
rdoc.title = "UnitController"
|
20
|
-
rdoc.options << '--line-numbers'
|
21
|
-
rdoc.rdoc_files.include('README')
|
22
|
-
end
|
23
|
-
|
24
|
-
desc "Upload RDoc to RubyForge"
|
25
|
-
task :publish_rdoc => [:rerdoc] do
|
26
|
-
Rake::SshDirPublisher.new("dcmanges@rubyforge.org", "/var/www/gforge-projects/unit-controller", "doc").upload
|
27
|
-
end
|
28
|
-
|
29
14
|
require "date"
|
30
15
|
Gem.manage_gems
|
31
16
|
gem_spec = Gem::Specification.new do |s|
|
32
17
|
s.name = "unit_controller"
|
33
18
|
s.summary = "UnitController assists with unit testing controllers by stubbing view rendering."
|
34
|
-
s.version = "0.0
|
19
|
+
s.version = "0.1.0"
|
35
20
|
s.author = "Dan Manges"
|
36
21
|
s.description = s.summary
|
37
22
|
s.email = "daniel.manges@gmail.com"
|
38
23
|
s.homepage = "http://unit-controller.rubyforge.org"
|
39
24
|
s.rubyforge_project = "unit-controller"
|
40
25
|
|
41
|
-
s.
|
42
|
-
s.extra_rdoc_files = ['README']
|
43
|
-
s.rdoc_options << '--title' << "UnitController" << '--main' << 'README' << '--line-numbers'
|
44
|
-
|
45
|
-
s.files = FileList['{lib,test}/**/*.rb', 'README', 'Rakefile'].to_a
|
26
|
+
s.files = FileList['{lib,test}/**/*.rb', 'README.markdown', 'LICENSE', 'Rakefile'].to_a
|
46
27
|
end
|
47
28
|
Rake::GemPackageTask.new(gem_spec) do |package|
|
48
29
|
package.need_zip = false
|
@@ -61,15 +42,22 @@ namespace :gemspec do
|
|
61
42
|
end
|
62
43
|
end
|
63
44
|
|
64
|
-
RAILS_VERSIONS = %w[1.2.6 2.0.2]
|
45
|
+
RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1]
|
65
46
|
|
66
47
|
namespace :test do
|
67
48
|
desc "test with multiple versions of rails"
|
68
49
|
task :multi do
|
69
50
|
RAILS_VERSIONS.each do |rails_version|
|
51
|
+
puts "Testing with Rails #{rails_version}"
|
70
52
|
sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1"
|
71
53
|
end
|
72
54
|
end
|
55
|
+
|
56
|
+
task :multi_verbose do
|
57
|
+
RAILS_VERSIONS.each do |rails_version|
|
58
|
+
sh "RAILS_VERSION='#{rails_version}' rake test"
|
59
|
+
end
|
60
|
+
end
|
73
61
|
end
|
74
62
|
|
75
63
|
desc "pre-commit task"
|
data/lib/unit_controller.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require "unit_controller/capture_render"
|
2
|
-
require "unit_controller/no_view_rendering"
|
3
2
|
require "unit_controller/test_case_extension"
|
4
|
-
|
3
|
+
|
4
|
+
ActionController::Base.send :include, UnitController::CaptureRender
|
5
5
|
Test::Unit::TestCase.send :include, UnitController::TestCaseExtension
|
@@ -2,11 +2,29 @@ module UnitController
|
|
2
2
|
module CaptureRender
|
3
3
|
attr_reader :rendered
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def do_not_render_view
|
6
|
+
@render_view = false
|
7
|
+
end
|
8
|
+
|
9
|
+
def render_view?
|
10
|
+
instance_variables.include?("@render_view") ? @render_view : true
|
11
|
+
end
|
12
|
+
|
13
|
+
def render_with_unit_controller(*args, &block)
|
14
|
+
@rendered = args.first
|
15
|
+
if render_view?
|
16
|
+
render_without_unit_controller *args, &block
|
17
|
+
else
|
18
|
+
raise ActionController::DoubleRenderError, "Can only render or redirect once per action" if performed?
|
19
|
+
@performed_render = true
|
20
|
+
response.headers["Status"] = ActionController::Base::DEFAULT_RENDER_STATUS_CODE
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(klass)
|
25
|
+
klass.class_eval do
|
26
|
+
alias_method_chain :render, :unit_controller
|
27
|
+
end
|
10
28
|
end
|
11
29
|
end
|
12
30
|
end
|
@@ -1,9 +1,6 @@
|
|
1
1
|
module UnitController
|
2
2
|
module TestCaseExtension
|
3
3
|
def assert_rendered(options = nil, message = nil)
|
4
|
-
if !@controller.respond_to?(:rendered)
|
5
|
-
raise NoMethodError, "#{@controller} did not remember what it rendered. Did you call @controller.do_not_render_view ?"
|
6
|
-
end
|
7
4
|
assert_equal options, @controller.rendered, message
|
8
5
|
end
|
9
6
|
end
|
data/test/sample_controller.rb
CHANGED
@@ -3,7 +3,7 @@ class SampleController < ActionController::Base
|
|
3
3
|
render :text => "once"
|
4
4
|
render :text => "twice"
|
5
5
|
end
|
6
|
-
|
6
|
+
|
7
7
|
def implicit_template
|
8
8
|
end
|
9
9
|
|
@@ -11,6 +11,16 @@ class SampleController < ActionController::Base
|
|
11
11
|
render :inline => "<% raise 'raise in template' %>"
|
12
12
|
end
|
13
13
|
|
14
|
+
def render_then_redirect
|
15
|
+
render :text => "hi"
|
16
|
+
redirect_to :action => :text_foo
|
17
|
+
end
|
18
|
+
|
19
|
+
def redirect_then_render
|
20
|
+
redirect_to :action => :text_foo
|
21
|
+
render :text => "hi"
|
22
|
+
end
|
23
|
+
|
14
24
|
def text_foo
|
15
25
|
render :text => "foo"
|
16
26
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
gem "rails", ENV["RAILS_VERSION"] || ">= 2.0.2"
|
3
3
|
require "rails/version"
|
4
|
+
puts "==== Testing with Rails #{Rails::VERSION::STRING} ===="
|
4
5
|
require "action_controller"
|
5
6
|
require "action_controller/test_process"
|
6
7
|
|
@@ -12,8 +13,16 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
|
12
13
|
require "unit_controller"
|
13
14
|
|
14
15
|
require "test/unit"
|
15
|
-
|
16
|
-
|
16
|
+
|
17
|
+
Test::Unit::TestCase.class_eval do
|
18
|
+
def self.test(name, &block)
|
19
|
+
test_name = "test_" + name.gsub(/\W/, "_")
|
20
|
+
if self.instance_methods.include? test_name
|
21
|
+
raise "#{test_name} is already defined in #{self}"
|
22
|
+
end
|
23
|
+
define_method test_name, &block
|
24
|
+
end
|
25
|
+
end
|
17
26
|
|
18
27
|
require File.dirname(__FILE__) + "/sample_controller"
|
19
28
|
|
@@ -29,12 +38,28 @@ end
|
|
29
38
|
|
30
39
|
module TestHelper
|
31
40
|
def self.define_test_case(&block)
|
32
|
-
klass = Rails::VERSION::MAJOR >= 2
|
41
|
+
klass = if Rails::VERSION::MAJOR >= 2
|
42
|
+
Rails::VERSION::MINOR.zero? ? rails2_0_test_case : rails2_test_case
|
43
|
+
else
|
44
|
+
rails1_test_case
|
45
|
+
end
|
33
46
|
klass.class_eval &block
|
34
47
|
klass
|
35
48
|
end
|
36
49
|
|
37
|
-
def self.rails2_test_case
|
50
|
+
def self.rails2_test_case(&block)
|
51
|
+
klass = Class.new(ActionController::TestCase)
|
52
|
+
klass.class_eval do
|
53
|
+
tests SampleController
|
54
|
+
|
55
|
+
def setup
|
56
|
+
@controller.do_not_render_view
|
57
|
+
end
|
58
|
+
end
|
59
|
+
klass
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.rails2_0_test_case
|
38
63
|
klass = Class.new(ActionController::TestCase)
|
39
64
|
klass.class_eval do
|
40
65
|
tests SampleController
|
@@ -60,3 +85,8 @@ module TestHelper
|
|
60
85
|
klass
|
61
86
|
end
|
62
87
|
end
|
88
|
+
|
89
|
+
# tests weren't running on rails 2.2.0 without this !?!?!?
|
90
|
+
if Rails::VERSION::STRING[0,3] == "2.2"
|
91
|
+
at_exit { subclasses_of(Test::Unit::TestCase) }
|
92
|
+
end
|
@@ -17,15 +17,31 @@ TestHelper.define_test_case do
|
|
17
17
|
get :double_render
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
test "raise on render then redirect" do
|
22
|
+
assert_raises(ActionController::DoubleRenderError) do
|
23
|
+
get :render_then_redirect
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "raise on redirect then render" do
|
28
|
+
assert_raises(ActionController::DoubleRenderError) do
|
29
|
+
get :redirect_then_render
|
30
|
+
end
|
31
|
+
end
|
20
32
|
|
21
|
-
test "
|
33
|
+
test "controller remembers what it renders even if rendering view" do
|
22
34
|
@controller = @controller.class.new
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
assert_equal true, @controller.render_view?
|
36
|
+
get :text_foo
|
37
|
+
assert_rendered :text => "foo"
|
38
|
+
assert_equal "foo", @response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
test "render_view?" do
|
42
|
+
@controller = @controller.class.new
|
43
|
+
assert_equal true, @controller.render_view?
|
44
|
+
@controller.do_not_render_view
|
45
|
+
assert_equal false, @controller.render_view?
|
30
46
|
end
|
31
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dan-manges-unit_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Manges
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-10-24 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -19,28 +19,24 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
24
|
files:
|
25
25
|
- lib/unit_controller/capture_render.rb
|
26
|
-
- lib/unit_controller/no_view_rendering.rb
|
27
26
|
- lib/unit_controller/test_case_extension.rb
|
28
27
|
- lib/unit_controller.rb
|
29
28
|
- test/sample_controller.rb
|
30
29
|
- test/test_case_test.rb
|
31
30
|
- test/test_helper.rb
|
32
31
|
- test/unit_controller_test.rb
|
33
|
-
- README
|
32
|
+
- README.markdown
|
33
|
+
- LICENSE
|
34
34
|
- Rakefile
|
35
|
-
has_rdoc:
|
35
|
+
has_rdoc: false
|
36
36
|
homepage: http://unit-controller.rubyforge.org
|
37
37
|
post_install_message:
|
38
|
-
rdoc_options:
|
39
|
-
|
40
|
-
- UnitController
|
41
|
-
- --main
|
42
|
-
- README
|
43
|
-
- --line-numbers
|
38
|
+
rdoc_options: []
|
39
|
+
|
44
40
|
require_paths:
|
45
41
|
- lib
|
46
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -58,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
54
|
requirements: []
|
59
55
|
|
60
56
|
rubyforge_project: unit-controller
|
61
|
-
rubygems_version: 1.0
|
57
|
+
rubygems_version: 1.2.0
|
62
58
|
signing_key:
|
63
59
|
specification_version: 2
|
64
60
|
summary: UnitController assists with unit testing controllers by stubbing view rendering.
|
data/README
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
= UnitController
|
2
|
-
|
3
|
-
Note to self: http://dev.rubyonrails.org/ticket/10536
|
4
|
-
|
5
|
-
== Development
|
6
|
-
|
7
|
-
Active development occurs on the GitHub.[http://github.com/dan-manges/unit-controller] Changes are pushed to the Rubyforge git repository.
|
8
|
-
|
9
|
-
== Contributors
|
10
|
-
|
11
|
-
* Dan Manges
|
12
|
-
|
13
|
-
== License
|
14
|
-
Released under Ruby's license.[http://www.ruby-lang.org/en/LICENSE.txt]
|