{"id":176,"date":"2010-05-14T12:03:51","date_gmt":"2010-05-14T12:03:51","guid":{"rendered":"http:\/\/nicholshayes.myzen.co.uk\/blog\/?p=176"},"modified":"2010-05-27T15:37:44","modified_gmt":"2010-05-27T15:37:44","slug":"unit-testing-attachment_fu","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=176","title":{"rendered":"Unit testing attachment_fu"},"content":{"rendered":"<p>I&#8217;m working on a portion of my latest application that will need a facility to upload files. As these files are not images, I decided to use the plug-in <a href=\"http:\/\/wiki.github.com\/technoweenie\/attachment_fu\/\">attachment_fu<\/a>. The thing that has caused me the most head scratching was how to set up unit tests to work with attachment_fu.<\/p>\n<p>The class stores files related to a product. These are either code exerts or documentation. I&#8217;ve called the class ProductFile.<\/p>\n<h3>Params<\/h3>\n<p>If you are going to run tests that pass in data via a params hash, how do you get a file into the params in the right format. Here is the solution I worked out:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">test_file_path = <span class=\"re2\">ActionController::TestCase<\/span>.<span class=\"me1\">fixture_path<\/span> <span class=\"sy0\">+<\/span> <span class=\"st0\">'\/files\/test.txt'<\/span><br \/>\ntest_file = <span class=\"re2\">ActionController::TestUploadedFile<\/span>.<span class=\"me1\">new<\/span><span class=\"br0\">&#40;<\/span>test_file_path<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"re1\">@product_file_params<\/span> = <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; product_id <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">1<\/span>,<br \/>\n&nbsp; <span class=\"re3\">:document_type<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">'document'<\/span>,<br \/>\n&nbsp; <span class=\"re3\">:version<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">2<\/span>,<br \/>\n&nbsp; <span class=\"re3\">:uploaded_data<\/span> <span class=\"sy0\">=&gt;<\/span> test_file<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>I couldn&#8217;t get this to work without having the test file in a sub-folder of the fixtures folder. So I added a file &#8216;test\/fixtures\/files\/test.txt&#8217;. Then I used ActionController::TestCase.fixture_path to determine the path.<\/p>\n<h3>Tear down<\/h3>\n<p>The other problem I had was that the standard Rails unit test tear down process wasn&#8217;t removing files created during the test. I overcame this by keeping track of each ProductFile created during the tests, and then using a teardown method to remove them all at the end of the test. This is the code that achieved that:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">@@created_product_files = <span class=\"kw3\">Array<\/span>.<span class=\"me1\">new<\/span><br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> create_product_file_from_params<span class=\"br0\">&#40;<\/span>params_mod = <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; product_file = ProductFile.<span class=\"me1\">create<\/span><span class=\"br0\">&#40;<\/span>@product_file_params.<span class=\"me1\">merge<\/span><span class=\"br0\">&#40;<\/span>params_mod<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; @@created_product_files <span class=\"sy0\">&lt;&lt;<\/span> product_file<br \/>\n&nbsp; <span class=\"kw2\">return<\/span> product_file<br \/>\n<span class=\"kw1\">end<\/span><br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> teardown<br \/>\n&nbsp; remove_product_files_created_during_test<br \/>\n<span class=\"kw1\">end<\/span><br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> remove_product_files_created_during_test<br \/>\n&nbsp; <span class=\"kw1\">for<\/span> product_file <span class=\"kw1\">in<\/span> @@created_product_files<br \/>\n&nbsp; &nbsp; product_file.<span class=\"me1\">destroy<\/span> <span class=\"kw1\">if<\/span> product_file.<span class=\"me1\">filename<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Now, as long as I used the create_product_file_from_params to create new product_files with params, the test would clean up after itself.<\/p>\n<h3>Fixtures<\/h3>\n<p>By the way, Product Files created via fixtures didn&#8217;t create a matching file in the file system. Which make it easier to keep things tidy, but needs to be kept in mind if you need an actual file to test against.<\/p>\n<h3>References<\/h3>\n<p><a href=\"http:\/\/wiki.github.com\/technoweenie\/attachment_fu\/\">attachment_fu wiki<\/a><\/p>\n<p><a href=\"http:\/\/clarkware.com\/cgi\/blosxom\/2007\/02\/24#FileUploadFu&#038;gt\">Mike Clark&#8217;s example of using attachment_fu<\/a><\/p>\n<p><a href=\"http:\/\/unixmonkey.net\/?p=18\">David Jones&#8217; very interesting article on using attachment_fu with acts_as_versioned<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on a portion of my latest application that will need a facility to upload files. As these files are not images, I decided to use the plug-in attachment_fu. The thing that has caused me the most head scratching &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=176\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/176"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=176"}],"version-history":[{"count":24,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":197,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/176\/revisions\/197"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}