Skip to content

Commit

Permalink
introduced rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
biwek committed Jul 28, 2018
1 parent 673986a commit 3322dd9
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ group :development, :test do

# code analyzer and formatter
gem 'rubocop', require: false
# behaviour-driven development
gem "rspec-rails"

# Generate sample test data
gem "faker"
end

group :development do
Expand All @@ -89,5 +94,14 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
# fixtures replacement
gem 'factory_bot_rails'
# One-liners that test common Rails functionality
gem "shoulda-matchers"
# Strategies for cleaning databases. Used to ensure a clean state for testing.
gem "database_cleaner"
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,25 @@ GEM
countries (~> 2.0)
sort_alphabetical (~> 1.0)
crass (1.0.4)
database_cleaner (1.7.0)
devise (4.4.3)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 6.0)
responders
warden (~> 1.2.3)
diff-lcs (1.3)
enumerize (2.2.2)
activesupport (>= 3.2)
erubi (1.7.1)
execjs (2.7.0)
factory_bot (4.10.0)
activesupport (>= 3.0.0)
factory_bot_rails (4.10.0)
factory_bot (~> 4.10.0)
railties (>= 3.0.0)
faker (1.9.1)
i18n (>= 0.7)
ffi (1.9.25)
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
Expand Down Expand Up @@ -194,6 +203,23 @@ GEM
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rolify (5.2.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
rubocop (0.58.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand All @@ -219,6 +245,8 @@ GEM
selenium-webdriver (3.13.0)
childprocess (~> 0.5)
rubyzip (~> 1.2)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
simple_form (4.0.1)
actionpack (>= 5.0)
activemodel (>= 5.0)
Expand Down Expand Up @@ -275,8 +303,11 @@ DEPENDENCIES
cocoon
coffee-rails (~> 4.2)
country_select
database_cleaner
devise
enumerize
factory_bot_rails
faker
font-awesome-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -290,9 +321,11 @@ DEPENDENCIES
rails (~> 5.2.0)
ransack
rolify
rspec-rails
rubocop
sass-rails (~> 5.0)
selenium-webdriver
shoulda-matchers
simple_form
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/scholars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :scholar, class: Scholar do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
end
end
10 changes: 10 additions & 0 deletions spec/models/scholar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rails_helper"

RSpec.describe Scholar, type: :model do
describe "#name" do
it "returns full name" do
scholar = build(:scholar, first_name: "Foo")
expect(scholar.name).to eq "Foo #{scholar.last_name}"
end
end
end
56 changes: 56 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'spec_helper'
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

# Setting locale to false to avoid 'InvalidLocale' error in tests
I18n.enforce_available_locales = false

RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods

# ## Mock Framework
config.mock_with :rspec

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

config.global_fixtures = :all

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end

# shoulda-matchers no longer installs itself into your test framework automatically
# since upgrade to version 3.0.0 -> https://github.com/thoughtbot/shoulda-matchers/issues/384
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
60 changes: 60 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed

# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end

# disable all monkey patching done by RSpec
# stops exposing DSL globally
# disables should and should_not syntax for rspec-expectations
# disables stub, should_receive, and should_not_receive syntax for rspec-mocks
config.disable_monkey_patching!

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect

# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
end
21 changes: 21 additions & 0 deletions spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
DatabaseCleaner.strategy = :transaction
end

config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation, {except: keep_tables}
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end

0 comments on commit 3322dd9

Please sign in to comment.