Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Milestone 5: controller specs #5

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri windows]

gem 'factory_bot_rails'

gem 'rails-controller-testing'

gem 'rspec-rails'

gem 'shoulda-matchers'
end

group :development do
Expand Down
28 changes: 21 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ GEM
ruby2_keywords
error_highlight (0.5.1)
erubi (1.12.0)
factory_bot (6.4.2)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.2)
factory_bot (~> 6.4)
railties (>= 5.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand All @@ -114,13 +119,13 @@ GEM
activesupport (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.6.0)
irb (1.9.1)
irb (1.10.1)
rdoc
reline (>= 0.3.8)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.3)
json (2.7.1)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
Expand All @@ -136,7 +141,7 @@ GEM
minitest (5.20.0)
msgpack (1.7.2)
mutex_m (0.2.0)
net-imap (0.4.6)
net-imap (0.4.7)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -145,7 +150,7 @@ GEM
timeout
net-smtp (0.4.0)
net-protocol
nio4r (2.6.1)
nio4r (2.7.0)
nokogiri (1.15.5-x86_64-linux)
racc (~> 1.4)
parallel (1.23.0)
Expand Down Expand Up @@ -181,6 +186,10 @@ GEM
activesupport (= 7.1.2)
bundler (>= 1.15.0)
railties (= 7.1.2)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand All @@ -198,9 +207,9 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.0)
rdoc (6.6.1)
psych (>= 4.0.0)
regexp_parser (2.8.2)
regexp_parser (2.8.3)
reline (0.4.1)
io-console (~> 0.5)
rexml (3.2.6)
Expand Down Expand Up @@ -242,6 +251,8 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
shoulda-matchers (5.3.0)
activesupport (>= 5.2.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -283,22 +294,25 @@ DEPENDENCIES
capybara
debug
error_highlight (>= 0.4.0)
factory_bot_rails
importmap-rails
jbuilder
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.2)
rails-controller-testing
rspec-rails
rubocop (~> 1.56.1)
selenium-webdriver
shoulda-matchers
sprockets-rails
stimulus-rails
turbo-rails
tzinfo-data
web-console

RUBY VERSION
ruby 3.1.3
ruby 3.1.3p185

BUNDLED WITH
2.4.22
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ To execute the tests, run the following command inside the project folder:
- [x] Processing data in models.
- [x] Validations and Model specs.
- [x] Controllers.
- [ ] Controllers specs.
- [x] Controllers specs.
- [ ] Views.
- [ ] Forms.
- [ ] Integration specs for Views and fixing n+1 problems.
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/comments_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :comment do
text { 'This is a sample comment' }
association :post, factory: :post
association :author, factory: :user
end
end
12 changes: 12 additions & 0 deletions spec/factories/likes_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FactoryBot.define do
factory :like do
association :post
association :user

# ? Build associated objects automatically
after(:build) do |like|
like.post.save!
like.user.save!
end
end
end
7 changes: 7 additions & 0 deletions spec/factories/posts_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :post do
title { 'Sample Title' }
text { 'This is sample text' }
association :author, factory: :user
end
end
8 changes: 8 additions & 0 deletions spec/factories/users_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
name { 'John Doe' }
photo { 'https://example.com/photo.jpg' }
bio { 'A bio for John Doe.' }
posts_counter { 0 }
end
end
79 changes: 79 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require 'factory_bot_rails'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# 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

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
# ... (todo lo demΓ‘s sigue igual)

# Shoulda Matchers
Shoulda::Matchers.configure do |shoulda_config|
shoulda_config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

# FactoryBot
RSpec.configure do |rspec_config|
rspec_config.include FactoryBot::Syntax::Methods
end
end
26 changes: 26 additions & 0 deletions spec/requests/posts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe 'PostsController', type: :request do
describe 'GET #index' do
it 'renders the index template' do
user = create(:user)
get "/users/#{user.id}/posts"

expect(response).to have_http_status(200)
expect(response).to render_template(:index)
expect(response.body).to include('posts/index')
end
end

describe 'GET #show' do
it 'renders the show template' do
user = create(:user)
post = create(:post, author: user)
get "/users/#{user.id}/posts/#{post.id}"

expect(response).to have_http_status(200)
expect(response).to render_template(:show)
expect(response.body).to include('posts/show')
end
end
end
24 changes: 24 additions & 0 deletions spec/requests/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

RSpec.describe 'UsersController', type: :request do
describe 'GET #index' do
it 'renders the index template' do
get '/users'

expect(response).to have_http_status(200)
expect(response).to render_template(:index)
expect(response.body).to include('users/index')
end
end

describe 'GET #show' do
it 'renders the show template' do
user = create(:user)
get "/users/#{user.id}"

expect(response).to have_http_status(200)
expect(response).to render_template(:show)
expect(response.body).to include('users/show')
end
end
end
7 changes: 1 addition & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative '../config/environment' # ! important to run rails environment first
# This file was generated by the `rspec --init` command. Conventionally, all
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
Expand Down Expand Up @@ -64,10 +63,6 @@
# # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
# config.disable_monkey_patching!
#
# # This setting enables warnings. It's recommended, but in some cases may
# # be too noisy due to issues in dependencies.
# config.warnings = 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.
Expand Down
Loading