From 7023600cd1b2d786c64f26df61d3a7330ad7820c Mon Sep 17 00:00:00 2001 From: sto Date: Thu, 27 Mar 2025 09:27:25 +0100 Subject: [PATCH] Setup Faker and Factorybot --- Gemfile | 2 ++ Gemfile.lock | 9 +++++++++ app/controllers/sessions_controller.rb | 1 + app/views/sessions/new.html.erb | 11 ----------- app/views/sessions/new.html.slim | 13 +++++++++++++ spec/factories/users.rb | 7 +++++++ spec/features/contest_spec.rb | 20 ++++++++++++++++++++ spec/rails_helper.rb | 2 +- spec/support/factory_bot.rb | 5 +++++ 9 files changed, 58 insertions(+), 12 deletions(-) delete mode 100644 app/views/sessions/new.html.erb create mode 100644 app/views/sessions/new.html.slim create mode 100644 spec/factories/users.rb create mode 100644 spec/features/contest_spec.rb create mode 100644 spec/support/factory_bot.rb diff --git a/Gemfile b/Gemfile index 679e335..e83784d 100644 --- a/Gemfile +++ b/Gemfile @@ -56,6 +56,8 @@ group :development, :test do gem "rubocop-rails-omakase", require: false gem "rspec-rails" + gem "factory_bot_rails" + gem "faker" end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 680ec06..3b6adf0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,13 @@ GEM et-orbi (1.2.11) tzinfo execjs (2.10.0) + factory_bot (6.5.1) + activesupport (>= 6.1.0) + factory_bot_rails (6.4.4) + factory_bot (~> 6.5) + railties (>= 5.0.0) + faker (3.5.1) + i18n (>= 1.8.11, < 2) friendly_id (5.5.1) activerecord (>= 4.0.0) fugit (1.11.1) @@ -423,6 +430,8 @@ DEPENDENCIES capybara dartsass-rails debug + factory_bot_rails + faker friendly_id (~> 5.5.0) importmap-rails jbuilder diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 95b8859..d6d306c 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,6 +4,7 @@ class SessionsController < ApplicationController before_action :skip_authorization def new + @title = "Login" end def create diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb deleted file mode 100644 index ff641c4..0000000 --- a/app/views/sessions/new.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -<%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %> -<%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %> - -<%= form_with url: session_path do |form| %> - <%= form.email_field :email_address, required: true, autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email_address] %>
- <%= form.password_field :password, required: true, autocomplete: "current-password", placeholder: "Enter your password", maxlength: 72 %>
- <%= form.submit "Sign in" %> -<% end %> -
- -<%= link_to "Forgot password?", new_password_path %> diff --git a/app/views/sessions/new.html.slim b/app/views/sessions/new.html.slim new file mode 100644 index 0000000..4eabada --- /dev/null +++ b/app/views/sessions/new.html.slim @@ -0,0 +1,13 @@ += form_with url: session_path do |form| + .row.mb-3 + .col + .form-floating + = form.email_field :email_address, autocomplete: "username", required: true, autofocus: true, class: "form-control" + = form.label :email_address, class: "required" + .row.mb-3 + .col + .form-floating + = form.password_field :password, autocomplete: "current-password", required: true, autofocus: true, class: "form-control", maxlength: 72 + = form.label :password, class: "required" + + = form.submit "Sign in" \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..11b8127 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :user do + username { Faker::Internet.username } + email_address { Faker::Internet.email } + password { Faker::Internet.password(min_length: 12, max_length: 18) } + end +end diff --git a/spec/features/contest_spec.rb b/spec/features/contest_spec.rb new file mode 100644 index 0000000..27fa079 --- /dev/null +++ b/spec/features/contest_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.feature "Contests", type: :feature do + context "visiting the home page" do + let!(:user) { create(:user) } + + before do + visit '/' + fill_in "Email address", with: user.email_address + fill_in "Password", with: user.password + click_button "Sign in" + end + + it "should display the username" do + visit root_path + + expect(page).to have_content(user.username) + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index cb2ba55..6221e85 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -23,7 +23,7 @@ require 'rspec/rails' # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f } +Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).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. diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 0000000..6f427ea --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,5 @@ +require "factory_bot_rails" + +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end