How to do controller.stub(:some_method) at request test when using rspec2

Hi,
I hava a controller

class DynamicFlowsController < ApplicationController

def create
@workflow = Workflow.find_by_id(params[:workflow_id])
table = @workflow.flex_table
save_u_and_node(table, @workflow.node_definition)
redirect_to “/#{table.name}”
end

end

and I had written some rspec codes

describe “save attachment” do

before(:each) do
  workflow, table, form = init_stubs("baoxiaos", 1)
  definition = workflow.node_definition
  controller.should_receive(:save_u_and_node).with(table,

definition)
end

it "should be successful" do
  file = UploadFile.new("a" * 10000, "testfile", "text/plain")
  post '/workflows/1/baoxiaos', { :attachment => file}
  response.should be_successful
end

end

When I run the test, unfortunately rspec told me that “controller” is
nil, I got a fail test.
So, I want to konw how to get the correct “controller” object.

Thanks
Kayak

On 3 Mar 2011, at 07:36, Jiang G. wrote:

end
controller.should_receive(:save_u_and_node).with(table,

rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Are you wanting to stub a method of the controller you are actually
testing?
I’m pretty sure this is bad practice.

On Mar 3, 2011, at 1:36 AM, Jiang G. wrote:

end
controller.should_receive(:save_u_and_node).with(table,

When I run the test, unfortunately rspec told me that “controller” is
nil, I got a fail test.
So, I want to konw how to get the correct “controller” object.

Please run it again with the --backtrace flag and post the output.

Rob A. wrote in post #985198:

On 3 Mar 2011, at 07:36, Jiang G. wrote:

end
controller.should_receive(:save_u_and_node).with(table,

rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Are you wanting to stub a method of the controller you are actually
testing?
I’m pretty sure this is bad practice.

You are right, it’s bad practice, i got nightmare because of
“save_u_and_node”. “save_u_and_node” is a huge code block that was
written by my colleagues some months ago, and i need add some features
based at “save_u_and_node”, but i can’t find any spec codes for
“save_u_and_node”, so i had to stub “save_u_and_node” in controller
roughly in order to pass spec.
Thanks
This forum’s email notification featrue seems dosen’t work

David C. wrote in post #985199:

On Mar 3, 2011, at 1:36 AM, Jiang G. wrote:

end
controller.should_receive(:save_u_and_node).with(table,

When I run the test, unfortunately rspec told me that “controller” is
nil, I got a fail test.
So, I want to konw how to get the correct “controller” object.

Please run it again with the --backtrace flag and post the output.

Thanks
This forum’s email notification featrue seems dosen’t work.

Ok, I run it again then rspec told me so mush, and I noted the message
“An expectation of :save_u_and_node was set on nil”, is it said that
controller is nil?
Why rspec can’t get controller in requests test for this spec?

No DRb server is running. Running in local process instead …
…An expectation of :save_u_and_node was set on nil. Called from
/home/yang/workspace/trunk/ent_os/spec/requests/workflows/dynamic_flows_spec.rb:57:in
`block (3 levels) in <top (required)>’. Use
allow_message_expectations_on_nil to disable warnings.
F

Failures:

  1. 运行报表 save attachment should be successful
    Failure/Error: click_button “提交”
    NoMethodError:
    You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.each

/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in
transaction' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:intransaction’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/implicit_render.rb:4:in
send_action' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/abstract_controller/base.rb:151:inprocess_action’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/rendering.rb:11:in
process_action' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/abstract_controller/callbacks.rb:18:inblock in process_action’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:435:in
_run__33140632__process_action__340421511__callbacks' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:409:in_run_process_action_callbacks’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in
run_callbacks' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/abstract_controller/callbacks.rb:17:inprocess_action’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/instrumentation.rb:30:in
block in process_action' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:inblock in instrument’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications/instrumenter.rb:21:in
instrument' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:ininstrument’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/instrumentation.rb:29:in
process_action' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/rescue.rb:17:inprocess_action’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/abstract_controller/base.rb:120:in
process' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/abstract_controller/rendering.rb:40:inprocess’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal.rb:138:in
dispatch' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal/rack_delegation.rb:14:indispatch’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_controller/metal.rb:178:in
block in action' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:62:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:62:in
dispatch' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:27:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/route_set.rb:148:in
block in call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:93:inblock in recognize’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:89:in
optimized_each' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/code_generation.rb:92:inrecognize’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-mount-0.6.13/lib/rack/mount/route_set.rb:139:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:492:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/best_standards_support.rb:17:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/head.rb:14:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/methodoverride.rb:24:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/params_parser.rb:21:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/flash.rb:182:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/session/abstract_store.rb:149:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/cookies.rb:295:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/query_cache.rb:32:inblock in call’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/query_cache.rb:28:in
cache' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/query_cache.rb:12:incache’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/query_cache.rb:31:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:353:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:46:in
block in call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:415:in_run_call_callbacks’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:44:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/sendfile.rb:107:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/remote_ip.rb:48:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/rack/logger.rb:13:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/runtime.rb:17:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/cache/strategy/local_cache.rb:72:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in
block in call' # <internal:prelude>:10:insynchronize’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/static.rb:30:incall’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/application.rb:168:in
call' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.6/lib/rack/mock_session.rb:30:inrequest’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.6/lib/rack/test.rb:209:in
process_request' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.6/lib/rack/test.rb:66:inpost’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/session.rb:278:in
process_request' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/session.rb:119:inrequest_page’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/elements/form.rb:20:in
submit' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/elements/field.rb:193:inclick’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/scope.rb:291:in
click_button' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/webrat-0.7.3/lib/webrat/core/methods.rb:7:inclick_button’
# ./spec/requests/workflows/dynamic_flows_spec.rb:62:in block (3 levels) in <top (required)>' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:29:ininstance_eval’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:29:in
run_in' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:60:inblock in run_all’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:60:in
each' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:60:inrun_all’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/hooks.rb:106:in
run_hook' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:190:inblock in eval_before_eachs’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:190:in
each' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:190:ineval_before_eachs’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:136:in
run_before_each' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:48:inblock (2 levels) in run’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:98:in
with_around_hooks' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:46:inblock in run’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:91:in
block in with_pending_capture' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:90:incatch’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:90:in
with_pending_capture' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example.rb:45:inrun’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:261:in
block in run_examples' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:257:inmap’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:257:in
run_examples' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:231:inrun’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:232:in
block in run' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:232:inmap’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/example_group.rb:232:in
run' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:27:inblock (2 levels) in run’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:27:in
map' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:27:inblock in run’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/reporter.rb:12:in
report' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:24:inrun’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:55:in
run_in_process' # /home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:44:inrun’
#
/home/yang/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:10:in
`block in autorun’

You should be able to call:
controller.stub(:some_method)

My suspicion is what you passed to the describe method.

Instead of:
describe “save attachment”

pass the controller name, without quotes:
describe DynamicFlowsController

then
controller.stub

should work, and then you can refactor the dependency on save_u_and_node
later.

Ken

2011/3/8 Jiang G. [email protected]

Kenrick Chien wrote in post #986591:

pass the controller name, without quotes:
describe DynamicFlowsController

then
controller.stub

should work, and then you can refactor the dependency on save_u_and_node
later.

Ken

Thanks, it’s work now! But I can’t get the controller object for
integration test.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs