moser-unit_controller 0.1.1

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 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 ADDED
@@ -0,0 +1,64 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ desc "Default: run tests"
7
+ task :default => "test:multi_verbose"
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
13
+
14
+ require "date"
15
+ Gem.manage_gems
16
+ gem_spec = Gem::Specification.new do |s|
17
+ s.name = "unit_controller"
18
+ s.summary = "UnitController assists with unit testing controllers by stubbing view rendering."
19
+ s.version = "0.1.0"
20
+ s.author = "Dan Manges"
21
+ s.description = s.summary
22
+ s.email = "daniel.manges@gmail.com"
23
+ s.homepage = "http://unit-controller.rubyforge.org"
24
+ s.rubyforge_project = "unit-controller"
25
+
26
+ s.files = FileList['{lib,test}/**/*.rb', 'README.markdown', 'LICENSE', 'Rakefile'].to_a
27
+ end
28
+ Rake::GemPackageTask.new(gem_spec) do |package|
29
+ package.need_zip = false
30
+ package.need_tar = false
31
+ end
32
+
33
+ Rake::Task["gem"].prerequisites.unshift "test:multi"
34
+
35
+ namespace :gemspec do
36
+ desc "generates unit_controller.gemspec"
37
+ task :generate do
38
+ File.open("unit_controller.gemspec", "w") do |f|
39
+ f.puts "# this file is generated by rake gemspec:generate for github"
40
+ f.write gem_spec.to_ruby
41
+ end
42
+ end
43
+ end
44
+
45
+ RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1 2.2.2]
46
+
47
+ namespace :test do
48
+ desc "test with multiple versions of rails"
49
+ task :multi do
50
+ RAILS_VERSIONS.each do |rails_version|
51
+ puts "Testing with Rails #{rails_version}"
52
+ sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1"
53
+ end
54
+ end
55
+
56
+ task :multi_verbose do
57
+ (RAILS_VERSIONS - %w[2.2.2]).each do |rails_version|
58
+ sh "RAILS_VERSION='#{rails_version}' rake test"
59
+ end
60
+ end
61
+ end
62
+
63
+ desc "pre-commit task"
64
+ task :pc => %w[test:multi gemspec:generate]
@@ -0,0 +1,5 @@
1
+ require "unit_controller/capture_render"
2
+ require "unit_controller/test_case_extension"
3
+
4
+ ActionController::Base.send :include, UnitController::CaptureRender
5
+ Test::Unit::TestCase.send :include, UnitController::TestCaseExtension
@@ -0,0 +1,40 @@
1
+ module UnitController
2
+ module CaptureRender
3
+ attr_reader :rendered
4
+
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
+ if args.first.is_a?(Hash)
21
+ status = args.first[:status]
22
+ elsif args.last.is_a?(Hash)
23
+ status = args.last[:status]
24
+ end
25
+ if status
26
+ status = ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[status] if status.is_a?(Symbol)
27
+ response.headers["Status"] = status.to_s
28
+ else
29
+ response.headers["Status"] = ActionController::Base::DEFAULT_RENDER_STATUS_CODE
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.included(klass)
35
+ klass.class_eval do
36
+ alias_method_chain :render, :unit_controller
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ module UnitController
2
+ module TestCaseExtension
3
+ def assert_rendered(options = nil, message = nil)
4
+ assert_equal options, @controller.rendered, message
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ class SampleController < ActionController::Base
2
+ def double_render
3
+ render :text => "once"
4
+ render :text => "twice"
5
+ end
6
+
7
+ def implicit_template
8
+ end
9
+
10
+ def raise_inline
11
+ render :inline => "<% raise 'raise in template' %>"
12
+ end
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
+
24
+ def text_foo
25
+ render :text => "foo"
26
+ end
27
+
28
+ def some_status
29
+ render :text => "lala", :status => 403
30
+ end
31
+
32
+ def will_not_find
33
+ render :text => "Nothing here", :status => :not_found
34
+ end
35
+ end
36
+
37
+ # Re-raise errors caught by the controller.
38
+ class SampleController; def rescue_action(e) raise e end; end
39
+
40
+ SampleController.instance_methods(false).each do |action|
41
+ ActionController::Routing::Routes.add_route \
42
+ "/sample/#{action}", :controller => "sample", :action => action
43
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+ TestHelper.define_test_case do
3
+ test "assert_rendered" do
4
+ get :text_foo
5
+ assert_passes do
6
+ assert_rendered :text => "foo"
7
+ end
8
+ assert_fails do
9
+ assert_rendered :text => "bar"
10
+ end
11
+ end
12
+
13
+ test "assert_rendered on implicit_template" do
14
+ get :implicit_template
15
+ assert_passes do
16
+ assert_rendered
17
+ end
18
+ assert_fails do
19
+ assert_rendered :something_that_it_didnt_render
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,95 @@
1
+ require "rubygems"
2
+ #tests don't work with 2.3.2 ('backtrace_cleaner' NoMethodError)
3
+ gem "rails", ">= 2.0.2", "<= 2.2.2" #, ENV["RAILS_VERSION"] || ">= 2.0.2"
4
+ require "rails/version"
5
+ puts "==== Testing with Rails #{Rails::VERSION::STRING} ===="
6
+ require "action_controller"
7
+ require "action_controller/test_process"
8
+
9
+ if Rails::VERSION::MAJOR >= 2
10
+ require "action_controller/test_case"
11
+ end
12
+
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
14
+ require "unit_controller"
15
+
16
+ require "test/unit"
17
+
18
+ Test::Unit::TestCase.class_eval do
19
+ begin
20
+ def self.test(name, &block)
21
+ test_name = "test_" + name.gsub(/\W/, "_")
22
+ if self.instance_methods.include? test_name
23
+ raise "#{test_name} is already defined in #{self}"
24
+ end
25
+ define_method test_name, &block
26
+ end
27
+ end
28
+ end
29
+
30
+ require File.dirname(__FILE__) + "/sample_controller"
31
+
32
+ Test::Unit::TestCase.class_eval do
33
+ def assert_fails(&block)
34
+ assert_raises Test::Unit::AssertionFailedError, &block
35
+ end
36
+
37
+ def assert_passes
38
+ yield
39
+ end
40
+ end
41
+
42
+ module TestHelper
43
+ def self.define_test_case(&block)
44
+ klass = if Rails::VERSION::MAJOR >= 2
45
+ Rails::VERSION::MINOR.zero? ? rails2_0_test_case : rails2_test_case
46
+ else
47
+ rails1_test_case
48
+ end
49
+ klass.class_eval &block
50
+ klass
51
+ end
52
+
53
+ def self.rails2_test_case(&block)
54
+ klass = Class.new(ActionController::TestCase)
55
+ klass.class_eval do
56
+ tests SampleController
57
+
58
+ def setup
59
+ @controller.do_not_render_view
60
+ end
61
+ end
62
+ klass
63
+ end
64
+
65
+ def self.rails2_0_test_case
66
+ klass = Class.new(ActionController::TestCase)
67
+ klass.class_eval do
68
+ tests SampleController
69
+
70
+ def setup
71
+ super
72
+ @controller.do_not_render_view
73
+ end
74
+ end
75
+ klass
76
+ end
77
+
78
+ def self.rails1_test_case
79
+ klass = Class.new(Test::Unit::TestCase)
80
+ klass.class_eval do
81
+ def setup
82
+ @controller = SampleController.new
83
+ @request = ActionController::TestRequest.new
84
+ @response = ActionController::TestResponse.new
85
+ @controller.do_not_render_view
86
+ end
87
+ end
88
+ klass
89
+ end
90
+ end
91
+
92
+ # tests weren't running on rails 2.2.0 without this !?!?!?
93
+ if Rails::VERSION::STRING[0,3] == "2.2"
94
+ at_exit { subclasses_of(Test::Unit::TestCase) }
95
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ TestHelper.define_test_case do
4
+
5
+
6
+ test "action that has view that raises" do
7
+ assert_nothing_raised do
8
+ get :raise_inline
9
+ end
10
+ end
11
+
12
+ test "assert_response :success works" do
13
+ get :implicit_template
14
+ assert_response :success
15
+ end
16
+
17
+ test "assert_response x works" do
18
+ get :some_status
19
+ assert_response 403
20
+ get :will_not_find
21
+ assert_response :not_found
22
+ end
23
+
24
+ test "raise on double render" do
25
+ assert_raises(ActionController::DoubleRenderError) do
26
+ get :double_render
27
+ end
28
+ end
29
+
30
+ test "raise on render then redirect" do
31
+ assert_raises(ActionController::DoubleRenderError) do
32
+ get :render_then_redirect
33
+ end
34
+ end
35
+
36
+ test "raise on redirect then render" do
37
+ assert_raises(ActionController::DoubleRenderError) do
38
+ get :redirect_then_render
39
+ end
40
+ end
41
+
42
+ test "controller remembers what it renders even if rendering view" do
43
+ @controller = @controller.class.new
44
+ assert_equal true, @controller.render_view?
45
+ get :text_foo
46
+ assert_rendered :text => "foo"
47
+ assert_equal "foo", @response.body
48
+ end
49
+
50
+ test "render_view?" do
51
+ @controller = @controller.class.new
52
+ assert_equal true, @controller.render_view?
53
+ @controller.do_not_render_view
54
+ assert_equal false, @controller.render_view?
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moser-unit_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Manges
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: UnitController assists with unit testing controllers by stubbing view rendering.
17
+ email: daniel.manges@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/unit_controller/capture_render.rb
26
+ - lib/unit_controller/test_case_extension.rb
27
+ - lib/unit_controller.rb
28
+ - test/sample_controller.rb
29
+ - test/test_case_test.rb
30
+ - test/test_helper.rb
31
+ - test/unit_controller_test.rb
32
+ - README.markdown
33
+ - LICENSE
34
+ - Rakefile
35
+ has_rdoc: false
36
+ homepage: http://unit-controller.rubyforge.org
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: unit-controller
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: UnitController assists with unit testing controllers by stubbing view rendering.
61
+ test_files: []
62
+