在Rails API中针对测试环境运行Pact

问题描述 投票:0回答:1

只需使用Pact对抗我的Rails API,并注意到默认情况下开箱即用的Pact设置会针对“开发”环境运行。

如何配置为针对“测试”环境运行,而不必在运行任务时在命令行中指定它(RAILS_ENV = test)。无法在文档中轻松找到它如何做到这一点。

使用以下宝石:

pact (1.10.0)
pact-mock_service (0.12.1)
pact-support (0.6.0)

pact_helper.rb:

require 'pact/provider/rspec'

Pact.service_provider 'Auslan API Service' do
  honours_pact_with 'Auslan Web App' do
    # This example points to a local file, however, on a real project with a continuous
    # integration box, you would use a [Pact Broker](https://github.com/bethesque/pact_broker) or publish your pacts as artifacts,
    # and point the pact_uri to the pact published by the last successful build.
    pact_uri './user-specs-user-api.json' # need to update this
  end
end

Pact.configure do | config |
  config.diff_formatter = :embedded
end

Pact.provider_states_for 'User-Specs' do
  provider_state 'there are users already added inside the database' do
    set_up do
      user1 = User.create(email: '[email protected]', first_name: 'Jane', last_name: 'Doe', password: 'abcd#1234')

      # set the Auth token
      token = Knock::AuthToken.new(payload: { sub: user1.id }).token   
      pacts = File.join(File.dirname(File.expand_path(__FILE__)), '../../user-specs-user-api.json')

      Dir.glob(pacts).each do |f|
        text = File.read(f)
        output_of_gsub = text.gsub(/\"Authorization\"\s*:\s*\".+\"/) { "\"Authorization\": \"Bearer #{token}\"" }
        File.open(f, "w") { |file| file.puts output_of_gsub }
      end
    end
  end
end

谢谢,莫

ruby-on-rails ruby testing pact pact-ruby
1个回答
1
投票

我没有写任何代码来允许这种情况发生。应用程序加载的代码部分在这里:https://github.com/pact-foundation/pact-ruby/blob/master/lib/pact/provider/configuration/service_provider_dsl.rb#L16

如果你有一个句柄,你可以覆盖配置中的应用程序,但我不记得如何使用我的头顶上的Rails应用程序。也许你可以玩一下Rack构建器,看看你是否可以传入任何环境变量。如果你能弄清楚如何去做,我会很乐意接受公关。

© www.soinside.com 2019 - 2024. All rights reserved.