Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5970 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Expand test coverage on RailTies

Reported by: kevinclark Assigned to: bitsweat
Priority: normal Milestone:
Component: Railties Version: edge
Severity: normal Keywords: test
Cc: nextangle

Description

A few big changes with this patch. I'm using mocha/stubba for testing because it makes working with this much easier. It also isolates our tests which is nice. I've added a test helper with a couple methods:

delegate_methods_to_mock! and delegate_method_to_mock! take a class and a method (or several methods) as symbols and then redefines them to call a mock instead. It then runs the passed block (which would result in the mock being manipulated) and cleans up. This means the mock is only used in the context of that block which is nifty.

As for using Mocha/Stubba, Flexmock could be used in place of Mocha but the functionality I'm using in Stubba doesn't exist elsewhere. Mocha (which contains Stubba) is under the Ruby license if it makes a difference.

Attachments

use_mocks_on_fcgi_handler_tests.diff (3.8 kB) - added by Kevin Clark on 08/30/06 06:39:10.
This brings code coverage on fgci_handler.rb up to 78%
use_mocks_on_fcgi_handler_tests.v2.diff (10.2 kB) - added by kevinclark on 08/30/06 09:59:46.
This one brings coverage up to 91%
use_mocks_on_fcgi_handler_tests.v3.diff (11.5 kB) - added by kevinclark on 08/30/06 10:52:57.
Uses Flexmock and cleans up the fcgi_handler a bit
use_mocks_on_fcgi_handler_tests.v4.diff (7.6 kB) - added by kevinclark on 09/02/06 03:49:27.
This patch moves back to using Mocha because it drastically reduces the complexity of the tests. This makes things clearer and easier to modify in the future. At this point I'm convinced Mocha has significant merits over Flexmock. See http://glu.ttono.us/articles/2006/09/01/the-flexibility-of-mocha

Change History

08/30/06 06:39:10 changed by Kevin Clark

  • attachment use_mocks_on_fcgi_handler_tests.diff added.

This brings code coverage on fgci_handler.rb up to 78%

08/30/06 07:16:31 changed by david

Good stuff, Kevin. Do you think you could make it do with flexmock, though? We already include that in Rails. And it seems to have the same capabilities.

08/30/06 07:40:12 changed by kevinclark

David, I'd be happy to use flexmock in place of Mocha. The reason I didn't use it in the initial patch is because I'm using Stubba (which is bundled with Mocha) and Flexmock doesn't have equivalent functionality there. A dev would still need to have mocha install to get these tests running. If you've got an alternative stubbing lib that you like better I could look into that.

08/30/06 09:59:46 changed by kevinclark

  • attachment use_mocks_on_fcgi_handler_tests.v2.diff added.

This one brings coverage up to 91%

08/30/06 10:02:42 changed by kevinclark

  • cc set to nextangle.

The latest patch covers most of the fcgi_handler.rb and cleans up some of the other tests. Of the two methods that aren't covered, I think reload! is broken and breakpoint! is hard for me to test.

reload! calls Dispatcher.reset_application! which doesn't appear to exist. If you let that method run you get errors.

breakpoint! calls methods on a Module which I haven't figured out how to mock yet. Gimie a little bit on that one.

Still using Mocha since I haven't heard from David.

08/30/06 10:13:44 changed by ulysses

  • type changed from defect to enhancement.

Great work kevin.

DHH says use flexmock. ;-)

08/30/06 10:52:57 changed by kevinclark

  • attachment use_mocks_on_fcgi_handler_tests.v3.diff added.

Uses Flexmock and cleans up the fcgi_handler a bit

08/31/06 01:53:03 changed by kevinclark

James Mead of Mocha/Stubba has agreed to release it under MIT. He'll update the RDoc later tonight. Maybe we can just include Stubba?

08/31/06 03:20:42 changed by david

I'm not overly excited about including even more libraries in core. Perhaps we'll just have to require mocha/stubba to run the railties tests. I'm not thrilled about that either, but the lesser of two evils.

I will say, though, that these are all squabbles in the face of you donating your time to increase test coverage. That's the most important thing. So ultimately, he who does the work gets the most say in how its being done.

So for starters, perhaps you just continue to use mocha/stubba and have a nice error message prompting people who to install those if they try to run the railties tests.

In any case, I think its a great move to see even more software under MIT :)

08/31/06 03:23:42 changed by kevinclark

I'll add in a check for Stubba and file a new patch. Eventually it'd be nice to be able to mock out some of the things in the other libraries (AR and AP come to mind). I'll use flexmock for those and see if I can't find another way to include the stubbing functionality that Stubba provides.

08/31/06 03:35:19 changed by david

Sounds good. Could you give a simple example of what you're using from Stubba that flexmock doesn't let you do? Perhaps we can get that into flexmock, if it feels like a good fit.

08/31/06 04:06:06 changed by kevinclark

If you take a look at http://pastie.caboo.se/10938

I use line 8 in the test method to ensure that the when_ready method is going to return the right thing when I run the code. It means that I know it takes the right branch. I could do this with monkey-patching, but I'd have to roll it back after the test was done (much like delegate_methods_to_mock! is doing). I can also make methods raise with Stubba which is nice for testing error handling.

09/02/06 03:49:27 changed by kevinclark

  • attachment use_mocks_on_fcgi_handler_tests.v4.diff added.

This patch moves back to using Mocha because it drastically reduces the complexity of the tests. This makes things clearer and easier to modify in the future. At this point I'm convinced Mocha has significant merits over Flexmock. See http://glu.ttono.us/articles/2006/09/01/the-flexibility-of-mocha

09/02/06 03:50:50 changed by kevinclark

Oh, patch v4 also brings coverage up to 98%.

09/03/06 00:18:32 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [4913]) Thoroughly test the FCGI dispatcher. Closes #5970.