Skip to content

Instantly share code, notes, and snippets.

@lucassus
Created April 22, 2011 16:07
Show Gist options
  • Save lucassus/936947 to your computer and use it in GitHub Desktop.
Save lucassus/936947 to your computer and use it in GitHub Desktop.
InheritedResources and RSpec mock_model
(rdb:1) ap e.backtrace
[
[ 0] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'",
[ 1] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_dispatch/routing/url_for.rb:133:in `url_for'",
[ 2] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/redirecting.rb:89:in `_compute_redirect_to_location'",
[ 3] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/redirecting.rb:61:in `redirect_to'",
[ 4] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/flash.rb:25:in `redirect_to'",
[ 5] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:60:in `block in redirect_to'",
[ 6] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `block in instrument'",
[ 7] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'",
[ 8] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `instrument'",
[ 9] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:59:in `redirect_to'",
[10] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:101:in `redirect_to'",
[11] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:150:in `navigation_behavior'",
[12] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:128:in `rescue in to_html'",
[13] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:126:in `to_html'",
[14] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/responders-0.6.2/lib/responders/flash_responder.rb:93:in `to_html'",
[15] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:119:in `respond'",
[16] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/responder.rb:112:in `call'",
[17] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/mime_responds.rb:232:in `respond_with'",
[18] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/inherited_resources-1.2.1/lib/inherited_resources/base_helpers.rb:247:in `respond_with_dual_blocks'",
[19] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/inherited_resources-1.2.1/lib/inherited_resources/actions.rb:37:in `create'",
[20] "/home/lucassus/Projects/sample_project/app/controllers/admin/branches_controller.rb:8:in `create'",
[21] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/implicit_render.rb:4:in `send_action'",
[22] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/abstract_controller/base.rb:150:in `process_action'",
[23] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/rendering.rb:11:in `process_action'",
[24] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/abstract_controller/callbacks.rb:18:in `block in process_action'",
[25] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:445:in `_run__363738802__process_action__276696986__callbacks'",
[26] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'",
[27] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:93:in `run_callbacks'",
[28] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/abstract_controller/callbacks.rb:17:in `process_action'",
[29] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'",
[30] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `block in instrument'",
[31] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'",
[32] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `instrument'",
[33] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:29:in `process_action'",
[34] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/rescue.rb:17:in `process_action'",
[35] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/abstract_controller/base.rb:119:in `process'",
[36] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/abstract_controller/rendering.rb:41:in `process'",
[37] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'",
[38] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:412:in `process'",
[39] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:47:in `process'",
[40] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:355:in `post'",
[41] "/home/lucassus/Projects/sample_project/spec/controllers/admin/branches_controller_spec.rb:58:in `block (4 levels) in <top (required)>'",
[42] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval'",
[43] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `run_in'",
[44] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `block in run_all'",
[45] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `each'",
[46] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all'",
[47] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:110:in `run_hook'",
[48] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs'",
[49] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `each'",
[50] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'",
[51] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:144:in `run_before_each'",
[52] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:48:in `block (2 levels) in run'",
[53] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'",
[54] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `block in run'",
[55] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `block in with_pending_capture'",
[56] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'",
[57] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'",
[58] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'",
[59] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `block in run_examples'",
[60] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'",
[61] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `run_examples'",
[62] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:232:in `run'",
[63] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `block in run'",
[64] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'",
[65] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'",
[66] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `block in run'",
[67] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'",
[68] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'",
[69] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `block in run'",
[70] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'",
[71] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'",
[72] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run'",
[73] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `map'",
[74] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block in run'",
[75] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/reporter.rb:12:in `report'",
[76] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:24:in `run'",
[77] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'",
[78] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:44:in `run'",
[79] "/home/lucassus/Projects/sample_project/vendor/gems/ruby/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'"
]
require 'spec_helper'
describe Admin::BranchesController do
login_admin
let(:resource_parent) { mock_model(School, :to_param => ':shool_id') }
let(:resource) { mock_model(Branch, :to_param => ':id') }
let(:resource_model) { resource.class }
let(:resource_name) { resource_model.to_s.underscore.to_s }
let(:resource_collection_name) { resource_name.to_s.pluralize.to_sym }
def translated_flash_for(action, type)
I18n.t(:"flash.#{resource_collection_name}.#{action}.#{type}")
end
def translated_flash_notice_for(action)
translated_flash_for(action, :notice)
end
before do
School.should_receive(:find).with(resource_parent.to_param).and_return(resource_parent)
end
describe "on POST to :create" do
let(:params) { {:street => 'Hell Street'}.stringify_keys }
before do
resource_parent.stub_chain(:branches, :build).with(params).and_return(resource.as_new_record)
end
shared_examples_for "successful InheritedResources :update action" do
it { should assign_to(resource_name).with(resource) }
it { should set_the_flash.to(translated_flash_notice_for(:update)) }
it { should respond_with(:redirect) }
it "should redirect to the :show resource action" do
should redirect_to(:action => :show, :id => resource.to_param)
end
end
shared_examples_for "unsuccessful InheritedResources :update action" do
it { should respond_with(:success) }
it { should assign_to(resource_name) { resource } }
it { should_not set_the_flash }
it { should render_template(:edit) }
end
shared_examples_for "successful InheritedResources :create action" do
it { should assign_to(resource_name) }
it { should set_the_flash.to(translated_flash_notice_for(:create)) }
it { should respond_with(:redirect) }
it "should redirect to the :show resource action" do
should redirect_to(:action => :show, :id => resource.to_param)
end
end
shared_examples_for "unsuccessful InheritedResources :create action" do
it { should respond_with(:success) }
it { should assign_to(resource_name) { resource } }
it { should_not set_the_flash }
it { should render_template(:new) }
end
context "with valid attributes" do
before do
resource.should_receive(:save).and_return(true)
post :create, :school_id => resource_parent.to_param, :branch => params
end
it_should_behave_like "successful InheritedResources :create action"
end
context "with invalid attributes" do
before do
resource.should_receive(:save).and_return(false)
post :create, :school_id => resource_parent.to_param, :branch => params
end
it_should_behave_like "unsuccessful InheritedResources :create action"
end
end
describe "on PUT to :update" do
let(:params) { {:street => 'Sezamkowa'}.stringify_keys }
before do
resource_parent.stub_chain(:branches, :find).with(resource.to_param).and_return(resource)
end
context "with valid attributes" do
before do
resource.should_receive(:update_attributes).and_return(true)
put :update, :school_id => resource_parent.to_param, :id => resource.to_param, :branch => params
end
it_should_behave_like "successful InheritedResources :update action"
end
context "with invalid attributes" do
before do
resource.should_receive(:update_attributes).and_return(false)
put :update, :school_id => resource_parent.to_param, :id => resource.to_param, :branch => params
end
it_should_behave_like "unsuccessful InheritedResources :update action"
end
end
end
class Admin::BranchesController < MyApp::AdminBaseController
inherit_resources
belongs_to :school
end
MyApp::Application.routes.draw do
devise_for :users
root :to => 'index#index'
namespace :admin do
resources :regions do
resources :cities
end
resources :cities do
resources :schools
end
resources :schools do
resources :branches
end
resources :branches do
resources :courses
end
root :to => 'index#index'
end
end
bundle exec rspec spec/controllers/admin/branches_controller_spec.rb -fd
No DRb server is running. Running in local process instead ...
Admin::BranchesController
on POST to :create
with valid attributes
it should behave like successful InheritedResources :create action
should assign @branch
should set the flash to "Oddział został dodany."
should respond with 300..399
should redirect to the :show resource action
with invalid attributes
it should behave like unsuccessful InheritedResources :create action
should redirect to {:action=>:show, :id=>":id", :host=>"test.host", :protocol=>"http://", :_path_segments=>{:school_id=>":shool_id", :controller=>"admin/branches", :action=>"create"}, :script_name=>""} (FAILED - 1)
(FAILED - 2)
(FAILED - 3)
(FAILED - 4)
on PUT to :update
with valid attributes
it should behave like successful InheritedResources :update action
should assign @branch
should set the flash to "Oddział został zapisany."
should respond with 300..399
should redirect to the :show resource action
with invalid attributes
it should behave like unsuccessful InheritedResources :update action
should redirect to {:action=>:show, :id=>":id", :host=>"test.host", :protocol=>"http://", :_path_segments=>{:school_id=>":shool_id", :id=>":id", :controller=>"admin/branches", :action=>"update"}, :script_name=>""} (FAILED - 5)
(FAILED - 6)
(FAILED - 7)
(FAILED - 8)
Failures:
1) Admin::BranchesController on POST to :create with invalid attributes it should behave like unsuccessful InheritedResources :create action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branches_url' for #<Admin::BranchesController:0x000000041b7e80>
Shared Example Group: "unsuccessful InheritedResources :create action" called from ./spec/controllers/admin/branches_controller_spec.rb:61
# ./spec/controllers/admin/branches_controller_spec.rb:58:in `block (4 levels) in <top (required)>'
2) Admin::BranchesController on POST to :create with invalid attributes it should behave like unsuccessful InheritedResources :create action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branches_url' for #<Admin::BranchesController:0x00000006608b18>
Shared Example Group: "unsuccessful InheritedResources :create action" called from ./spec/controllers/admin/branches_controller_spec.rb:61
# ./spec/controllers/admin/branches_controller_spec.rb:58:in `block (4 levels) in <top (required)>'
3) Admin::BranchesController on POST to :create with invalid attributes it should behave like unsuccessful InheritedResources :create action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branches_url' for #<Admin::BranchesController:0x00000006454a38>
Shared Example Group: "unsuccessful InheritedResources :create action" called from ./spec/controllers/admin/branches_controller_spec.rb:61
# ./spec/controllers/admin/branches_controller_spec.rb:58:in `block (4 levels) in <top (required)>'
4) Admin::BranchesController on POST to :create with invalid attributes it should behave like unsuccessful InheritedResources :create action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branches_url' for #<Admin::BranchesController:0x0000000517f818>
Shared Example Group: "unsuccessful InheritedResources :create action" called from ./spec/controllers/admin/branches_controller_spec.rb:61
# ./spec/controllers/admin/branches_controller_spec.rb:58:in `block (4 levels) in <top (required)>'
5) Admin::BranchesController on PUT to :update with invalid attributes it should behave like unsuccessful InheritedResources :update action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branch_url' for #<Admin::BranchesController:0x00000002fcd350>
Shared Example Group: "unsuccessful InheritedResources :update action" called from ./spec/controllers/admin/branches_controller_spec.rb:95
# ./spec/controllers/admin/branches_controller_spec.rb:92:in `block (4 levels) in <top (required)>'
6) Admin::BranchesController on PUT to :update with invalid attributes it should behave like unsuccessful InheritedResources :update action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branch_url' for #<Admin::BranchesController:0x00000001e2d550>
Shared Example Group: "unsuccessful InheritedResources :update action" called from ./spec/controllers/admin/branches_controller_spec.rb:95
# ./spec/controllers/admin/branches_controller_spec.rb:92:in `block (4 levels) in <top (required)>'
7) Admin::BranchesController on PUT to :update with invalid attributes it should behave like unsuccessful InheritedResources :update action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branch_url' for #<Admin::BranchesController:0x000000065994e8>
Shared Example Group: "unsuccessful InheritedResources :update action" called from ./spec/controllers/admin/branches_controller_spec.rb:95
# ./spec/controllers/admin/branches_controller_spec.rb:92:in `block (4 levels) in <top (required)>'
8) Admin::BranchesController on PUT to :update with invalid attributes it should behave like unsuccessful InheritedResources :update action
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `school_branch_url' for #<Admin::BranchesController:0x00000006620ce0>
Shared Example Group: "unsuccessful InheritedResources :update action" called from ./spec/controllers/admin/branches_controller_spec.rb:95
# ./spec/controllers/admin/branches_controller_spec.rb:92:in `block (4 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment