Nicholas Wieland
2011-Apr-13 01:48 UTC
[rspec-users] Need suggestions on how to test a class
Hi *, the class I need to test is: https://gist.github.com/49ef28014bb648ffa63e I know my question is very generic, but I honestly fail to see how to test something that is so strictly procedural and so dependent to the platform/libraries. Should I mock/stub filesystem, services and pretty much everything that is in there and test how it responds to different inputs, or adopt an "integration style" ? How would you proceed in this case ? (you can refer to RSpec book and point to parts that are relevant). ngw -- [ 926381, 23200231779, 1299022, 1045307475 ].collect { |a| a.to_s( 36 ) }.join( " " ) Nicholas Wieland (ngw) ngw at nofeed.org http://www.nofeed.org
On 13 Apr 2011, at 02:48, Nicholas Wieland wrote:> Hi *, the class I need to test is: > > https://gist.github.com/49ef28014bb648ffa63e > > I know my question is very generic, but I honestly fail to see how to test something that is so strictly procedural and so dependent to the platform/libraries. > Should I mock/stub filesystem, services and pretty much everything that is in there and test how it responds to different inputs, or adopt an "integration style" ? > How would you proceed in this case ? (you can refer to RSpec book and point to parts that are relevant). > > ngwIt depends on what you''re worried about breaking. It looks to me like the unpack method is the most likely one to go wrong, and the most likely one to grow with more cases, so I''d focus my effort on that. You have about three distinct responsibilities going on in this class - there''s something which sets up a directory structure, something which fetches zip files from S3, and something which unpacks a zip file into a folder structure. If you encapsulated what''s going on in #sandbox into a Target or Destination or Sandbox class, with methods like write_stylesheet and and write_image, you could mock out that object easily. Similarly if you create a wrapper object around the responsibility of calling S3, you''d be able to test your unpack method with some fixture zip files from your test suite. The Sandbox and S3Fetcher classes will be pretty simple and unlikely to break, so you might not need to test them at all, other than with a quick manual integration test. Depending on how mission critical this code is, you might want a simple integration test to check it all wires up, but that depends on you appetite for risk, how often this code will change, and where that change happens. If all you do is add more cases to the unpack method, you''re going to be safe testing those changes with specs and running a quick manual test.> > -- > [ 926381, 23200231779, 1299022, 1045307475 ].collect { |a| a.to_s( 36 ) }.join( " " ) > Nicholas Wieland (ngw) > ngw at nofeed.org > http://www.nofeed.org > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, Matt -- Freelance programmer & coach Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne
Hi Nicholas, You have little choice but to mock external dependencies like S3 and the file system in order to be able to do TDD. The problem is that, as Matt said, if it''s a mission critical feature you''ll want to create infrastructure to run tests against a real environment (even if it''s just a smoke suite that''s separate from the unit tests). Take a look at https://github.com/thoughtworks/cruisecontrol.rb/blob/master/test/file_sandbox.rb which deals with this kind of issue in CC.rb for a (what I consider) a rather nice way of dealing with this. Best, Sidu. http://c42.in http://about.me/ponnappa On 13 April 2011 07:18, Nicholas Wieland <ngw at nofeed.org> wrote:> Hi *, the class I need to test is: > > https://gist.github.com/49ef28014bb648ffa63e > > I know my question is very generic, but I honestly fail to see how to test something that is so strictly procedural and so dependent to the platform/libraries. > Should I mock/stub filesystem, services and pretty much everything that is in there and test how it responds to different inputs, or adopt an "integration style" ? > How would you proceed in this case ? (you can refer to RSpec book and point to parts that are relevant). > > ?ngw > > -- > [ 926381, 23200231779, 1299022, 1045307475 ].collect { |a| a.to_s( 36 ) }.join( " " ) > Nicholas Wieland (ngw) > ngw at nofeed.org > http://www.nofeed.org > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >