No more @controller.expects in Test::Unit?

12 views
Skip to first unread message

Fernando Perez

unread,
Jul 26, 2010, 9:15:53 AM7/26/10
to rubyonra...@googlegroups.com
Hi,

In my functional tests it was convenient to use:

@controller.expects(:admin?).at_least_once.returns(true)

to simulate requests which come from an admin user. But it seems that in
Rails 2.3.8 it's gone away. What replaces it now?


Thanks
--
Posted via http://www.ruby-forum.com/.

Fernando Perez

unread,
Jul 27, 2010, 4:56:39 PM7/27/10
to rubyonra...@googlegroups.com
Fernando Perez wrote:
> Hi,
>
> In my functional tests it was convenient to use:
>
> @controller.expects(:admin?).at_least_once.returns(true)
>
> to simulate requests which come from an admin user. But it seems that in
> Rails 2.3.8 it's gone away. What replaces it now?

Anyone? Currently my apps are running test less because I can't use
expects or whatever method to stub out some behavior.

Where could I find relevant information about that?

Fernando Perez

unread,
Jul 29, 2010, 10:53:40 AM7/29/10
to rubyonra...@googlegroups.com
>> In my functional tests it was convenient to use:
>>
>> @controller.expects(:admin?).at_least_once.returns(true)
>>
>> to simulate requests which come from an admin user. But it seems that in
>> Rails 2.3.8 it's gone away. What replaces it now?

Up, it's important. Nobody uses functional tests here?

Frederick Cheung

unread,
Jul 29, 2010, 12:15:20 PM7/29/10
to Ruby on Rails: Talk


On Jul 29, 3:53 pm, Fernando Perez <li...@ruby-forum.com> wrote:
> >> In my functional tests it was convenient to use:
>
> >> @controller.expects(:admin?).at_least_once.returns(true)
>
> >> to simulate requests which come from an admin user. But it seems that in
> >> Rails 2.3.8 it's gone away. What replaces it now?
>
> Up, it's important. Nobody uses functional tests here?

Should still work. What happens instead? What version were you
migrating from? That stuff is setup by ActionController::TestCase

Fred

Fernando Perez

unread,
Jul 29, 2010, 12:23:57 PM7/29/10
to rubyonra...@googlegroups.com
Hi Fred,


Here is some code:
------------------

require 'test_helper'

class Admin::OrdersControllerTest < ActionController::TestCase

def setup
@controller = Admin::OrdersController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller.expects(:admin?).at_least_once.returns(true)
@order = Factory.order
end

def test_index
get :index
assert_response :success
end
end


And here is the error message:
------------------------------

1) Error:
test_index(Admin::OrdersControllerTest):
NoMethodError: undefined method `expects' for
#<Admin::OrdersController:0x229a32c @real_format=nil>

Frederick Cheung

unread,
Jul 29, 2010, 4:52:58 PM7/29/10
to Ruby on Rails: Talk


On Jul 29, 5:23 pm, Fernando Perez <li...@ruby-forum.
>
> class Admin::OrdersControllerTest < ActionController::TestCase
>
>   def setup
>     @controller = Admin::OrdersController.new
>     @request    = ActionController::TestRequest.new
>     @response   = ActionController::TestResponse.new
>     @controller.expects(:admin?).at_least_once.returns(true)
>     @order = Factory.order
>   end

Do you explicitly require mocha somewhere? By the way you no longer
need to setup @controller/@request/@response -
actioncontroller::testcase does that for you

Fred

Fred

Fernando Perez

unread,
Jul 30, 2010, 5:53:25 AM7/30/10
to rubyonra...@googlegroups.com
> Do you explicitly require mocha somewhere?

I don't use mocha. Did rails delegate the stubbing to mocha recently? So
that could be my missing piece of the puzzle. Where should I require it
in? test_helper.rb?


> By the way you no longer need to setup @controller/@request/@response -
> actioncontroller::testcase does that for you

Indeed, I'll update my tests accordingly, thanks for pointing that out.

Fernando Perez

unread,
Jul 30, 2010, 6:00:47 AM7/30/10
to rubyonra...@googlegroups.com
It seems that simply installing the mocha gem solved my problem.

Frederick Cheung

unread,
Jul 30, 2010, 6:14:01 AM7/30/10
to Ruby on Rails: Talk


On Jul 30, 10:53 am, Fernando Perez <li...@ruby-forum.com> wrote:
> > Do you explicitly require mocha somewhere?
>
> I don't use mocha. Did rails delegate the stubbing to mocha recently? So
> that could be my missing piece of the puzzle. Where should I require it
> in? test_helper.rb?
>

it always has, but rails may have been accidentally requiring it for
you

Fred

Fernando Perez

unread,
Jul 30, 2010, 7:47:22 AM7/30/10
to rubyonra...@googlegroups.com
> it always has, but rails may have been accidentally requiring it for
> you
>
> Fred

That makes sense. Thanks Fred.

Reply all
Reply to author
Forward
0 new messages