Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created October 22, 2010 16:12
Show Gist options
  • Select an option

  • Save floehopper/640838 to your computer and use it in GitHub Desktop.

Select an option

Save floehopper/640838 to your computer and use it in GitHub Desktop.
mocha-mailing-list-question-
# I have a setup method in my controller test that looks like this:
def setup
@order = Order.new
@order.save_new
session[:order_id] = @order.id
end
# I have a test that looks like this:
test "order in progress less than five minutes gets redirected to process_order action" do
Transaction.expects(:validation_without_rails).returns({})
o = Order.create
@controller.expects(:current_order).with(false).returns(o)
o.expects(:updated_at).returns(1.minute.ago)
o.expects(:state).returns("InProgress")
get :process_order
assert_redirected_to :action => 'process_order'
end
# This test passes, but what I initially tried was
test "order in progress less than five minutes gets redirected to process_order action" do
Transaction.expects(:validation_without_rails).returns({})
Order.any_instance.stubs(:updated_at).returns(1.minute.ago)
Order.any_instance.stubs(:state).returns("InProgress")
get :process_order
assert_redirected_to :action => 'process_order'
end
# and this failed. Somehow the #current_order method of the controller
# is returning an object that is not stubbed. StoreController <
# ApplicationController and #current_order defined on
# ApplicationController like so:
def current_order(create_if_nil=true)
order_id = session[:order_id]
if order_id
@order = Order.find(order_id) rescue nil
end
if @order.nil? && create_if_nil
@order = Order.new
@order.save_new
session[:order_id] = @order.id
end
@order
end
# It's pretty standard I feel. I tried to reproduce the error in a test
# project, but there must be something I'm missing. The #save_new method
# isn't terribly interesting as it just sets some default values and
# does some checks before saving the Order to the database with state
# set to "New".
# I've double-checked that mocha is being required after test/unit and
# all of that other 0.9.6+ jazz. What am I missing? At this point, I've
# worked around the issue by stubbing the current_order method, but I
# think my bigger concern is with something like this biting me again in
# the future if I don't understand how stubbing actually works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment