我可以覆盖 test_helper.rb 中的任务:环境来测试 rake 任务吗?
我在 Rakefile 中有一系列 rake 任务,我想将其作为规范等的一部分进行测试。每个任务都以以下形式定义:
task :do_somthing => :environment do
# Do something with the database here
end
其中 :environment 任务设置 ActiveRecord/DataMapper 数据库连接和类。 我没有将其用作 Rails 的一部分,但我有一系列测试,我喜欢将其作为 BDD 的一部分运行。
此片段说明了我如何尝试测试 rake 任务。
def setup
@rake = Rake::Application.new
Rake.application = @rake
load File.dirname(__FILE__) + '/../../tasks/do_something.rake'
end
should "import data" do
@rake["do_something"].invoke
assert something_in_the_database
end
所以我请求帮助 - 是否可以覆盖 test_helper.rb 文件中的 :environment 任务,以便我的 rake 测试与我的测试数据库而不是生产数据库交互? 我尝试在帮助程序文件中重新定义任务,但这不起作用。
任何解决方案的帮助都会很棒,因为过去一周我一直坚持这个问题。
I have a series of rake tasks in a Rakefile which I'd like to test as part of my specs etc. Each task is defined in the form:
task :do_somthing => :environment do
# Do something with the database here
end
Where the :environment task sets up an ActiveRecord/DataMapper database connection and classes. I'm not using this as part of Rails but I have a series of tests which I like to run as part of BDD.
This snippet illustrates how I'm trying to test the rake tasks.
def setup
@rake = Rake::Application.new
Rake.application = @rake
load File.dirname(__FILE__) + '/../../tasks/do_something.rake'
end
should "import data" do
@rake["do_something"].invoke
assert something_in_the_database
end
So my request for help - is it possible to over-ride the :environment task in my test_helper.rb file so I my rake testing interacts with the my test database, rather than production? I've tried redefining the task in the helper file, but this doesn't work.
Any help for a solution would be great, as I've been stuck on this for the past week.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对类似问题的“解决方案”是从 .rake 文件中提取所有逻辑并创建类来执行任务,只在 rake 文件中留下一行调用,我相信不会太难测试。 然后可以非常正常地测试这些类。
我不知道这能在多大程度上承受一组复杂的相互依赖的任务,这些任务维持一些影响深远的状态:可能不太好,但话又说回来,这很可能表明其他一些设计问题......
我'我很好奇我是否错过了更好的东西。
编辑:曾经有一篇博客文章这里< /a> (a) 说的是同样的事情,而 (b) 说得更好。 看来也是他先说的。
My "solution" to a similar problem was to extract all the logic from my .rake files and create classes to perform the tasks, leaving just a one-line call in the rake file, which I felt confident in not testing too hard. The classes could then be tested pretty much normally.
I don't know how well this would stand up to a complex set of interdependent tasks that maintain some far-reaching state: probably not well, but then again that would most likely be an indication of some other design problem...
I'm curious to see if I've missed something better.
EDIT: There used to be a blog post here that (a) says the same thing and (b) says it better. Looks like he said it first, too.
我想你正在寻找这一行:
require(File.join(RAILS_ROOT, '配置', '环境'))
这正是您在“任务:环境”实现中找到的内容
我用它来使用 rspec 测试我的 rake 任务
I think you are looking for this line:
require(File.join(RAILS_ROOT, 'config', 'environment'))
it's exactly what you find in "task :environment" implementation
I use it to test my rake tasks using rspec
当您运行测试时,正在加载的环境是测试。
所以我没有看到任何理由覆盖 test_helper.rb 中的 rake 任务
when you are running tests environment is that is being loaded is test.
So i dont see any reason to override your rake task in test_helper.rb