Test a method which starts a Rack App

Hey folks!

Just added some tests for my first gem
(https://github.com/Benny1992/rack-blogengine).

Played a little bit with TestUnit, MiniTest Unit/Spec, RSpec and
Cucumber/Capybara

For Unit testing i decided for MiniTest::Unit, because it’s in the
stdlib
For Feature testing (only 1 test feature yet) i’ll use Cucumber/Capybara

The question for my unit tests:

In the lib folder i got a cli class which handles all cli interaction
(run, generate, etc). When i try to test my run method in
command_line_interface_test.rb i had to add a “environment” parameter to
the lib file, so it doesn’t start the specified Rack::Server in the
Test.

I think this isn’t very clean to edit a Core file so tests can hook into
it
Is there a better way to achieve this?

In general:
If you find something which can be done better, just say it (it’s my
first real Ruby Application) :slight_smile:

Am 19.02.2014 23:23, schrieb Benjamin Klotz:

In the lib folder i got a cli class which handles all cli interaction
(run, generate, etc). When i try to test my run method in
command_line_interface_test.rb i had to add a “environment” parameter to
the lib file, so it doesn’t start the specified Rack::Server in the
Test.

I think this isn’t very clean to edit a Core file so tests can hook into
it
Is there a better way to achieve this?

Some vague thoughts (I had no time to look into the source):

I assume the purpose of the `run’ method is to start the server,
so what do you want to test if not that a server is started?
Maybe your run method does more than it should (like setup).

  • you could move those parts to a different method
    and only test the helper method

  • or use a mock for Rack::Server that doesn’t really start the server
    (MiniTest::Mock)

  • or you might want to try monkey patching Rack::Server
    in your test file

Regards,
Marcus

unknown wrote in post #1137302:

Am 19.02.2014 23:23, schrieb Benjamin Klotz:

In the lib folder i got a cli class which handles all cli interaction
(run, generate, etc). When i try to test my run method in
command_line_interface_test.rb i had to add a “environment” parameter to
the lib file, so it doesn’t start the specified Rack::Server in the
Test.

I think this isn’t very clean to edit a Core file so tests can hook into
it
Is there a better way to achieve this?

Some vague thoughts (I had no time to look into the source):

I assume the purpose of the `run’ method is to start the server,
so what do you want to test if not that a server is started?
Maybe your run method does more than it should (like setup).

  • you could move those parts to a different method
    and only test the helper method

  • or use a mock for Rack::Server that doesn’t really start the server
    (MiniTest::Mock)

  • or you might want to try monkey patching Rack::Server
    in your test file

Regards,
Marcus

Thanks for your response! :slight_smile:
I think I’m going to look in Mock objects and as u said move some part
into helper methods.

Greetings,
Benny

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