我可以覆盖test_helper.rb中的task:environment来测试rake任务吗?

时间:2021-01-11 22:28:06

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:

我在Rakefile中有一系列rake任务,我想测试它作为我的规范的一部分等。每个任务都以下面的形式定义:

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.

其中:environment任务设置ActiveRecord / DataMapper数据库连接和类。我没有将它作为Rails的一部分使用,但我有一系列测试,我喜欢将它作为BDD的一部分运行。

This snippet illustrates how I'm trying to test the rake tasks.

这个片段说明了我是如何尝试测试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

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.

所以我的帮助请求 - 是否有可能在我的test_helper.rb文件中覆盖:environment任务,所以我的rake测试与我的测试数据库交互,而不是生产?我已经尝试在帮助文件中重新定义任务,但这不起作用。

Any help for a solution would be great, as I've been stuck on this for the past week.

对解决方案的任何帮助都会很棒,因为过去一周我一直坚持这一点。

3 个解决方案

#1


9  

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.

我对类似问题的“解决方案”是从我的.rake文件中提取所有逻辑并创建类来执行任务,只在rake文件中留下一行调用,我对没有测试太过努力感到自信。然后可以非常正常地测试这些类。

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.

编辑:这里曾经有一篇博客文章,(a)说同样的事情,(b)说它更好。看起来他也是第一个说的。

#2


1  

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

我想你正在寻找这一行:require(File.join(RAILS_ROOT,'config','environment'))它正是你在“task:environment”实现中找到的

I use it to test my rake tasks using rspec

我用它来测试我的rake任务使用rspec

#3


0  

when you are running tests environment is that is being loaded is test.

当你运行测试环境时,正在加载的是测试。

so you are interacting with test database only.

所以你只与测试数据库进行交互。

So i dont see any reason to override your rake task in test_helper.rb

所以我没有看到任何理由在test_helper.rb中覆盖你的rake任务

#1


9  

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.

我对类似问题的“解决方案”是从我的.rake文件中提取所有逻辑并创建类来执行任务,只在rake文件中留下一行调用,我对没有测试太过努力感到自信。然后可以非常正常地测试这些类。

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.

编辑:这里曾经有一篇博客文章,(a)说同样的事情,(b)说它更好。看起来他也是第一个说的。

#2


1  

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

我想你正在寻找这一行:require(File.join(RAILS_ROOT,'config','environment'))它正是你在“task:environment”实现中找到的

I use it to test my rake tasks using rspec

我用它来测试我的rake任务使用rspec

#3


0  

when you are running tests environment is that is being loaded is test.

当你运行测试环境时,正在加载的是测试。

so you are interacting with test database only.

所以你只与测试数据库进行交互。

So i dont see any reason to override your rake task in test_helper.rb

所以我没有看到任何理由在test_helper.rb中覆盖你的rake任务