{"id":507,"date":"2015-01-08T11:01:11","date_gmt":"2015-01-08T11:01:11","guid":{"rendered":"http:\/\/nicholshayes.co.uk\/blog\/?p=507"},"modified":"2015-01-08T11:08:18","modified_gmt":"2015-01-08T11:08:18","slug":"using-jasmine-with-rails-4-1","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=507","title":{"rendered":"Using Jasmine with Rails 4.1"},"content":{"rendered":"<p>I had a few problem getting fixtures to work within a Jasmine environment. I was getting the error &#8220;ReferenceError: loadFixtures is not defined&#8221;<\/p>\n<p>The fixture was an html pages at spec\/javascripts\/fixtures\/form.html, and this was my initial test code:<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">describe<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Rollback prompt&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; beforeEach<span class=\"br0\">&#40;<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; loadFixtures<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;form.html&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; describe<span class=\"br0\">&#40;<\/span><span class=\"st0\">'check test environment'<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; it<span class=\"br0\">&#40;<\/span><span class=\"st0\">'should always pass'<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; expect<span class=\"br0\">&#40;<\/span><span class=\"kw2\">true<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">toBe<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">true<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>I&#8217;d copied this format from a rails 3 project where I&#8217;d used the jasmine gem successfully.<\/p>\n<p>To get this to work with Rails 4.1, I had to:<\/p>\n<h2>Use jasmine-rails gems<\/h2>\n<p>I added this to my Gemfile (replacing gem &#8216;jasmine&#8217;)<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">group <span class=\"re3\">:development<\/span>, <span class=\"re3\">:test<\/span> <span class=\"kw1\">do<\/span><br \/>\n&nbsp; <span class=\"co1\"># JavaScript test environment<\/span><br \/>\n&nbsp; gem <span class=\"st0\">'jasmine-rails'<\/span><br \/>\n&nbsp; gem <span class=\"st0\">'jasmine-jquery-rails'<\/span><br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>And added a spec_helper at spec\/javascripts\/helpers\/spec_helper.js:<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"co1\">\/\/= require jquery<\/span><br \/>\n<span class=\"co1\">\/\/= require jasmine-jquery<\/span><\/div><\/div>\n<h2>Mount fixtures separately<\/h2>\n<p>I mounted my fixtures via config\/initializers\/jasmine.rb:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\"><span class=\"co1\"># Map fixtures directory for Jasmine suite<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"kw1\">defined<\/span>?<span class=\"br0\">&#40;<\/span>JasmineRails<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; JasmineFixtureServer = <span class=\"kw3\">Proc<\/span>.<span class=\"me1\">new<\/span> <span class=\"kw1\">do<\/span> <span class=\"sy0\">|<\/span>env<span class=\"sy0\">|<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re2\">Rack::Directory<\/span>.<span class=\"me1\">new<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'spec\/javascripts\/fixtures'<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">call<\/span><span class=\"br0\">&#40;<\/span>env<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>And then updated my config\/routes.rb:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">&nbsp; <span class=\"kw1\">if<\/span> <span class=\"kw1\">defined<\/span>?<span class=\"br0\">&#40;<\/span>JasmineRails<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; mount <span class=\"re2\">JasmineRails::Engine<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">'\/specs'<\/span><br \/>\n&nbsp; &nbsp; mount JasmineFixtureServer <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">'\/spec\/javascripts\/fixtures'<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><\/div><\/div>\n<p>After that my test passed successfully and I was ready to start building by JavaScript functions.<\/p>\n<h2>Solution source<\/h2>\n<p>These pages were key to me finding this solution:<\/p>\n<ul>\n<li>\n    <a href=\"https:\/\/github.com\/searls\/jasmine-rails\/issues\/123\" target=\"_blank\">https:\/\/github.com\/searls\/jasmine-rails\/issues\/123<\/a>\n  <\/li>\n<li>\n    <a href=\"https:\/\/github.com\/travisjeffery\/jasmine-jquery-rails\/issues\/4#issuecomment-29684484\" target=\"_blank\">https:\/\/github.com\/travisjeffery\/jasmine-jquery-rails\/issues\/4#issuecomment-29684484<\/a>\n  <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I had a few problem getting fixtures to work within a Jasmine environment. I was getting the error &#8220;ReferenceError: loadFixtures is not defined&#8221; The fixture was an html pages at spec\/javascripts\/fixtures\/form.html, and this was my initial test code: describe&#40;&quot;Rollback prompt&quot;, &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=507\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/507"}],"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=507"}],"version-history":[{"count":12,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":519,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions\/519"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}