Rails 2 and setup in controller tests

28 views
Skip to first unread message

Zach

unread,
Dec 23, 2007, 9:17:40 PM12/23/07
to shoulda
I've been working with Shoulda on Rails 2 and it seems like it falls
down on one scenario when testing controllers:

------
load_all_fixtures

def setup
@post = (grab a post)
end

context "The Public" do
setup { @post = (grab a post) } #necessary as it doesn't pull from
the setup block

should_be_restful do |resource|
#fun stuff
end
end
------

So basically I want to find my @post only once in the setup block, but
my tests explode if I don't set it up in the setup block within each
context.

After reading up some more it seems like it's because Rails 2.0
inherits from ActionController::TestCase instead of the older way. It
seems like a partial solution is to call super at the beginning of the
main setup, but that doesn't solve it for me. In the meantime I can
probably repeat the setup in each setup block, but it'd be nice to
just toss it in the main setup and be done with it. In the meantime,
if you have any ideas for a workaround or a possible patch, it would
be greatly appreciated.

James Golick

unread,
Dec 23, 2007, 9:36:20 PM12/23/07
to sho...@googlegroups.com
It's rails, not shoulda.

In 2.0.1, ActionController(or ActionMailer)::TestCase had a problem
where inherited classes would break the setup method. The remedy for
that problem is to call super in your custom setup method. I submitted
a patch for ActionMailer, and somebody else submitted one for
ActionController.

http://dev.rubyonrails.org/ticket/10406
http://dev.rubyonrails.org/ticket/10382

In 2.0.2, overriding setup in a TestCase inherited from one of those
classes doesn't have any effect at all. Without shoulda's contexts +
setup blocks, you can't define any setup for your controller/mailer
tests.

http://dev.rubyonrails.org/changeset/8392

How they released code that breaks the _setup_ method in TestCases is
baffling. They seem to be ignoring our patches that would solve the
problem, for whatever reason. Go +1 them if this is bugging you (it's
driving me totally nuts as you may have guessed).

In the meantime, inherit from Test::Unit::TestCase, and test your
controller the normal way.

Zach

unread,
Dec 24, 2007, 12:01:18 AM12/24/07
to shoulda
Yeah, I've been following those tickets too, so I was on the right
track at least. I'll follow along and see if it gets updated here in
the future. In the meantime, I've gone with what you've suggested and
inherited from Test::Unit::TestCase and it appears to be working as
intended... thanks!

Zach

On Dec 23, 8:36 pm, "James Golick" <jamesgol...@gmail.com> wrote:
> It's rails, not shoulda.
>
> In 2.0.1, ActionController(or ActionMailer)::TestCase had a problem
> where inherited classes would break the setup method. The remedy for
> that problem is to call super in your custom setup method. I submitted
> a patch for ActionMailer, and somebody else submitted one for
> ActionController.
>
> http://dev.rubyonrails.org/ticket/10406http://dev.rubyonrails.org/ticket/10382
>
> In 2.0.2, overriding setup in a TestCase inherited from one of those
> classes doesn't have any effect at all. Without shoulda's contexts +
> setup blocks, you can't define any setup for your controller/mailer
> tests.
>
> http://dev.rubyonrails.org/changeset/8392
>
> How they released code that breaks the _setup_ method in TestCases is
> baffling. They seem to be ignoring our patches that would solve the
> problem, for whatever reason. Go +1 them if this is bugging you (it's
> driving me totally nuts as you may have guessed).
>
> In the meantime, inherit from Test::Unit::TestCase, and test your
> controller the normal way.
>

neti...@gmail.com

unread,
Jan 13, 2008, 12:08:32 PM1/13/08
to shoulda
I've run across this problem in rails 2.0.2 I checked out the tickets
but there is not a lot there to say whether this was fixed or not.

Do you guys know if it was fixed? I can't believe they would release
with this kind of mistake.

On Dec 24 2007, 12:01 am, Zach <google....@zachholman.com> wrote:
> Yeah, I've been following those tickets too, so I was on the right
> track at least. I'll follow along and see if it gets updated here in
> the future. In the meantime, I've gone with what you've suggested and
> inherited from Test::Unit::TestCase and it appears to be working as
> intended... thanks!
>
> Zach
>
> On Dec 23, 8:36 pm, "James Golick" <jamesgol...@gmail.com> wrote:
>
> > It's rails, not shoulda.
>
> > In 2.0.1, ActionController(or ActionMailer)::TestCase had a problem
> > where inherited classes would break the setup method. The remedy for
> > that problem is to call super in your custom setup method. I submitted
> > a patch for ActionMailer, and somebody else submitted one for
> > ActionController.
>
> >http://dev.rubyonrails.org/ticket/10406http://dev.rubyonrails.org/tic...

James Golick

unread,
Jan 13, 2008, 12:10:01 PM1/13/08
to sho...@googlegroups.com
It has been fixed in edge rails. But, not in 2.0.2.

The solution is to either freeze to edge, or inherit from Test::Unit::TestCase, and stub the controllers yourself.

On Jan 13, 2008 12:08 PM, neti...@gmail.com <neti...@gmail.com> wrote:

I've run across this problem in rails 2.0.2  I checked out the tickets
but there is not a lot there to say whether this was fixed or not.

Do you guys know if it was fixed?  I can't believe they would release
with this kind of mistake.

On Dec 24 2007, 12:01am, Zach <google....@zachholman.com> wrote:
> Yeah, I've been following those tickets too, so I was on the right
> track at least. I'll follow along and see if it gets updated here in
> the future. In the meantime, I've gone with what you've suggested and
> inherited from Test::Unit::TestCase and it appears to be working as
> intended... thanks!
>
> Zach
>

neti...@gmail.com

unread,
Jan 13, 2008, 12:24:08 PM1/13/08
to shoulda
Thanks for the quick reply.

I went with your suggestion to inherit from Test::Unit::TestCase

Thanks again.

On Jan 13, 12:10 pm, "James Golick" <jamesgol...@gmail.com> wrote:
> It has been fixed in edge rails. But, not in 2.0.2.
>
> The solution is to either freeze to edge, or inherit from
> Test::Unit::TestCase, and stub the controllers yourself.
>
Reply all
Reply to author
Forward
0 new messages