运行rspec for ruby​​时如何更改环境变量? [英] how can I change environment variables when running rspec for ruby?

查看:143
本文介绍了运行rspec for ruby​​时如何更改环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个ruby脚本,并用rspec进行测试。



我把我的环境放在一个env.rb文件(现在),所以我可以在本地访问它们在生产中将它们放在配置变量中。



但是当我运行rspec时,我想要不同的环境变量。两个用例:




  • 我运行Twilio,所以我想要更改用于他们的测试凭据的SID

  • 我将数据库中的东西存储为一项服务,并希望有一个单独的测试数据库


解决方案




  • 在上下文中使用 ENV [FOO_BAR]显式设置ENV vars ] =baz

  • 在您的初始化器中检查 Rails.env.test?以设置twilio和具有测试特定选择的其他客户端

  • 具有所有env设置进行测试的文件,然后使用 dotenv



我个人更喜欢仅在创建对象时使用ENV vars,然后将env var值传递给构造函数,所以我可以测试对象类而不关心ENV,并且我可以测试使用env var初始化另一个对象的对象,只需断言所创建的使用t他的env var。



所以你可以改变一些如

  class Client 
def initialize
@restclient = RestClient :: Resource.new(ENV [API_URL])
end
end

to

  class Client 
def初始化(url)
@restclient = RestClient :: Resource.new(url)
end
end

,并且有什么是初始化该实例,然后传递env var的值

  def fetch_content 
client = Client.new(ENV [API_URL])
#...
end

这样你可以测试客户端类,而不用通过传递任何url来关心env var,然后可以测试类实例化客户端作为

 它使用客户端do 
ENV [API_URL] =foo.com
expect(Client).to receive(:new).with(ENV [API_URL])
subject.fetch_con帐篷
end

更新env var的一个问题是更改在整个休息期间持续存在的测试套件,如果您不希望在某些测试中出现问题,可能会导致问题,在这些情况下,您可以使用

  expect(ENV).to receive(:[])。with(API_URL)。and_return(foo.com)


I have several ruby scripts and test them with rspec.

I put my environments in a env.rb file (for now) so I can access them locally and in production put them in the config variables.

But when I run rspec, I would like different environment variables. Two use cases:

  • I run Twilio, so I want to be able to change the SID used to their Test Credentials
  • I store things in a database as a service, and want to have a separate test database

解决方案

You can

  • set ENV vars explicitly in the context with ENV["FOO_BAR"] = "baz"
  • check Rails.env.test? in your initializers to setup twilio and other clients with test specific opts
  • have a file with all your env setup for test and then use dotenv

I personally prefer to use ENV vars only when creating objects and then passing the env var value to the constructor, so I can test the object class without caring about ENV, and I can test the object that initializes the other object with the env var, by just asserting the creation used the env var.

So you'd change something like

class Client
  def initialize
    @restclient = RestClient::Resource.new(ENV["API_URL"])
  end
end

to

class Client
  def initialize(url)
    @restclient = RestClient::Resource.new(url)
  end
end

and have whatever is initializing that instance to then pass the value of the env var

def fetch_content
  client = Client.new(ENV["API_URL"])
  # ...
end

this way you can test the Client class without caring about the env var by just passing any url, and then can test the class that instantiates the client as

it "uses client" do
  ENV["API_URL"] = "foo.com"
  expect(Client).to receive(:new).with(ENV["API_URL"])  
  subject.fetch_content
end

one problem with updating the env var is that the change persist throughout the rest of the test suite, and may cause problems if you don't expect it to be there in some tests, in these cases you can mock the value with

expect(ENV).to receive(:[]).with("API_URL").and_return("foo.com")

这篇关于运行rspec for ruby​​时如何更改环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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