【问题标题】:rspec setup method in separate file单独文件中的 rspec 设置方法
【发布时间】:2014-02-20 03:01:17
【问题描述】:

我正在尝试为 rspec 创建一个设置文件,以便我的 '_spec.rb' 测试只有这样的 'it' 块:

setup.rb

require 'rspec'

module Setup
 describe "setup" do

 before(:each) do
  @foo = 'hello'
  'do something'
 end

 after(:each) do
 'do something'
 end
end

spec/test_spec.rb

require_relative '../setup.rb'

include Setup

it "test_sample" do
 puts @foo
 'do test'
end

这将返回未定义的变量。有人可以照亮我的问题吗?我有多个测试想要共享相同的设置,并且我不想为每个测试重复设置。

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    为此使用 rspec 的 configure 方法:

    # in spec_helper.rb (or setup.rb)
    RSpec.configure do |config|
      config.before(:each) do
      ...
      end
    
      config.after(:each) do
      ...
      end
    end
    

    有关设置配置的更多信息,请参阅here

    【讨论】:

    • 感谢您的快速回复!
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 2011-01-14
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多