2017-02-09 93 views
0

只是在Pact對抗我的Rails API時發現,默認情況下,開箱即用的Pact安裝程序針對「開發」環境運行。在Rails API中針對測試環境運行契約

當我運行任務(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 

感謝, 莫

回答

0

我沒有寫任何代碼,允許這樣的事情發生。應用程序加載的代碼部分位於以下位置:https://github.com/realestate-com-au/pact/blob/master/lib/pact/provider/configuration/service_provider_dsl.rb#L16

如果您擁有該應用程序的句柄,則可以覆蓋該配置中的應用程序,但是我不記得如何使用Rails應用程序關閉該應用程序我的頭頂。也許你可以在Rack Builder中玩一下,看看你是否可以傳入任何環境變量。如果你可以研究如何去做,我很樂意接受公關。