如何在测试中重新加载灯具

时间:2016-05-13 05:20:24

标签: ruby-on-rails ruby rake minitest fixtures

我试图通过将动态生成的Feed与已知输出进行比较来测试RSS Feed。为了使这项工作,我需要加载时间冻结的灯具。以下工作,但通过重新加载架构和灯具来重置数据库似乎有点过分。

# test/integration/feed_test.rb
require 'test_helper'
load 'Rakefile'

class FeedTest < ActionDispatch::IntegrationTest
  def setup
    # Normalize time in order to match fixture file
    travel_to Time.zone.parse('2015-03-01T12:00:00') do
      silence_stream(STDOUT) do
        # anything written to STDOUT here will be silenced
        Rake::Task['db:schema:load'].reenable
        Rake::Task['db:schema:load'].invoke
      end
      Rake::Task['db:fixtures:load'].reenable
      Rake::Task['db:fixtures:load'].invoke
    end
  end

  test 'feed matches fixture file' do
    get feed_path
    assert_equal contents('feed.atom'), response.body
  end
end

# test/test_helper.rb
module ActiveSupport
  class TestCase
    fixtures :all

    def contents(file_name)
      IO.read "test/fixtures/files/#{file_name}"
    end
  end
end

我无法弄清楚如何仅在travel_to块内重新加载项目过滤器。我也无法让travel_totest_helper.rb工作。

但是,这种方法的真正问题是重新加载Rakefile会导致来自secure_headers gem的以下虚假警告:

$ rake test
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:1: warning: already initialized constant INLINE_SCRIPT_REGEX
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:1: warning: previous definition of INLINE_SCRIPT_REGEX was here
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:2: warning: already initialized constant INLINE_STYLE_REGEX
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:2: warning: previous definition of INLINE_STYLE_REGEX was here
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:3: warning: already initialized constant INLINE_HASH_SCRIPT_HELPER_REGEX
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:3: warning: previous definition of INLINE_HASH_SCRIPT_HELPER_REGEX was here
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:4: warning: already initialized constant INLINE_HASH_STYLE_HELPER_REGEX
/Users/dan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/secure_headers-3.3.1/lib/tasks/tasks.rake:4: warning: previous definition of INLINE_HASH_STYLE_HELPER_REGEX was here
Run options: --seed 17868

# Running:

........................................................................................................

Finished in 17.397614s, 5.9778 runs/s, 191.3481 assertions/s.

104 runs, 3329 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for MiniTest to /Users/dan/Dropbox/Documents/dev/cii-best-practices-badge/coverage. 754 / 781 LOC (96.54%) covered.

似乎我应该能够在不使用rake的情况下重新加载一些灯具,但我无法弄清楚如何这样做。

1 个答案:

答案 0 :(得分:0)

以下是解决我的问题的修复方法:

# frozen_string_literal: true
require 'test_helper'
load 'Rakefile'

class FeedTest < ActionDispatch::IntegrationTest
  # Turn off transactional fixtures for this test since we are loading
  # the fixtures database anyway. This will prevent the timestamp change
  # from spilling into other tests.
  self.use_transactional_tests = false

  setup do
    # Ensure the test db has its environment metadata set to test,
    # otherwise tasks farther down will fail.  New for Rails 5
    Rake::Task['db:environment:set'].invoke
    # Normalize time in order to match fixture file
    travel_to Time.zone.parse('2015-03-01T12:00:00') do
      ActiveRecord::Schema.verbose = false
      Rake::Task['db:schema:load'].reenable
      Rake::Task['db:schema:load'].invoke
      Rake::Task['db:fixtures:load'].reenable
      Rake::Task['db:fixtures:load'].invoke
    end
  end

  test 'feed matches fixture file' do
    get feed_path
    assert_equal contents('feed.atom'), response.body
  end
end