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

Add rspec #148

Merged
merged 2 commits into from
Sep 22, 2024
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "rspec-rails"
gem 'factory_bot_rails'
gem "capybara"
gem "selenium-webdriver"
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ GEM
drb (2.2.1)
erubi (1.13.0)
excon (0.110.0)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faker (3.4.1)
i18n (>= 1.8.11, < 2)
faraday (2.9.0)
Expand Down Expand Up @@ -460,6 +465,7 @@ DEPENDENCIES
debug
dotenv-rails
draper
factory_bot_rails
faker
fog-aws
font-awesome-rails
Expand Down
16 changes: 16 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FactoryBot.define do
factory :user do
name { Faker::Name.unique.name }
email { Faker::Internet.unique.email }
password { "password" }
password_confirmation { 'password' }
end

trait :general do
role { :general }
end

trait :admin do
role { :admin }
end
end
31 changes: 31 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe User, type: :model do
it 'ニックネーム、メールがあり、パスワードは3文字以上であれば有効であること' do
user = build(:user)
expect(user).to be_valid
end

it 'メールはユニークであること' do
user1 = create(:user)
user2 = build(:user)
user2.email = user1.email
user2.valid?
expect(user2.errors[:email]).to include('メールアドレスはすでに使用されています')
end

it 'ニックネーム、メールアドレスが必須項目であること' do
user = build(:user)
user.name = nil
user.email = nil
user.valid?
expect(user.errors[:name]).to include('を入力してください')
expect(user.errors[:email]).to include('を入力してください')
end


it 'ニックネームは255文字以内であること' do
user = build(:user)
user.name = 'a' * 256
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Rails.root.join('spec/fixtures')
]

config.include FactoryBot::Syntax::Methods#Factorybotの初期設定
# 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.
Expand Down
Loading