Forum: Ruby Test a method which starts a Rack App

9fa32e40121025416cb389b0f5b69cbb?d=identicon&s=25 Benjamin Klotz (benny1992)
on 2014-02-19 23:23
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) :)
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2014-02-20 07:21
(Received via mailing list)
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
9fa32e40121025416cb389b0f5b69cbb?d=identicon&s=25 Benjamin Klotz (benny1992)
on 2014-02-20 07:31
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! :)
I think I'm going to look in Mock objects and as u said move some part
into helper methods.

Greetings,
Benny
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.