-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathsetup_tests_spec.rb
More file actions
93 lines (77 loc) · 3.35 KB
/
setup_tests_spec.rb
File metadata and controls
93 lines (77 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
require "spec_helper"
describe DataMigrate::Tasks::SetupTests do
let(:file_contents_with_injection) do
<<~FILE_CONTENTS
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# data_migrate: Include data migrations for writing test coverage
Dir[Rails.root.join(DataMigrate.config.data_migrations_path, '*.rb')].each { |f| require f }
FILE_CONTENTS
end
let(:file_contents_without_injection) do
<<~FILE_CONTENTS
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
FILE_CONTENTS
end
let(:file_contents_without_injection_matcher) do
<<~FILE_CONTENTS
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
FILE_CONTENTS
end
let(:rails_root) { Pathname.new('/fake/app') }
let(:test_suite_inferrer) { instance_double(DataMigrate::Helpers::InferTestSuiteType) }
before do
allow(Rails).to receive(:root).and_return(rails_root)
allow(DataMigrate::Helpers::InferTestSuiteType).to receive(:new).and_return(test_suite_inferrer)
end
describe "#call" do
context 'when the injected code already exists' do
it 'returns early' do
allow(test_suite_inferrer).to receive(:call).and_return(:rspec)
allow(File).to receive(:readlines).and_return(file_contents_with_injection.lines)
expect(File).not_to receive(:open)
expect {
DataMigrate::Tasks::SetupTests.new.call
}.to output(/data_migrate: Test setup already exists./).to_stdout
end
context 'when the INJECTION_MATCHER is not found' do
it 'returns early' do
allow(test_suite_inferrer).to receive(:call).and_return(:rspec)
allow(File).to receive(:readlines).and_return(file_contents_without_injection_matcher.lines)
expect(File).not_to receive(:open)
expect {
DataMigrate::Tasks::SetupTests.new.call
}.to output(/data_migrate: config\/environment.rb was not found in the test helper file./).to_stdout
end
end
context 'for RSpec' do
it 'calls File.open for writing to rails_helper.rb' do
allow(test_suite_inferrer).to receive(:call).and_return(:rspec)
allow(File).to receive(:readlines).and_return(file_contents_without_injection.lines)
expect(File).to receive(:open).with(rails_root.join('spec', 'rails_helper.rb'), 'w')
expect {
DataMigrate::Tasks::SetupTests.new.call
}.to output(/data_migrate: Test setup complete./).to_stdout
end
end
context 'for Minitest' do
it 'calls File.open for writing to test_helper.rb' do
allow(test_suite_inferrer).to receive(:call).and_return(:minitest)
allow(File).to receive(:readlines).and_return(file_contents_without_injection.lines)
expect(File).to receive(:open).with(rails_root.join('test', 'test_helper.rb'), 'w')
expect {
DataMigrate::Tasks::SetupTests.new.call
}.to output(/data_migrate: Test setup complete./).to_stdout
end
end
end
end
end