spec/fabricators/contract_fabricator.rb in pacto-0.4.0.rc1 vs spec/fabricators/contract_fabricator.rb in pacto-0.4.0.rc2
- old
+ new
@@ -1,13 +1,20 @@
# -*- encoding : utf-8 -*-
require 'pacto'
require 'hashie/mash'
# Fabricators for contracts or parts of contracts
+unless defined? PACTO_DEFAULT_FORMAT
+ PACTO_DEFAULT_FORMAT = (ENV['PACTO_DEFAULT_FORMAT'] || 'legacy')
+ CONTRACT_CLASS = Pacto::Formats.const_get(PACTO_DEFAULT_FORMAT.capitalize).const_get('Contract')
+ REQUEST_CLAUSE_CLASS = Pacto::Formats.const_get(PACTO_DEFAULT_FORMAT.capitalize).const_get('RequestClause')
+ RESPONSE_CLAUSE_CLASS = Pacto::Formats.const_get(PACTO_DEFAULT_FORMAT.capitalize).const_get('ResponseClause')
+end
-Fabricator(:contract, from: Pacto::Contract) do
- initialize_with { @_klass.new to_hash } # Hash based initialization
+Fabricator(:contract, from: CONTRACT_CLASS) do
+ initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
+
transient example_count: 0
name { 'Dummy Contract' }
file { 'file:///does/not/exist/dummy_contract.json' }
request { Fabricate(:request_clause).to_hash }
response { Fabricate(:response_clause).to_hash }
@@ -21,21 +28,23 @@
examples
else
nil
end
end
+
+ # after_save { | contract, _transients | contract.freeze }
end
-Fabricator(:partial_contract, from: Pacto::Contract) do
- initialize_with { @_klass.new to_hash } # Hash based initialization
+Fabricator(:partial_contract, from: CONTRACT_CLASS) do
+ initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
name { 'Dummy Contract' }
file { 'file:///does/not/exist/dummy_contract.json' }
request { Fabricate(:request_clause).to_hash }
end
-Fabricator(:request_clause, from: Pacto::RequestClause) do
- initialize_with { @_klass.new to_hash } # Hash based initialization
+Fabricator(:request_clause, from: REQUEST_CLAUSE_CLASS) do
+ initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
host { 'example.com' }
http_method { 'GET' }
path { '/abcd' }
headers do
{
@@ -47,11 +56,11 @@
}
end
params {}
end
-Fabricator(:response_clause, from: Pacto::ResponseClause) do
- initialize_with { @_klass.new to_hash } # Hash based initialization
+Fabricator(:response_clause, from: RESPONSE_CLAUSE_CLASS) do
+ initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
status { 200 }
headers do
{
'Content-Type' => 'application/json'
}