focused_controller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/.gitignore +5 -0
  2. data/.travis.yml +15 -0
  3. data/Appraisals +11 -0
  4. data/Gemfile +4 -0
  5. data/README.md +1 -0
  6. data/Rakefile +21 -0
  7. data/focused_controller.gemspec +30 -0
  8. data/gemfiles/rails-3-0.gemfile +7 -0
  9. data/gemfiles/rails-3-1.gemfile +7 -0
  10. data/gemfiles/rails-3-2.gemfile +7 -0
  11. data/lib/focused_controller.rb +4 -0
  12. data/lib/focused_controller/action_name.rb +6 -0
  13. data/lib/focused_controller/functional_test_helper.rb +25 -0
  14. data/lib/focused_controller/mixin.rb +44 -0
  15. data/lib/focused_controller/route.rb +15 -0
  16. data/lib/focused_controller/route_mapper.rb +58 -0
  17. data/lib/focused_controller/rspec_controller_class.rb +15 -0
  18. data/lib/focused_controller/rspec_functional_helper.rb +25 -0
  19. data/lib/focused_controller/rspec_helper.rb +42 -0
  20. data/lib/focused_controller/test_helper.rb +183 -0
  21. data/lib/focused_controller/version.rb +3 -0
  22. data/test/acceptance/app_test.rb +156 -0
  23. data/test/app/.gitignore +15 -0
  24. data/test/app/Gemfile +6 -0
  25. data/test/app/README.rdoc +261 -0
  26. data/test/app/Rakefile +7 -0
  27. data/test/app/app/controllers/application_controller.rb +6 -0
  28. data/test/app/app/controllers/posts_controller.rb +60 -0
  29. data/test/app/app/models/.gitkeep +0 -0
  30. data/test/app/app/models/post.rb +88 -0
  31. data/test/app/app/views/layouts/application.html.erb +13 -0
  32. data/test/app/app/views/posts/_form.html.erb +31 -0
  33. data/test/app/app/views/posts/edit.html.erb +6 -0
  34. data/test/app/app/views/posts/index.html.erb +23 -0
  35. data/test/app/app/views/posts/new.html.erb +5 -0
  36. data/test/app/app/views/posts/show.html.erb +8 -0
  37. data/test/app/config.ru +4 -0
  38. data/test/app/config/application.rb +16 -0
  39. data/test/app/config/boot.rb +6 -0
  40. data/test/app/config/environment.rb +5 -0
  41. data/test/app/config/environments/development.rb +21 -0
  42. data/test/app/config/environments/test.rb +29 -0
  43. data/test/app/config/initializers/backtrace_silencers.rb +7 -0
  44. data/test/app/config/initializers/inflections.rb +15 -0
  45. data/test/app/config/initializers/mime_types.rb +5 -0
  46. data/test/app/config/initializers/secret_token.rb +7 -0
  47. data/test/app/config/initializers/session_store.rb +8 -0
  48. data/test/app/config/locales/en.yml +5 -0
  49. data/test/app/config/routes.rb +62 -0
  50. data/test/app/db/seeds.rb +7 -0
  51. data/test/app/doc/README_FOR_APP +2 -0
  52. data/test/app/lib/assets/.gitkeep +0 -0
  53. data/test/app/lib/tasks/.gitkeep +0 -0
  54. data/test/app/log/.gitkeep +0 -0
  55. data/test/app/public/404.html +26 -0
  56. data/test/app/public/422.html +26 -0
  57. data/test/app/public/500.html +25 -0
  58. data/test/app/public/favicon.ico +0 -0
  59. data/test/app/public/index.html +241 -0
  60. data/test/app/public/javascripts/application.js +9663 -0
  61. data/test/app/public/robots.txt +5 -0
  62. data/test/app/public/stylesheets/application.css +83 -0
  63. data/test/app/script/rails +6 -0
  64. data/test/app/spec/controllers/posts_controller_spec.rb +59 -0
  65. data/test/app/spec/isolated_spec_helper.rb +9 -0
  66. data/test/app/spec/spec_helper.rb +5 -0
  67. data/test/app/spec/unit/controllers/posts_controller_isolated_spec.rb +60 -0
  68. data/test/app/spec/unit/controllers/posts_controller_spec.rb +59 -0
  69. data/test/app/test/functional/.gitkeep +0 -0
  70. data/test/app/test/functional/posts_controller_test.rb +67 -0
  71. data/test/app/test/isolated_test_helper.rb +10 -0
  72. data/test/app/test/test_helper.rb +6 -0
  73. data/test/app/test/unit/.gitkeep +0 -0
  74. data/test/app/test/unit/controllers/posts_controller_isolated_test.rb +69 -0
  75. data/test/app/test/unit/controllers/posts_controller_test.rb +67 -0
  76. data/test/app/vendor/assets/javascripts/.gitkeep +0 -0
  77. data/test/app/vendor/assets/stylesheets/.gitkeep +0 -0
  78. data/test/app/vendor/plugins/.gitkeep +0 -0
  79. data/test/helper.rb +33 -0
  80. data/test/unit/functional_test_helper_test.rb +65 -0
  81. data/test/unit/mixin_test.rb +70 -0
  82. data/test/unit/route_mapper_test.rb +73 -0
  83. data/test/unit/route_test.rb +39 -0
  84. data/test/unit/rspec_functional_helper.rb +42 -0
  85. data/test/unit/rspec_helper_test.rb +91 -0
  86. data/test/unit/test_helper_test.rb +235 -0
  87. metadata +285 -0
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+
3
+ class PostsController
4
+ class TestCase < ActiveSupport::TestCase
5
+ include FocusedController::TestHelper
6
+
7
+ setup do
8
+ @post = Post.create(:title => 'Hello', :body => 'Omg')
9
+ end
10
+ end
11
+
12
+ class IndexTest < TestCase
13
+ test "should get index" do
14
+ req
15
+ assert_response :success
16
+ assert_not_nil controller.posts
17
+ end
18
+ end
19
+
20
+ class NewTest < TestCase
21
+ test "should get new" do
22
+ req
23
+ assert_response :success
24
+ end
25
+ end
26
+
27
+ class CreateTest < TestCase
28
+ test "should create post" do
29
+ assert_difference('Post.count') do
30
+ req :post => @post.attributes
31
+ end
32
+
33
+ assert_redirected_to post_path(controller.post)
34
+ end
35
+ end
36
+
37
+ class ShowTest < TestCase
38
+ test "should show post" do
39
+ req :id => @post.id
40
+ assert_response :success
41
+ end
42
+ end
43
+
44
+ class EditTest < TestCase
45
+ test "should get edit" do
46
+ req :id => @post.id
47
+ assert_response :success
48
+ end
49
+ end
50
+
51
+ class UpdateTest < TestCase
52
+ test "should update post" do
53
+ req :id => @post.id
54
+ assert_redirected_to post_path(controller.post)
55
+ end
56
+ end
57
+
58
+ class DestroyTest < TestCase
59
+ test "should destroy post" do
60
+ assert_difference('Post.count', -1) do
61
+ req :id => @post.id
62
+ end
63
+
64
+ assert_redirected_to posts_path
65
+ end
66
+ end
67
+ end
File without changes
File without changes
File without changes
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'bundler/setup'
2
+ require 'test/unit/testcase'
3
+ require 'active_support/test_case'
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'focused_controller'
7
+ require 'pathname'
8
+ require 'ostruct'
9
+ require 'rspec/core'
10
+
11
+ TEST_ROOT = File.expand_path('..', __FILE__)
12
+
13
+ # Don't want to actually use RSpec to run our tests
14
+ module RSpec::Core::DSL
15
+ remove_method :describe
16
+ end
17
+
18
+ # Annoying monkey-patches. "require 'rspec/rails'" pulls in 'capybara/rails', if it
19
+ # can, and capybara/rails assumes there is a full rails env present. So this is a
20
+ # hack to make it not fail.
21
+ module Rails
22
+ def self.version
23
+ '3.0'
24
+ end
25
+
26
+ def self.root
27
+ Pathname.new('')
28
+ end
29
+
30
+ def self.application
31
+ OpenStruct.new(:env_config => {}, :env_defaults => {})
32
+ end
33
+ end
@@ -0,0 +1,65 @@
1
+ require 'helper'
2
+ require 'focused_controller/functional_test_helper'
3
+ require 'action_controller'
4
+
5
+ module FocusedController
6
+ module FunctionalTestHelper
7
+ class FakePostsController
8
+ class Action < ActionController::Base; end
9
+ class Index < Action; end
10
+ class Show < Action; end
11
+
12
+ class TestCase < ActionController::TestCase
13
+ include FocusedController::FunctionalTestHelper
14
+
15
+ def initialize(method_name = :foo)
16
+ super
17
+ end
18
+
19
+ def foo; end
20
+ end
21
+
22
+ class IndexTest < TestCase
23
+ end
24
+
25
+ class ShowTest < TestCase
26
+ end
27
+ end
28
+
29
+ describe FunctionalTestHelper do
30
+ subject { FakePostsController::IndexTest.new }
31
+
32
+ it 'automatically determines the controller class' do
33
+ FakePostsController::IndexTest.controller_class.
34
+ must_equal FakePostsController::Index
35
+ FakePostsController::ShowTest.controller_class.
36
+ must_equal FakePostsController::Show
37
+ end
38
+
39
+ it "doesn't require using the action name to dispatch the action" do
40
+ subject.singleton_class.class_eval do
41
+ attr_reader :last_process
42
+
43
+ def process(*args)
44
+ @last_process = args
45
+ end
46
+ end
47
+
48
+ subject.get :foo, :bar, :baz
49
+ subject.last_process.must_equal ['run', :foo, :bar, :baz, 'GET']
50
+
51
+ subject.post :foo, :bar, :baz
52
+ subject.last_process.must_equal ['run', :foo, :bar, :baz, 'POST']
53
+
54
+ subject.put :foo, :bar, :baz
55
+ subject.last_process.must_equal ['run', :foo, :bar, :baz, 'PUT']
56
+
57
+ subject.delete :foo, :bar, :baz
58
+ subject.last_process.must_equal ['run', :foo, :bar, :baz, 'DELETE']
59
+
60
+ subject.head :foo, :bar, :baz
61
+ subject.last_process.must_equal ['run', :foo, :bar, :baz, 'HEAD']
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+ require 'action_controller'
3
+
4
+ module FocusedController
5
+ module Test
6
+ class MixinTestBaseController
7
+ def view_assigns
8
+ {'some' => 'var'}
9
+ end
10
+ end
11
+
12
+ class MixinTestController < ActionController::Base
13
+ include FocusedController::Mixin
14
+
15
+ class << self
16
+ attr_accessor :name
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module FocusedController
23
+ describe Mixin do
24
+ describe "with a PostsController::Show class" do
25
+ let(:klass) do
26
+ klass = Class.new(FocusedController::Test::MixinTestController)
27
+ klass.name = "PostsController::Show"
28
+ klass
29
+ end
30
+
31
+ subject { klass.new }
32
+
33
+ it "has a .controller_path of 'posts'" do
34
+ klass.controller_path.must_equal 'posts'
35
+ end
36
+
37
+ it "has a .call which dispatches the #run action" do
38
+ def klass.action(name)
39
+ if name.to_s == 'run'
40
+ proc { |env| "omg" }
41
+ end
42
+ end
43
+
44
+ klass.call(nil).must_equal "omg"
45
+ end
46
+
47
+ it "has an #action_name of 'show'" do
48
+ subject.action_name.must_equal 'show'
49
+ end
50
+
51
+ it "uses the run method for the action" do
52
+ subject.method_for_action('whatever').must_equal 'run'
53
+ end
54
+
55
+ it "removes all view assigns by default" do
56
+ subject.view_assigns.must_equal({})
57
+ end
58
+
59
+ it "can be configured to allow view assigns" do
60
+ subject.class.allow_view_assigns = true
61
+ subject.instance_variable_set('@foo', 'bar')
62
+ subject.view_assigns['foo'].must_equal('bar')
63
+ end
64
+
65
+ it "has a #run method by default" do
66
+ subject.run.must_equal nil
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,73 @@
1
+ require 'helper'
2
+
3
+ # RouteSet is missing some requires
4
+ require 'active_support/core_ext/uri'
5
+ require 'active_support/core_ext/enumerable'
6
+
7
+ module FocusedController
8
+ describe RouteMapper do
9
+ let(:route_set) { ActionDispatch::Routing::RouteSet.new }
10
+
11
+ def recognize(path, environment = {})
12
+ method = (environment[:method] || "GET").to_s.upcase
13
+ env = Rack::MockRequest.env_for(path, {:method => method})
14
+ req = route_set.request_class.new(env)
15
+
16
+ if route_set.respond_to?(:router)
17
+ router = route_set.router # Rails 3.2+
18
+ else
19
+ router = route_set.set # Rails 3.0, 3.1
20
+ end
21
+
22
+ router.recognize(req) do |route, matches, params|
23
+ return route
24
+ end
25
+ end
26
+
27
+ it 'creates routes that map to focused controllers' do
28
+ route_set.draw do
29
+ focused_controller_routes do
30
+ match 'posts' => 'PostsController::Index'
31
+
32
+ resources :comments do
33
+ resources :replies
34
+ end
35
+
36
+ resource :account
37
+
38
+ namespace :admin do
39
+ resources :comments
40
+ end
41
+ end
42
+ end
43
+
44
+ mappings = {
45
+ [:get, '/posts'] => 'PostsController::Index',
46
+ [:get, '/comments'] => 'CommentsController::Index',
47
+ [:get, '/comments/4'] => 'CommentsController::Show',
48
+ [:put, '/comments/4'] => 'CommentsController::Update',
49
+ [:get, '/account'] => 'AccountsController::Show',
50
+ [:get, '/comments/4/replies'] => 'RepliesController::Index',
51
+ [:get, '/admin/comments'] => 'Admin::CommentsController::Index'
52
+ }
53
+
54
+ mappings.each do |(method, path), controller|
55
+ route = recognize(path, :method => method)
56
+ route.app.name.must_equal controller
57
+ route.defaults[:action].must_equal 'run'
58
+ end
59
+ end
60
+
61
+ it "doesn't mess with callable routes" do
62
+ app = Object.new
63
+ def app.call; end
64
+
65
+ route_set.draw do
66
+ focused_controller_routes do
67
+ match 'posts' => app
68
+ end
69
+ end
70
+ recognize('/posts').app.must_equal app
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,39 @@
1
+ require 'helper'
2
+
3
+ module FocusedController
4
+ module Test
5
+ end
6
+
7
+ describe Route do
8
+ let(:controller) { Object.new }
9
+ subject { Route.new('FocusedController::Test::RouteTestController') }
10
+
11
+ before do
12
+ Test.const_set(:RouteTestController, controller)
13
+ end
14
+
15
+ after do
16
+ Test.send(:remove_const, :RouteTestController)
17
+ end
18
+
19
+ describe '#call' do
20
+ it 'constantizes the name and invokes #call on the constant' do
21
+ env, resp = Object.new, Object.new
22
+
23
+ # Not using MiniTest::Mock for this because it caused problems
24
+ # with Rubinius
25
+ controller.singleton_class.send :define_method, :call do |call_env|
26
+ resp if call_env == env
27
+ end
28
+
29
+ subject.call(env).must_equal resp
30
+ end
31
+ end
32
+
33
+ describe '#to_s' do
34
+ it "returns the the name" do
35
+ subject.to_s.must_equal 'FocusedController::Test::RouteTestController'
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ require 'helper'
2
+ require 'focused_controller/rspec_functional_helper'
3
+
4
+ module FocusedController
5
+ module RSpecFunctionalHelper
6
+ class FakePostsController
7
+ class Action < ActionController::Base; end
8
+ class Index < Action; end
9
+ class Show < Action; end
10
+ end
11
+
12
+ index_spec = RSpec::Core::ExampleGroup.describe FakePostsController::Index do
13
+ include RSpec::Rails::ControllerExampleGroup
14
+ include FocusedController::RSpecFunctionalHelper
15
+ end
16
+
17
+ show_spec = nil
18
+ inner_show_spec = nil
19
+ RSpec::Core::ExampleGroup.describe FakePostsController do
20
+ include RSpec::Rails::ControllerExampleGroup
21
+ include FocusedController::RSpecFunctionalHelper
22
+
23
+ show_spec = describe(FakePostsController::Show) do
24
+ inner_show_spec = describe('foo') { }
25
+ end
26
+ end
27
+
28
+ describe RSpecFunctionalHelper do
29
+ subject { index_spec.new }
30
+
31
+ it 'automatically determines the controller class' do
32
+ index_spec.controller_class.must_equal FakePostsController::Index
33
+ show_spec.controller_class.must_equal FakePostsController::Show
34
+ inner_show_spec.controller_class.must_equal FakePostsController::Show
35
+ end
36
+
37
+ it 'includes the FocusedController::FunctionalTestHelper' do
38
+ subject.is_a?(FocusedController::FunctionalTestHelper).must_equal true
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,91 @@
1
+ require 'helper'
2
+ require 'action_controller'
3
+ require 'focused_controller/rspec_helper'
4
+
5
+ module FocusedController
6
+ module RSpecHelper
7
+ module FakePostsController
8
+ class Action < ActionController::Base
9
+ end
10
+
11
+ class Index < Action
12
+ end
13
+
14
+ class Show < Action
15
+ end
16
+
17
+ class Edit < Action
18
+ end
19
+
20
+ class Destroy < Action
21
+ end
22
+ end
23
+
24
+ index_spec = RSpec::Core::ExampleGroup.describe FakePostsController::Index do
25
+ include FocusedController::RSpecHelper
26
+ end
27
+
28
+ show_spec = nil
29
+ inner_show_spec = nil
30
+ RSpec::Core::ExampleGroup.describe FakePostsController do
31
+ show_spec = describe FakePostsController::Show do
32
+ include FocusedController::RSpecHelper
33
+
34
+ inner_show_spec = describe 'foo' do
35
+ end
36
+ end
37
+ end
38
+
39
+ edit_spec = RSpec::Core::ExampleGroup.describe "the edit action" do
40
+ include FocusedController::RSpecHelper
41
+ self.controller_class = FakePostsController::Edit
42
+ end
43
+
44
+ describe RSpecHelper do
45
+ it 'finds the correct controller class' do
46
+ index_spec.controller_class.must_equal FakePostsController::Index
47
+ show_spec.controller_class.must_equal FakePostsController::Show
48
+ inner_show_spec.controller_class.must_equal FakePostsController::Show
49
+ edit_spec.controller_class.must_equal FakePostsController::Edit
50
+ end
51
+
52
+ subject { index_spec.new }
53
+
54
+ def must_fail(&block)
55
+ block.must_raise RSpec::Expectations::ExpectationNotMetError
56
+ end
57
+
58
+ def must_succeed(&block)
59
+ block.call
60
+ true.must_equal true # to count the assertion
61
+ end
62
+
63
+ it 'has a redirect_to matcher' do
64
+ must_fail { subject.should subject.redirect_to('/foo') }
65
+ subject.controller.redirect_to('/foo')
66
+ must_succeed { subject.should subject.redirect_to('/foo') }
67
+ end
68
+
69
+ it 'matches response type' do
70
+ must_succeed { subject.response.should subject.be_success }
71
+ must_fail { subject.response.should subject.be_error }
72
+
73
+ subject.controller.render :status => :not_found
74
+
75
+ must_fail { subject.response.should subject.be_success }
76
+ must_succeed { subject.response.should subject.be_missing }
77
+ end
78
+
79
+ it 'has a render_template matcher' do
80
+ subject.controller.render :foo
81
+
82
+ must_succeed { subject.response.should subject.render_template(:foo) }
83
+ must_fail { subject.response.should subject.render_template(:bar) }
84
+ end
85
+
86
+ it 'sets the subject to be the controller instance' do
87
+ subject.subject.must_equal subject.controller
88
+ end
89
+ end
90
+ end
91
+ end