开发者

How to generate fixtures for use in Jasmine tests

开发者 https://www.devze.com 2023-04-04 22:42 出处:网络
I\'m writing a Rails plugin/gem, that is basically some helpers (FormHelpers, FormTagHelpers), and some asso开发者_运维百科ciated Javascript to add behavior.

I'm writing a Rails plugin/gem, that is basically some helpers (FormHelpers, FormTagHelpers), and some asso开发者_运维百科ciated Javascript to add behavior.

I'm testing the tag helpers with RSpec. And now I am setting up Jasmine to test the Javascript behaviors.

I would like to generate the Jasmine fixtures out of the tag helper code in my plugin, instead of using static (brittle) fixtures. So when the tag code changes, the fixtures for the Jasmine tests will automatically update.

My first thought was to extend RSpec, with something like the shared behavior of "it_should_behave_like", only it would be "it_should_save_fixture_on_success". And it would be something like an after(:all) block.

Two things I would need to know... how to get at the "title" of the context (which would be the default name of the fixture", and how to determine, in the after(:all), if all the specs ran successfully.

Does that sound reasonable? Or have I missed something obvious?


I did something similar for Jasmine. I wrote a Rake task which compiles some HAML templates into HTML and puts them in Jasmine's fixtures path. Then, I set up the Rake task to be a dependency so that it would run before the jasmine:ci task. Here is the Rake task that I wrote:

namespace :dom do                                                                                                                                           
  namespace :fixtures do
    target_path   = "spec/javascripts/fixtures"
    template_path = "spec/javascripts/templates"

    task :compile do
      view_paths  = ActionController::Base.view_paths
      view_paths  << template_path

      view = ActionView::Base.new(view_paths, {}) 

      Dir.glob File.join(template_path, '*') do |path|
        template  = File.basename(path)
        template  = template.slice(0...template.index('.')) if template.index('.')
        target    = File.join(target_path, template) + ".html"

        puts "Rendering fixture '#{template}' to #{target}"

        File.open(target, 'w') do |f| 
          f.write view.render(:file => template, :layout => false)
        end 
      end 
    end 

    task :clean do
      Dir.glob File.join(target_path, '*') do |path|
        File.delete path
      end 
    end 
  end 
end

namespace :spec do
  desc "Run specs in spec/javascripts"
  task :javascripts => ['dom:fixtures:compile', 'jasmine:ci']
end

This lets you write HAML or ERB templates in spec/javascript/templates and they get compiled to spec/javascript/fixtures, which can then be loaded by Jasmine. The line view_paths = ActionController::Base.view_paths makes your application's partials available to the templates in spec/javascript/templates. You'll probably need to tweak this example to make your helpers available as well. Lastly, I should mention that this is from a Rails 2.3 application. I haven't tried it in Rails 3.x yet. I hope this helps.


On the off chance that someone stumbles across this, here is a newer, better answer.

https://github.com/crismali/magic_lamp

Magic Lamp will allow you to render views/partials as fixtures to use in your Javascript tests. Which is pretty much what I was looking for, anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号