|
| 1 | +require 'spec_helper' |
| 2 | +require 'open3' |
| 3 | +require 'rbconfig' |
| 4 | + |
| 5 | +describe 'RSpec fabricators' do |
| 6 | + let(:application_fabricator) do |
| 7 | + <<~RUBY |
| 8 | + Fabricator(:team, class_name: 'Team') do |
| 9 | + name { 'Application team' } |
| 10 | + token { 'application-token' } |
| 11 | + team_id { 'TAPPLICATION' } |
| 12 | + end |
| 13 | + RUBY |
| 14 | + end |
| 15 | + |
| 16 | + def fabricator_names_after(code) |
| 17 | + script = <<~RUBY |
| 18 | + require 'logger' |
| 19 | + Bundler.require |
| 20 | + require 'fabrication' |
| 21 | + require 'json' |
| 22 | + if defined?(ActiveRecord::Base) |
| 23 | + ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL')) |
| 24 | + end |
| 25 | + #{code} |
| 26 | + puts JSON.generate( |
| 27 | + application: Fabricate.build(:team).name, |
| 28 | + framework: Fabricate.build(:slack_ruby_bot_server_team, name: 'Framework team').name |
| 29 | + ) |
| 30 | + RUBY |
| 31 | + output, error, status = Open3.capture3(RbConfig.ruby, '-Ilib', '-rbundler/setup', '-e', script) |
| 32 | + expect(status.success?).to be(true), "#{output}\n#{error}" |
| 33 | + JSON.parse(output.lines.last) |
| 34 | + end |
| 35 | + |
| 36 | + it 'persists framework installations with unique credentials independently of the legacy name' do |
| 37 | + first = Fabricate(:slack_ruby_bot_server_team) |
| 38 | + second = Fabricate(:slack_ruby_bot_server_team) |
| 39 | + expect(Team.find(first.id).token).to eq first.token |
| 40 | + expect(first.token).not_to eq second.token |
| 41 | + expect(first.team_id).not_to eq second.team_id |
| 42 | + end |
| 43 | + |
| 44 | + it 'keeps the legacy helper working without an application fabricator' do |
| 45 | + names = fabricator_names_after("require 'slack-ruby-bot-server/rspec'") |
| 46 | + expect(names['application']).not_to be_empty |
| 47 | + expect(names['framework']).to eq 'Framework team' |
| 48 | + end |
| 49 | + |
| 50 | + it 'preserves an application fabricator registered before the legacy helper' do |
| 51 | + names = fabricator_names_after(<<~RUBY) |
| 52 | + #{application_fabricator} |
| 53 | + require 'slack-ruby-bot-server/rspec' |
| 54 | + RUBY |
| 55 | + expect(names).to eq('application' => 'Application team', 'framework' => 'Framework team') |
| 56 | + end |
| 57 | + |
| 58 | + it 'allows applications to inherit and customize framework defaults' do |
| 59 | + names = fabricator_names_after(<<~RUBY) |
| 60 | + require 'slack-ruby-bot-server/rspec/fabricators' |
| 61 | + Fabricator(:team, from: :slack_ruby_bot_server_team) do |
| 62 | + name { 'Application team' } |
| 63 | + end |
| 64 | + RUBY |
| 65 | + expect(names).to eq('application' => 'Application team', 'framework' => 'Framework team') |
| 66 | + end |
| 67 | + |
| 68 | + %w[before after].each do |order| |
| 69 | + it "allows an application fabricator #{order} the prefixed-only helper" do |
| 70 | + setup = [application_fabricator, "require 'slack-ruby-bot-server/rspec/fabricators'"] |
| 71 | + setup.reverse! if order == 'after' |
| 72 | + expect(fabricator_names_after(setup.join("\n"))).to eq( |
| 73 | + 'application' => 'Application team', 'framework' => 'Framework team' |
| 74 | + ) |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments