使用rspec测试生产特定代码 - Sinatra

时间:2017-01-11 22:18:56

标签: ruby testing rspec sinatra

我有这段代码来配置生产和测试/开发环境的日志记录:

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的问题是说我没有测试生产块,因为我在spec_helper文件中将我的环境设置为'test'。

有没有办法用rspec测试'test'和'production'代码?

1 个答案:

答案 0 :(得分:0)

您可以将configure块体中的代码分解为一个或多个将环境值作为参数的方法,然后为这些方法编写单独的测试。但是,如果唯一的目标是获得100%的覆盖率,我建议不要使代码复杂化。