-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
generators.rb
125 lines (100 loc) · 4.15 KB
/
generators.rb
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module RSpec
module Rails
module Specs
module Generators
module Macros
# Tell the generator where to put its output (what it thinks of as
# Rails.root)
def set_default_destination
destination File.expand_path('../../tmp', __dir__)
end
def setup_default_destination
set_default_destination
before { prepare_destination }
end
end
def self.included(klass)
klass.extend(Macros)
klass.include(RSpec::Rails::FeatureCheck)
end
RSpec.shared_examples_for 'a model generator with fixtures' do |name, class_name|
before { run_generator [name, '--fixture'] }
describe 'the spec' do
subject { file("spec/models/#{name}_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe #{class_name}, #{type_metatag(:model)}/) }
end
describe 'the fixtures' do
subject { file("spec/fixtures/#{name}.yml") }
it { is_expected.to contain(Regexp.new('# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html')) }
end
end
RSpec.shared_examples_for "a request spec generator" do
describe 'generated with flag `--no-request-specs`' do
before do
run_generator %w[posts --no-request-specs]
end
subject(:request_spec) { file('spec/requests/posts_spec.rb') }
it "does not create the request spec" do
expect(request_spec).not_to exist
end
end
describe 'generated with no flags' do
before do
run_generator name
end
subject(:request_spec) { file(spec_file_name) }
context 'When NAME=posts' do
let(:name) { %w[posts] }
let(:spec_file_name) { 'spec/requests/posts_spec.rb' }
it "creates the request spec" do
expect(request_spec).to exist
end
it "the generator requires 'rails_helper'" do
expect(request_spec).to contain(/require 'rails_helper'/)
end
it "the generator describes the provided NAME without monkey " \
"patching setting the type to `:request`" do
expect(request_spec).to contain(
/^RSpec.describe "Posts", #{type_metatag(:request)}/
)
end
it "the generator includes a sample GET request" do
expect(request_spec).to contain(/describe "GET \/posts"/)
end
it "the generator sends the GET request to the index path" do
expect(request_spec).to contain(/get posts_index_path/)
end
end
context 'When NAME=api/posts' do
let(:name) { %w[api/posts] }
let(:spec_file_name) { 'spec/requests/api/posts_spec.rb' }
it "creates the request spec" do
expect(request_spec).to exist
end
it "the generator requires 'rails_helper'" do
expect(request_spec).to contain(/require 'rails_helper'/)
end
it "the generator describes the provided NAME without monkey " \
"patching setting the type to `:request`" do
expect(request_spec).to contain(
/^RSpec.describe "Api::Posts", #{type_metatag(:request)}/
)
end
it "the generator includes a sample GET request" do
expect(request_spec).to contain(/describe "GET \/api\/posts"/)
end
it "the generator sends the GET request to the index path" do
expect(request_spec).to contain(/get api_posts_index_path\n/)
end
end
end
end
end
end
end
end
RSpec.configure do |config|
config.include RSpec::Rails::Specs::Generators, type: :generator
end