测试 rake 任务中定义的方法
我想测试 rake 任务中定义的方法。
rake 文件
#lib/tasks/simple_task.rake
namespace :xyz do
task :simple_task => :environment do
begin
if task_needs_to_run?
puts "Lets run this..."
#some code which I don't wish to test
...
end
end
end
def task_needs_to_run?
# code that needs testing
return 2 > 1
end
end
现在,我想在测试文件中测试此方法 task_needs_to_run?
我该怎么做?
附加说明:我理想地希望在 rake 任务中测试另一个私有方法......但我可以稍后再担心。
I want to test a method defined in a rake task.
rake file
#lib/tasks/simple_task.rake
namespace :xyz do
task :simple_task => :environment do
begin
if task_needs_to_run?
puts "Lets run this..."
#some code which I don't wish to test
...
end
end
end
def task_needs_to_run?
# code that needs testing
return 2 > 1
end
end
Now, I want to test this method, task_needs_to_run?
in a test file
How do I do this ?
Additional note: I would ideally want test another private method in the rake task as well... But I can worry about that later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常的方法是将所有实际代码移至模块中,并保留任务实现:
如果您使用环境变量或命令参数,只需将它们传入:
这样您就可以测试和重构实现,而无需触及根本没有耙子任务。
The usual way to do this is to move all actual code into a module and leave the task implementation to be only:
If you use environmental variables or command argument, just pass them in:
This way you can test and refactor the implementation without touching the rake task at all.
你可以这样做:
我自己尝试过......在 Rake 命名空间内定义一个方法与在顶层定义它相同。
加载
Rakefile 不会运行任何任务...它只是定义它们。因此,在测试脚本中加载
Rakefile 没有什么坏处,因此您可以测试关联的方法。You can just do this:
I tried this myself... defining a method inside a Rake namespace is the same as defining it at the top level.
load
ing a Rakefile doesn't run any of the tasks... it just defines them. So there is no harm inload
ing your Rakefile inside a test script, so you can test associated methods.在已定义 rake 上下文 (类似这样的内容) 的项目中工作时:
When working within a project with a rake context (something like this) already defined: