Skip to content

Commit cf6bfd8

Browse files
author
Lewis Youl
committed
chore: configure rspec and factory bot
1 parent a48681b commit cf6bfd8

File tree

8 files changed

+215
-2
lines changed

8 files changed

+215
-2
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ gem 'friendly_id', '~> 5.4.0'
4444
gem 'bootsnap', '>= 1.4.2', require: false
4545

4646
group :development, :test do
47-
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
48-
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
47+
gem 'factory_bot_rails'
48+
gem 'rspec-rails', '~> 4.1.0'
4949
end
5050

5151
group :development do

Gemfile.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ GEM
102102
activesupport
103103
concurrent-ruby (1.1.6)
104104
crass (1.0.6)
105+
diff-lcs (1.4.4)
105106
domain_name (0.5.20190701)
106107
unf (>= 0.0.5, < 1.0.0)
107108
equalizer (0.0.11)
108109
erubi (1.9.0)
110+
factory_bot (6.1.0)
111+
activesupport (>= 5.0.0)
112+
factory_bot_rails (6.1.0)
113+
factory_bot (~> 6.1.0)
114+
railties (>= 5.0.0)
109115
faraday (1.3.0)
110116
faraday-net_http (~> 1.0)
111117
multipart-post (>= 1.2, < 3)
@@ -234,6 +240,23 @@ GEM
234240
responders (3.0.1)
235241
actionpack (>= 5.0)
236242
railties (>= 5.0)
243+
rspec-core (3.10.1)
244+
rspec-support (~> 3.10.0)
245+
rspec-expectations (3.10.1)
246+
diff-lcs (>= 1.2.0, < 2.0)
247+
rspec-support (~> 3.10.0)
248+
rspec-mocks (3.10.2)
249+
diff-lcs (>= 1.2.0, < 2.0)
250+
rspec-support (~> 3.10.0)
251+
rspec-rails (4.1.2)
252+
actionpack (>= 4.2)
253+
activesupport (>= 4.2)
254+
railties (>= 4.2)
255+
rspec-core (~> 3.10)
256+
rspec-expectations (~> 3.10)
257+
rspec-mocks (~> 3.10)
258+
rspec-support (~> 3.10)
259+
rspec-support (3.10.2)
237260
ruby2_keywords (0.0.4)
238261
ruby_dep (1.5.0)
239262
sass-rails (6.0.0)
@@ -307,6 +330,7 @@ DEPENDENCIES
307330
bootsnap (>= 1.4.2)
308331
byebug
309332
devise!
333+
factory_bot_rails
310334
friendly_id (~> 5.4.0)
311335
jbuilder (~> 2.7)
312336
letter_opener
@@ -318,6 +342,7 @@ DEPENDENCIES
318342
puma (~> 4.1)
319343
rails (~> 6.0.2, >= 6.0.2.2)
320344
redis
345+
rspec-rails (~> 4.1.0)
321346
sass-rails (>= 6)
322347
spring
323348
spring-watcher-listen (~> 2.0.0)

spec/factories/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :user do
3+
name { 'LewisYoul' }
4+
email { 'lewisyoul@gmail.com' }
5+
password { '12345676543!@£$@ytrefgKJHGFG' }
6+
confirmed_at { Date.today }
7+
end
8+
end

spec/models/user_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RSpec.describe User do
2+
let(:user) { FactoryBot.create(:user) }
3+
4+
describe '#created' do
5+
it 'returns true when the user created the snippet' do
6+
expect(user).to eq(user)
7+
end
8+
end
9+
end

spec/rails_helper.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
require File.expand_path('../config/environment', __dir__)
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
require 'rspec/rails'
8+
require 'support/factory_bot.rb'
9+
10+
RSpec.configure do |config|
11+
config.include FactoryBot::Syntax::Methods
12+
end
13+
# Add additional requires below this line. Rails is not loaded until this point!
14+
15+
# Requires supporting ruby files with custom matchers and macros, etc, in
16+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
17+
# run as spec files by default. This means that files in spec/support that end
18+
# in _spec.rb will both be required and run as specs, causing the specs to be
19+
# run twice. It is recommended that you do not name files matching this glob to
20+
# end with _spec.rb. You can configure this pattern with the --pattern
21+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
22+
#
23+
# The following line is provided for convenience purposes. It has the downside
24+
# of increasing the boot-up time by auto-requiring all files in the support
25+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
26+
# require only the support files necessary.
27+
#
28+
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
29+
30+
# Checks for pending migrations and applies them before tests are run.
31+
# If you are not using ActiveRecord, you can remove these lines.
32+
begin
33+
ActiveRecord::Migration.maintain_test_schema!
34+
rescue ActiveRecord::PendingMigrationError => e
35+
puts e.to_s.strip
36+
exit 1
37+
end
38+
RSpec.configure do |config|
39+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
40+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
41+
42+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
43+
# examples within a transaction, remove the following line or assign false
44+
# instead of true.
45+
config.use_transactional_fixtures = true
46+
47+
# You can uncomment this line to turn off ActiveRecord support entirely.
48+
# config.use_active_record = false
49+
50+
# RSpec Rails can automatically mix in different behaviours to your tests
51+
# based on their file location, for example enabling you to call `get` and
52+
# `post` in specs under `spec/controllers`.
53+
#
54+
# You can disable this behaviour by removing the line below, and instead
55+
# explicitly tag your specs with their type, e.g.:
56+
#
57+
# RSpec.describe UsersController, type: :controller do
58+
# # ...
59+
# end
60+
#
61+
# The different available types are documented in the features, such as in
62+
# https://relishapp.com/rspec/rspec-rails/docs
63+
config.infer_spec_type_from_file_location!
64+
65+
# Filter lines from Rails gems in backtraces.
66+
config.filter_rails_from_backtrace!
67+
# arbitrary gems may also be filtered via:
68+
# config.filter_gems_from_backtrace("gem name")
69+
end

spec/spec_helper.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require 'rails_helper'
2+
3+
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
4+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5+
# The generated `.rspec` file contains `--require spec_helper` which will cause
6+
# this file to always be loaded, without a need to explicitly require it in any
7+
# files.
8+
#
9+
# Given that it is always loaded, you are encouraged to keep this file as
10+
# light-weight as possible. Requiring heavyweight dependencies from this file
11+
# will add to the boot time of your test suite on EVERY test run, even for an
12+
# individual file that may not need all of that loaded. Instead, consider making
13+
# a separate helper file that requires the additional dependencies and performs
14+
# the additional setup, and require it from the spec files that actually need
15+
# it.
16+
#
17+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18+
RSpec.configure do |config|
19+
# rspec-expectations config goes here. You can use an alternate
20+
# assertion/expectation library such as wrong or the stdlib/minitest
21+
# assertions if you prefer.
22+
config.expect_with :rspec do |expectations|
23+
# This option will default to `true` in RSpec 4. It makes the `description`
24+
# and `failure_message` of custom matchers include text for helper methods
25+
# defined using `chain`, e.g.:
26+
# be_bigger_than(2).and_smaller_than(4).description
27+
# # => "be bigger than 2 and smaller than 4"
28+
# ...rather than:
29+
# # => "be bigger than 2"
30+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31+
end
32+
33+
# rspec-mocks config goes here. You can use an alternate test double
34+
# library (such as bogus or mocha) by changing the `mock_with` option here.
35+
config.mock_with :rspec do |mocks|
36+
# Prevents you from mocking or stubbing a method that does not exist on
37+
# a real object. This is generally recommended, and will default to
38+
# `true` in RSpec 4.
39+
mocks.verify_partial_doubles = true
40+
end
41+
42+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
43+
# have no way to turn it off -- the option exists only for backwards
44+
# compatibility in RSpec 3). It causes shared context metadata to be
45+
# inherited by the metadata hash of host groups and examples, rather than
46+
# triggering implicit auto-inclusion in groups with matching metadata.
47+
config.shared_context_metadata_behavior = :apply_to_host_groups
48+
49+
# The settings below are suggested to provide a good initial experience
50+
# with RSpec, but feel free to customize to your heart's content.
51+
=begin
52+
# This allows you to limit a spec run to individual examples or groups
53+
# you care about by tagging them with `:focus` metadata. When nothing
54+
# is tagged with `:focus`, all examples get run. RSpec also provides
55+
# aliases for `it`, `describe`, and `context` that include `:focus`
56+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
57+
config.filter_run_when_matching :focus
58+
59+
# Allows RSpec to persist some state between runs in order to support
60+
# the `--only-failures` and `--next-failure` CLI options. We recommend
61+
# you configure your source control system to ignore this file.
62+
config.example_status_persistence_file_path = "spec/examples.txt"
63+
64+
# Limits the available syntax to the non-monkey patched syntax that is
65+
# recommended. For more details, see:
66+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
67+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
68+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
69+
config.disable_monkey_patching!
70+
71+
# Many RSpec users commonly either run the entire suite or an individual
72+
# file, and it's useful to allow more verbose output when running an
73+
# individual spec file.
74+
if config.files_to_run.one?
75+
# Use the documentation formatter for detailed output,
76+
# unless a formatter has already been configured
77+
# (e.g. via a command-line flag).
78+
config.default_formatter = "doc"
79+
end
80+
81+
# Print the 10 slowest examples and example groups at the
82+
# end of the spec run, to help surface which specs are running
83+
# particularly slow.
84+
config.profile_examples = 10
85+
86+
# Run specs in random order to surface order dependencies. If you find an
87+
# order dependency and want to debug it, you can fix the order by providing
88+
# the seed, which is printed after each run.
89+
# --seed 1234
90+
config.order = :random
91+
92+
# Seed global randomization in this process using the `--seed` CLI option.
93+
# Setting this allows you to use `--seed` to deterministically reproduce
94+
# test failures related to randomization by passing the same `--seed` value
95+
# as the one that triggered the failure.
96+
Kernel.srand config.seed
97+
=end
98+
end

spec/support/factory_bot.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.include FactoryBot::Syntax::Methods
3+
end

0 commit comments

Comments
 (0)