首页 > 解决方案 > 无法使用 docker compose 执行 rake rspec 单元测试

问题描述

当我rake spec对这个 rake 文件执行时,docker compose 会出现,然后在执行单元测试之前它会关闭。如果我删除最后一个运行的命令docker-compose down它工作正常

如何修改脚本以便它可以运行docker-compose up --detach,然后run the unit tests,然后docker-compose down在单元测试完成后运行?

我尝试添加睡眠,但效果不佳。

在我的Rakefile:

Spec::Core::RakeTask.new(:spec)

# start docker compose.
# we use the system command to wait until docker compose is up and running before starting unit tests
system("docker-compose up --detach")

# run unit tests
task :default => :spec

# stop docker compose
system("docker-compose down")

标签: rubydockerdocker-composerakerakefile

解决方案


我认为你的问题不是关于,docker而是关于rake. Rake(如 make)允许您将依赖项添加到任务中,我们可以使用依赖项链接来安排任务的顺序:

task :docker_up do
 system("docker-compose up --detach")
end

task :docker_down do
 system("docker-compose down")
end

task :default => [:docker_up, :spec, :docker_down]

这是您可以了解如何使用 rake的主题


推荐阅读