为RSpec套件中的所有示例设置变量一次(不使用全局变量) [英] Set variable once for all examples in RSpec suite (without using a global variable)

查看:372
本文介绍了为RSpec套件中的所有示例设置变量一次(不使用全局变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在在<$ c $中设置一个全局变量c> spec_helper ,用于检查规范是否在调试模式下运行

  $ debug = ENV.key?('DEBUG')&& (ENV ['DEBUG']。casecmp('false')!= 0)&& (ENV ['DEBUG']。casecmp('no')!= 0)

将这些信息提供给套件中的所有示例,而无需使用全局变量,也无需重新计算每个上下文和/或示例的值? (我的理解是,在(:all)块之前使用会在每个上下文中重新计算一次;但是, before(:suite)不能用于设置实例变量。)



(注意:我要求了解更多关于优秀设计的知识来解决这个问题问题,我知道一个全局并不是什么大问题。)为解决这个问题,我通常编写自定义模块,我可以包括在 spec_helper.rb 文件中。



假设我正在测试后端API,每一次JSON响应正文都不想分析。

  spec / 
spec / spec_helper.rb
spec / support / request_helper.rb
spec / controllers / api / v1 / users_controller_spec.rb



<

 #request_helper.rb 
模块请求我在第一个模块中定义了一个函数。
模块JsonHelpers
def json_response
@json_response || = JSON.parse(response.body,symbolize_names:true)
end
end
end

然后,我在一些测试类型中默认包含这个模块

  #spec_helper .rb 
#...
RSpec.configure do | config |
config.include Request :: JsonHelpers,输入::controller
end

然后我使用测试中定义的方法。

 #users_controller_spec.rb 
require'rails_helper'

RSpec.describe Api :: V1 :: UsersController,输入::controller do
#...

描述POST #createdo

上下文当成功创建时做
之前(:每个)做
@user_attributes = FactoryGirl.attributes_for:用户
发布:创建,参数:{user:@user_attributes}
end

为刚刚创建的用户记录呈现json表示形式do
user_response = json_response [:user]
expect(user_response [:email])。to eq @user_attributes [:email])
end

it {should respond_with 201}
end
end
  pre 
$ b

模块E nvHelper
def is_debug_mode?
@debug_mode || = ENV ['DEBUG']
结束
结束

然后您可以包含它并在测试中简单地使用方法 is_debug_mode?


What is the conventional way to set a variable once to be used by all examples in an RSpec suite?

I currently set a global variable in spec_helper that checks whether the specs are being run in a "debug mode"

$debug = ENV.key?('DEBUG') && (ENV['DEBUG'].casecmp('false') != 0) && (ENV['DEBUG'].casecmp('no') != 0)

How do I make this information available to all the examples in the suite without using a global variable and without re-computing the value for each context and/or example? (My understanding is that using a before(:all) block would re-compute it once per context; but, that before(:suite) can't be used to set instance variables.)

(Note: I'm asking more to learn about good design that to address this specific problem. I know one global isn't a big deal.)

解决方案

for this purpose I usually write custom modules that I can include in the spec_helper.rb file.

Let's say that I am testing a back-end API and I don't want to parse every time the JSON response body.

spec/
spec/spec_helper.rb
spec/support/request_helper.rb
spec/controllers/api/v1/users_controller_spec.rb

I first define a function in a module placed under the support subfolder.

# request_helper.rb
module Request
  module JsonHelpers
    def json_response
      @json_response ||= JSON.parse(response.body, symbolize_names: true)
    end
  end
end

Then I include this module by default for some test types

#spec_helper.rb
#...
RSpec.configure do |config|
  config.include Request::JsonHelpers, type: :controller
end

Then I use methods defined in the test.

# users_controller_spec.rb
require 'rails_helper'

RSpec.describe Api::V1::UsersController, type: :controller do
  # ...

 describe "POST #create" do

    context "when is successfully created" do
      before(:each) do
        @user_attributes = FactoryGirl.attributes_for :user
        post :create, params: { user: @user_attributes }
      end

      it "renders the json representation for the user record just created" do
        user_response = json_response[:user]
        expect(user_response[:email]).to eq(@user_attributes[:email])
      end

      it { should respond_with 201 }
    end
end

In your case, you could create a module such as

module EnvHelper
  def is_debug_mode?
    @debug_mode ||= ENV['DEBUG']
  end
end

Then you can include it and simply use the method is_debug_mode? in your tests.

这篇关于为RSpec套件中的所有示例设置变量一次(不使用全局变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆