简体   繁体   English

rails 如何在没有 rspec 的测试之前加载测试夹具?

[英]rails How to load test fixtures before tests without rspec?

Right now, I have a unit test that looks something like this:现在,我有一个看起来像这样的单元测试:

class ModelTest < ActiveSupport::TestCase
  # fixtures are not loaded yet
  # would like to setup something here ONCE (not once for every test)
  context 'test1' do
    #fixtures are not loaded in here either
    should 'test1 test1' do
      #fixtures are only loaded in here
     end
  end
end

As the comments suggest, I want to setup something there ONCE (not once for every test) before all the tests run, but in order to do so, it requires the fixtures defined in test/fixtures folder to be loaded outside the scope of the tests and before the tests even run.正如评论所暗示的,我想在所有测试运行之前设置一次(不是每个测试一次),但为了做到这一点,它需要在test/fixtures文件夹中定义的夹具加载到 scope 之外测试甚至在测试运行之前。 Is there any way of accomplishing this?有没有办法做到这一点?

You can use before(:suite) hook.您可以使用before(:suite)钩子。 It will be called only once.它只会被调用一次。

before(:suite) do
  #load test/fixtures here
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在导轨的模拟对象中加载夹具以进行测试? - How to load fixtures in a mock object in rails for tests? Rails 生成的应用程序无需测试,如何在不重新创建项目的情况下生成 rspec 测试文件? - Rails generated app without tests, how can i generate rspec test files without recreating project? 使用 Watir 进行 Rails 测试。 如何从 /test/fixtures 加载我的测试数据库 - Rails testing with Watir. How do I load my test db with the fixtures from /test/fixtures 如何在不运行 rake 规范的情况下为 Rails rspec 测试准备测试数据库? - How do I prepare test database(s) for Rails rspec tests without running rake spec? Rails 3和Rspec 2关闭各个测试的事务夹具 - Rails 3 and Rspec 2 turn off transactional fixtures for individual tests 在Rails中,如何在特定的Rspec测试之前加载所有代码? - In Rails, how can I eager load all code before a specific Rspec test? 在加载课程之前加载导轨固定装置 - Load rails fixtures before classes are loaded 在Rails中从Minitest切换到RSpec时,如何使“ rails test”(或“ rake test”)运行RSpec测试? - When switching from Minitest to RSpec in Rails, how to make `rails test` (or `rake test`) run the RSpec tests? Rails Fixtures 未使用 rspec 加载 - Rails Fixtures not loading with rspec 如何在测试之前阻止rspec删除测试数据库 - How to stop rspec from dropping the test database before tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM