目前我有130行具有类似Ruby代码的代码:
@acquirer = create(:acquirer,
name: 'OmniPay Datawire acquirer',
country_code: 372,
timezone: 'Europe/Dublin'
)
@reseller = create(:reseller,
name: 'OmniPay Datawire reseller',
domain: 'op-res-domain')
@merchant = create(:merchant,
name: 'OmniPay Datawire Merchant',
state_raw: 2,
supported_features: 15,
merchant_owner: @reseller,
login: 'test',
pwd: 'test',
notification_url: 'http://127.0.0.1/notification_url')
@contract = create(:contract_supports_all,
name: 'Omnipay Datawire Contract',
gateway: 'test',
mpi: 'test',
acquirer: @acquirer,
contract_owner: @reseller,
merchant: @merchant,
descriptor: 'Descriptor',
activated: '1',
)
如何创建重用此代码的工厂方法?
我想尽可能多地节省空间。你能告诉我一些非常有用的例子,我可以用它作为解决方案吗?
答案 0 :(得分:0)
您可以使用将填充数据的方法创建模块:
# spec/support/sample
module Sample
def populate_data
@acquirer = create(:acquirer,
name: 'OmniPay Datawire acquirer',
country_code: 372,
timezone: 'Europe/Dublin'
)
@reseller = create(:reseller,
name: 'OmniPay Datawire reseller',
domain: 'op-res-domain')
@merchant = create(:merchant,
name: 'OmniPay Datawire Merchant',
state_raw: 2,
supported_features: 15,
merchant_owner: @reseller,
login: 'test',
pwd: 'test',
notification_url: 'http://127.0.0.1/notification_url')
@contract = create(:contract_supports_all,
name: 'Omnipay Datawire Contract',
gateway: 'test',
mpi: 'test',
acquirer: @acquirer,
contract_owner: @reseller,
merchant: @merchant,
descriptor: 'Descriptor',
activated: '1',
)
end
end
并将其包含在测试中:
require 'support/sample'
#spec/sample_test_spec
describe 'test something' do
include Sample
before do
populate_data
end
it 'test something' do
expect(@acquirer).to_not be nil
end
end