我有这段代码来为生产和测试/开发环境配置日志记录:
class ApplicationController < Sinatra::Base
configure :development, :test do
enable :logging
file = File.new("#{settings.root}/../../log/#{settings.environment}.log", 'a+')
file.sync = true
use Rack::CommonLogger, file
end
configure :production do
enable :logging
log_file = File.new("#{settings.root}/../../log/#{settings.environment}.log", 'a+')
STDOUT.reopen(log_file)
STDERR.reopen(log_file)
STDOUT.sync = true
STDERR.sync = true
end
end
simplecov 的问题表明我没有测试生产 block ,因为我在我的 spec_helper 文件中将我的环境设置为“测试”。
有没有办法用 rspec 测试“测试”和“生产”代码?
最佳答案
您可以将 configure
block 主体中的代码分解为一个或多个将环境值作为参数的方法,然后为这些方法编写单独的测试。但是,如果唯一的目标是达到 100% 的覆盖率,我建议不要使代码复杂化。
关于ruby - 使用 rspec 测试生产特定代码 - Sinatra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601741/