Skip to content

Commit 49138fb

Browse files
committed
Incorporate rspec-core's project initialization.
The Rails centric helper is now `rails_helper`. Leverage the standard RSpec configuration for creating basic configurations for `.rspec` and `spec_helper`. This also keeps us in-sync with any future changes. - Fix #1029
1 parent d3065c0 commit 49138fb

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

features/directory_structure.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Feature: Directory Structure
110110
│   ├── books_spec.rb
111111
├── routing
112112
│   └── books_routing_spec.rb
113+
├── spec_helper.rb
113114
└── tasks
114115
│   ├── irc_spec.rb
115116
└── views
@@ -133,7 +134,7 @@ Feature: Directory Structure
133134
Scenario: Non-rails related specs do not require `:type` metadata by default
134135
Given a file named "spec/ledger/entry_spec.rb" with:
135136
"""ruby
136-
require "rails_helper"
137+
require "spec_helper"
137138
138139
Entry = Struct.new(:description, :us_cents)
139140

features/support/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def copy(file_or_dir)
4747
end
4848
end
4949

50-
["spec/rails_helper.rb"].each do |file_or_dir|
50+
["spec/spec_helper.rb", "spec/rails_helper.rb"].each do |file_or_dir|
5151
write_symlink("tmp/example_app/#{file_or_dir}")
5252
end
5353
end

features/view_specs/view_spec.feature

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ Feature: view spec
167167
Then the examples should all pass
168168

169169
Scenario: passing view spec that stubs a helper method
170-
Given a file named "app/views/secrets/index.html.erb" with:
170+
Given a file named "app/helpers/application_helper.rb" with:
171+
"""ruby
172+
module ApplicationHelper
173+
def admin?
174+
false
175+
end
176+
end
177+
"""
178+
And a file named "app/views/secrets/index.html.erb" with:
171179
"""
172180
<%- if admin? %>
173181
<h1>Secret admin area</h1>
@@ -179,7 +187,7 @@ Feature: view spec
179187
180188
describe 'secrets/index' do
181189
before do
182-
view.stub(:admin?).and_return(true)
190+
allow(view).to receive(:admin?).and_return(true)
183191
end
184192
185193
it 'checks for admin access' do

lib/generators/rspec/install/install_generator.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
RSpec::Support.require_rspec_core "project_initializer"
2+
13
module Rspec
24
module Generators
35
# @private
@@ -12,14 +14,31 @@ def self.source_root
1214
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
1315
end
1416

15-
def copy_dot_rspec
16-
template '.rspec'
17+
def copy_spec_files
18+
Dir.mktmpdir do |dir|
19+
generate_rspec_init dir
20+
template File.join(dir, '.rspec'), '.rspec'
21+
directory File.join(dir, 'spec'), 'spec'
22+
end
1723
end
1824

19-
def copy_spec_files
20-
directory 'spec'
25+
def copy_rails_files
26+
template 'spec/rails_helper.rb'
2127
end
2228

29+
private
30+
31+
def generate_rspec_init(tmpdir)
32+
initializer = ::RSpec::Core::ProjectInitializer.new(
33+
:destination => tmpdir,
34+
:report_stream => StringIO.new
35+
)
36+
initializer.run
37+
gsub_file File.join(tmpdir, 'spec', 'spec_helper.rb'),
38+
'rspec --init',
39+
'rails generate rspec:install',
40+
:verbose => false
41+
end
2342
end
2443
end
2544
end

lib/generators/rspec/install/templates/.rspec

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is copied to spec/ when you run 'rails generate rspec:install'
22
ENV["RAILS_ENV"] ||= 'test'
3+
require 'spec_helper'
34
require File.expand_path("../../config/environment", __FILE__)
45
require 'rspec/rails'
56

templates/generate_stuff.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414

1515
file "app/views/things/custom_action.html.erb", "This is a template for a custom action.", {:force=>true}
1616

17+
gsub_file 'spec/spec_helper.rb', /^=(begin|end)/, ''
18+
1719
run('rake db:migrate')
1820
run('rake db:migrate RAILS_ENV=test')

0 commit comments

Comments
 (0)