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

标签 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 的问题表明我没有测试生产 block ,因为我在我的 spec_helper 文件中将我的环境设置为“测试”。

有没有办法用 rspec 测试“测试”和“生产”代码?

最佳答案

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

关于ruby - 使用 rspec 测试生产特定代码 - Sinatra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601741/

相关文章:

api - Paypal Sandbox- 进入现场生产环境

python - Django 1.8.3 运行测试在创建数据库时失败

ruby - 如何从 Twitter gem 中获取更多记录?

ruby - 从 ActiveRecord 迁移中输出 SQL 而不执行它(不是 rails!)

ruby - Ruby 中的读/写进程内存

testing - 使用 Selenium 集成开发环境

ruby-on-rails - 使用 RSPec 模拟/ stub 系统调用

ruby-on-rails - 使用RSpec测试Rails辅助方法时,如何在params哈希中设置值?

ruby-on-rails - Rspec: ||= 运算符重复计算代码

ruby-on-rails - Ruby rufus 调度程序 gem