私はジャスミンのために同じようなことをしました。いくつかのHAMLテンプレートをHTMLにコンパイルし、それらをJasmineのフィクスチャパスに配置するRakeタスクを作成しました。次に、Rakeタスクを依存関係として設定し、jasmine:ci
タスクの前に実行されるようにしました。これが私が書いたRakeタスクです:
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
これにより、HAMLまたはERBテンプレートをで記述できspec/javascript/templates
、それらはにコンパイルされspec/javascript/fixtures
、Jasmineによってロードできます。この行view_paths = ActionController::Base.view_paths
により、アプリケーションのパーシャルがのテンプレートで使用できるようになりますspec/javascript/templates
。ヘルパーも利用できるようにするには、おそらくこの例を微調整する必要があります。最後に、これはRails2.3アプリケーションからのものであることを述べておきます。Rails3.xではまだ試していません。これがお役に立てば幸いです。