gpt4 book ai didi

javascript - 如何生成用于 Jasmine 测试的 fixture

转载 作者:行者123 更新时间:2023-11-29 18:31:50 25 4
gpt4 key购买 nike

我正在编写一个 Rails 插件/gem,它基本上是一些帮助程序(FormHelpers、FormTagHelpers)和一些关联的 Javascript 来添加行为。

我正在使用 RSpec 测试标签助手。现在我正在设置 Jasmine 来测试 Javascript 行为。

我想从我的插件中的标签助手代码生成 Jasmine 装置,而不是使用静态(脆弱的)装置。因此,当标记代码更改时,Jasmine 测试的 fixture 将自动更新。

我的第一个想法是扩展 RSpec,使用类似于“it_should_behave_like”的共享行为,只是它会是“it_should_save_fixture_on_success”。它会像一个 after(:all) block 。

我需要知道两件事...如何获得上下文的“标题”(这将是 fixture 的默认名称),以及如何在 after(:all) 中确定是否所有规范都成功运行。

这听起来合理吗?还是我错过了一些明显的东西?

最佳答案

我为 Jasmine 做了类似的事情。我写了一个 Rake 任务,它将一些 HAML 模板编译成 HTML,并将它们放在 Jasmine 的固定路径中。然后,我将 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

这让您可以在 spec/javascript/templates 中编写 HAML 或 ERB 模板,并将它们编译为 spec/javascript/fixtures,然后可以由 Jasmine 加载。 view_paths = ActionController::Base.view_paths 行使您的应用程序的部分可用于 spec/javascript/templates 中的模板。您可能需要调整此示例以使您的助手也可用。最后,我应该提到这是来自 Rails 2.3 应用程序。我还没有在 Rails 3.x 中尝试过。我希望这会有所帮助。

关于javascript - 如何生成用于 Jasmine 测试的 fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com