Forum: Ruby on Rails [ANN] testcase_setup_and_teardown_with_blocks (aka "The plug

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-11 05:40
(Received via mailing list)
Hey all,

I got annoyed recently that I couldn't specifiy multiple chunks of
test setup and teardown code that get executed for each test. You're
currently limited to just one setup method (unless I'm much mistaken).
Anyway, I just wrapped up my solution in a plugin. The answer: use
blocks!

  class Test::Unit::TestCase
    # This is overridden by the subclass and will not be called
    def setup
    end

    # This is overridden by the subclass and will not be called
    def teardown
    end
  end

  class PersonTest < Test::Unit::TestCase
    # This overrides the method in the superclass
    def setup
    end

    # This overrides the method in the superclass
    def teardown
    end

    # Only the setup and teardown methods in this class will be called
    def test_name
    end
  end

Becomes ...

  class Test::Unit::TestCase
    setup do
    end

    teardown do
    end
  end

  class PersonTest < Test::Unit::TestCase
    setup do
    end

    teardown do
    end

    def test_name
    end
  end

It plays nicely with fixtures and should be completely backwards
compatible with existing code that uses setup and teardown methods.

Install with script/plugin install
http://svn.viney.net.nz/things/rails/plugins/testc...

If anyone can come up with a better name for the plugin please do
suggest it!

-Jonathan.
D61a2db96e73acf66b0b18688a39ecfb?d=identicon&s=25 Obie Fernandez (Guest)
on 2006-06-11 05:55
(Received via mailing list)
It might be helpful if you explain why you felt the pain that led to
creation of this plugin.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-11 10:38
(Received via mailing list)
SImple :). I wanted to define setup/teardown code in test_helper.rb as
well as the test files themselves. I also have another plugin that
will want to add bits of setup/teardown code and this makes it very
easy.

In my opinion this is actually a weekness in Test::Unit. Rails already
hacks around it to make fixtures work by aliasing setup/teardown
methods when you define your own method. This just takes it one step
further.

Has anyone else come up against this problem? How did you solve it?

-Jonathan.
30ee518e6fdc5b07e060775b5a542bdb?d=identicon&s=25 Jón Borgþórsson (jongretar)
on 2006-06-11 16:01
(Received via mailing list)
On 6/11/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:
> SImple :). I wanted to define setup/teardown code in test_helper.rb as
> well as the test files themselves. I also have another plugin that
> will want to add bits of setup/teardown code and this makes it very
> easy.
>
> In my opinion this is actually a weekness in Test::Unit. Rails already
> hacks around it to make fixtures work by aliasing setup/teardown
> methods when you define your own method. This just takes it one step
> further.
>

Have you tested rSpec instead and seen if you are happier?
I started using it a week ago. Felt more natural than the Test
framework.

--
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-12 00:28
(Received via mailing list)
Thanks for that, I hadn't seen rSpec. It looks very interesting.

-Jonathan.
30ee518e6fdc5b07e060775b5a542bdb?d=identicon&s=25 Jón Borgþórsson (jongretar)
on 2006-06-12 00:47
(Received via mailing list)
A good introduction video.
http://video.google.com/videoplay?docid=8135690990081075324

On 6/11/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:
> > >
> > --------------
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2006-06-12 06:34
(Received via mailing list)
I'll try to fork the thread instead of hijacking it :)

With rSpec, is there anything analogous to autotest? I like the
positive-assert way BDD poses tests but the two things that are (to me)
stumbling blocks are:

- no autotest that I know of :(
- no clear migration path for existing projects

rSpec seems faster that Unit::Test. Could that be true?

Thanks
--
View this message in context:
http://www.nabble.com/-ANN--testcase_setup_and_tea...
Sent from the RubyOnRails Users forum at Nabble.com.
6a28ba6ec44cf691eb4f4d7d40e094e5?d=identicon&s=25 Craig Demyanovich (Guest)
on 2006-06-12 06:49
(Received via mailing list)
On Jun 12, 2006, at 12:31 AM, s.ross wrote:
>
> With rSpec, is there anything analogous to autotest? I like the
> positive-assert way BDD poses tests but the two things that are (to
> me)
> stumbling blocks are:
>
> - no autotest that I know of :(
> - no clear migration path for existing projects

Regarding migration from Test::Unit, see test2spec [http://
rspec.rubyforge.org/tools/test2spec.html]. I haven't used it; I just
know it's there.

Craig
This topic is locked and can not be replied to.