diff --git a/Gemfile b/Gemfile index ed73c00..e54c8f8 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 430a8c7..a580c08 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -283,14 +294,17 @@ 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 @@ -298,7 +312,7 @@ DEPENDENCIES web-console RUBY VERSION - ruby 3.1.3 + ruby 3.1.3p185 BUNDLED WITH 2.4.22 diff --git a/README.md b/README.md index a50df1a..7115be3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/spec/factories/comments_factory.rb b/spec/factories/comments_factory.rb new file mode 100644 index 0000000..ba8fef2 --- /dev/null +++ b/spec/factories/comments_factory.rb @@ -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 diff --git a/spec/factories/likes_factory.rb b/spec/factories/likes_factory.rb new file mode 100644 index 0000000..e9e3bb5 --- /dev/null +++ b/spec/factories/likes_factory.rb @@ -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 diff --git a/spec/factories/posts_factory.rb b/spec/factories/posts_factory.rb new file mode 100644 index 0000000..f6bb951 --- /dev/null +++ b/spec/factories/posts_factory.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :post do + title { 'Sample Title' } + text { 'This is sample text' } + association :author, factory: :user + end +end diff --git a/spec/factories/users_factory.rb b/spec/factories/users_factory.rb new file mode 100644 index 0000000..ce47589 --- /dev/null +++ b/spec/factories/users_factory.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..98dc673 --- /dev/null +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb new file mode 100644 index 0000000..f325b08 --- /dev/null +++ b/spec/requests/posts_controller_spec.rb @@ -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 diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb new file mode 100644 index 0000000..23284c0 --- /dev/null +++ b/spec/requests/users_controller_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 886eed8..9c96a9b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 @@ -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.