-
-
Notifications
You must be signed in to change notification settings - Fork 109
News specの強化(追加ケースの実装) #1719
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
base: main
Are you sure you want to change the base?
News specの強化(追加ケースの実装) #1719
Changes from all commits
7e29ea1
b0f4365
052cfe2
b35ad64
9951bae
1850e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,67 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe News, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
describe 'バリデーション' do | ||
let(:news) { build(:news) } | ||
|
||
it '有効なファクトリーを持つ' do | ||
expect(news).to be_valid | ||
end | ||
|
||
describe 'title' do | ||
it 'タイトルが空の場合は無効になる' do | ||
news.title = nil | ||
expect(news).not_to be_valid | ||
expect(news.errors[:title]).not_to be_empty | ||
end | ||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有効な場合も検証すると良さそうです! |
||
end | ||
|
||
describe 'url' do | ||
it 'URL が空の場合は無効になる' do | ||
news.url = nil | ||
expect(news).not_to be_valid | ||
expect(news.errors[:url]).not_to be_empty | ||
end | ||
|
||
it 'URL が重複している場合は無効になる' do | ||
create(:news, url: 'https://example.com/test') | ||
duplicate_news = build(:news, url: 'https://example.com/test') | ||
expect(duplicate_news).not_to be_valid | ||
expect(duplicate_news.errors[:url]).not_to be_empty | ||
end | ||
|
||
it 'URL形式であること' do | ||
news.url = 'invalid-url' | ||
expect(news).not_to be_valid | ||
expect(news.errors[:url]).not_to be_empty | ||
end | ||
|
||
it 'HTTPSとHTTPを許可する' do | ||
news.url = 'https://example.com' | ||
expect(news).to be_valid | ||
|
||
news.url = 'http://example.com' | ||
expect(news).to be_valid | ||
end | ||
end | ||
Comment on lines
+19
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 数が多いので、無効な場合と有効な場合で分けてあげるとわかりやすいかなと思いました💭
また、it の説明は「どうなるか」を書いてあげると良さそうです # 有効な場合の検証に読めてしまう
- it 'URL形式であること' do
# it の説明は「~なる」「〜する」など動詞がわかりやすいと教えてもらいました📝
+ it 'URL形式でない場合は無効になる' do |
||
|
||
describe 'published_at' do | ||
it '公開日時が空の場合は無効になる' do | ||
news.published_at = nil | ||
expect(news).not_to be_valid | ||
expect(news.errors[:published_at]).not_to be_empty | ||
end | ||
end | ||
Comment on lines
+48
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここも有効な場合も検証すると良さそうです🛠️ |
||
end | ||
|
||
describe 'スコープ' do | ||
describe '.recent' do | ||
it '公開日時の降順で並び替える' do | ||
old_news = create(:news, published_at: 2.days.ago) | ||
new_news = create(:news, published_at: 1.day.ago) | ||
|
||
expect(News.recent).to eq([new_news, old_news]) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有効なnewsを生成できるファクトリーがあるか?になっていると思うので、ここでは不要そうです👀
(newsモデルのバリデーションが正しく動作しているかは、以下で個別に検証しているので問題なさそうです。)