Skip to content

Commit 515b165

Browse files
committed
feat(tests): add rspec and tests for contacts
1 parent 3d1e5dc commit 515b165

File tree

8 files changed

+221
-1
lines changed

8 files changed

+221
-1
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--require spec_helper
2+
--format documentation

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,7 @@ group :development, :test do
6565

6666
# Rails >= 3 pry initializer
6767
gem "pry-rails"
68+
69+
# RSpec for Rails 7+
70+
gem "rspec-rails"
6871
end

Gemfile.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ GEM
110110
bcrypt (~> 3.0)
111111
devise (> 3.5.2, < 5)
112112
rails (>= 4.2.0, < 8.1)
113+
diff-lcs (1.5.1)
113114
dotenv (3.1.7)
114115
drb (2.2.1)
115116
ed25519 (1.3.0)
@@ -262,6 +263,23 @@ GEM
262263
responders (3.1.1)
263264
actionpack (>= 5.2)
264265
railties (>= 5.2)
266+
rspec-core (3.13.3)
267+
rspec-support (~> 3.13.0)
268+
rspec-expectations (3.13.3)
269+
diff-lcs (>= 1.2.0, < 2.0)
270+
rspec-support (~> 3.13.0)
271+
rspec-mocks (3.13.2)
272+
diff-lcs (>= 1.2.0, < 2.0)
273+
rspec-support (~> 3.13.0)
274+
rspec-rails (7.1.1)
275+
actionpack (>= 7.0)
276+
activesupport (>= 7.0)
277+
railties (>= 7.0)
278+
rspec-core (~> 3.13)
279+
rspec-expectations (~> 3.13)
280+
rspec-mocks (~> 3.13)
281+
rspec-support (~> 3.13)
282+
rspec-support (3.13.2)
265283
rubocop (1.71.0)
266284
json (~> 2.3)
267285
language_server-protocol (>= 3.17.0)
@@ -371,6 +389,7 @@ DEPENDENCIES
371389
puma (>= 5.0)
372390
rack-cors
373391
rails (~> 8.0.1)
392+
rspec-rails
374393
rubocop-rails-omakase
375394
solid_cable
376395
solid_cache

app/models/contact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Contact < ApplicationRecord
22
validates_presence_of :kind
3-
validates_presence_of :address
3+
# validates_presence_of :address
44

55
belongs_to :kind# , optional: true
66
has_many :phones

config/environments/test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
Rails.application.configure do
77
# Settings specified here will take precedence over those in config/application.rb.
88

9+
Rails.application.routes.default_url_options = {
10+
host: "localhost",
11+
port: 3000
12+
}
913
# While tests run files are not watched, reloading is not necessary.
1014
config.enable_reloading = false
1115

spec/controllers/v1/contacts_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "rails_helper"
2+
3+
describe V1::ContactsController, type: :controller do
4+
describe "GET #index - GET v1/contacts/" do
5+
it "should return status code 200 OK, when header accept json is provided" do
6+
request.accept = "application/vnd.api+json"
7+
get :index
8+
# expect(response.status).to eql(200)
9+
expect(response).to have_http_status(200)
10+
end
11+
12+
it "should return status code 406 Not Acceptable, when header accept json is not provided" do
13+
get :index
14+
expect(response).to have_http_status(406)
15+
end
16+
end
17+
18+
describe "GET #show - GET v1/contacts/:id" do
19+
it "should return correct contact data" do
20+
contact = Contact.first
21+
request.accept = "application/vnd.api+json"
22+
get :show, params: { id: contact.id }
23+
response_body = JSON.parse(response.body) # returns a hash (contact info nested in "data")
24+
expect(response_body.fetch("data").fetch("id")).to eq(contact.id.to_s)
25+
expect(response_body.fetch("data").fetch("type")).to eq("contacts")
26+
end
27+
end
28+
end

spec/rails_helper.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_relative '../config/environment'
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
# Uncomment the line below in case you have `--require rails_helper` in the `.rspec` file
8+
# that will avoid rails generators crashing because migrations haven't been run yet
9+
# return unless Rails.env.test?
10+
require 'rspec/rails'
11+
# Add additional requires below this line. Rails is not loaded until this point!
12+
13+
# Requires supporting ruby files with custom matchers and macros, etc, in
14+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
15+
# run as spec files by default. This means that files in spec/support that end
16+
# in _spec.rb will both be required and run as specs, causing the specs to be
17+
# run twice. It is recommended that you do not name files matching this glob to
18+
# end with _spec.rb. You can configure this pattern with the --pattern
19+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
20+
#
21+
# The following line is provided for convenience purposes. It has the downside
22+
# of increasing the boot-up time by auto-requiring all files in the support
23+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
24+
# require only the support files necessary.
25+
#
26+
# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }
27+
28+
# Checks for pending migrations and applies them before tests are run.
29+
# If you are not using ActiveRecord, you can remove these lines.
30+
begin
31+
ActiveRecord::Migration.maintain_test_schema!
32+
rescue ActiveRecord::PendingMigrationError => e
33+
abort e.to_s.strip
34+
end
35+
RSpec.configure do |config|
36+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
37+
config.fixture_paths = [
38+
Rails.root.join('spec/fixtures')
39+
]
40+
41+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
42+
# examples within a transaction, remove the following line or assign false
43+
# instead of true.
44+
config.use_transactional_fixtures = true
45+
46+
# You can uncomment this line to turn off ActiveRecord support entirely.
47+
# config.use_active_record = false
48+
49+
# RSpec Rails uses metadata to mix in different behaviours to your tests,
50+
# for example enabling you to call `get` and `post` in request specs. e.g.:
51+
#
52+
# RSpec.describe UsersController, type: :request do
53+
# # ...
54+
# end
55+
#
56+
# The different available types are documented in the features, such as in
57+
# https://rspec.info/features/7-1/rspec-rails
58+
#
59+
# You can also this infer these behaviours automatically by location, e.g.
60+
# /spec/models would pull in the same behaviour as `type: :model` but this
61+
# behaviour is considered legacy and will be removed in a future version.
62+
#
63+
# To enable this behaviour uncomment the line below.
64+
# config.infer_spec_type_from_file_location!
65+
66+
# Filter lines from Rails gems in backtraces.
67+
config.filter_rails_from_backtrace!
68+
# arbitrary gems may also be filtered via:
69+
# config.filter_gems_from_backtrace("gem name")
70+
end

spec/spec_helper.rb

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

0 commit comments

Comments
 (0)