-
Notifications
You must be signed in to change notification settings - Fork 71
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
記事ごとの登録日時を持たない場合も例外が発生しないよう修正 #6941
Changes from all commits
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 |
---|---|---|
|
@@ -32,29 +32,84 @@ class ExternalEntryTest < ActiveSupport::TestCase | |
rdf_item.expect(:link, 'https://test.com/index.rdf') | ||
rdf_item.expect(:description, 'article description') | ||
rdf_item.expect(:dc_date, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
rdf_item.expect(:dc_date, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
|
||
assert ExternalEntry.save_rdf_feed(user, rdf_item, nil) | ||
end | ||
|
||
test '.save_rdf_feed with no article publish date' do | ||
user = users(:hatsuno) | ||
rdf_item = Minitest::Mock.new | ||
rdf_item.expect(:link, 'https://test.com/index.rdf') | ||
rdf_item.expect(:title, 'test title') | ||
rdf_item.expect(:link, 'https://test.com/index.rdf') | ||
rdf_item.expect(:description, 'article description') | ||
rdf_item.expect(:dc_date, nil) | ||
|
||
assert ExternalEntry.save_rdf_feed(user, rdf_item, nil) | ||
end | ||
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. 記事の公開日が存在しない場合に正しく動作するかを検証するためのtestとは思うのですが、すいません、私が初学者ということもあり、
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. メソッド内で引数で渡したオブジェクトのインスタンス変数を参照したり、インスタンスメソッドを実行したりする際に、想定通りの値を返すようにすることがMockを使用している目的です。 def save_rdf_feed(rdf_item)
return if ExternalEntry.find_by(url: rdf_item.link) # rdf_item.link → 'https://test.com/index.rdf'が返される
ExternalEntry.create(
title: rdf_item.title,
url: rdf_item.link
)
end また、mock.expectの内容はメソッド内で呼び出す順番に設定する必要があり、上記のメソッドの場合は mock = Minitest::Mock.new
mock.expected(:link, 'https://test.com/index.rdf')
mock.expected(:title, 'test title')
mock.expected(:link, 'https://test.com/index.rdf') # もう一度設定しないとエラーになる 別のところに出てくると思うのですが、値を取得する際に mock = Minitest::Mock.new
mock.expected(:link, mock) # mock自身を返すようにする
mock.expected(:href, 'https://test.com/index.rdf') 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. 解説ありがとうございます🙏 |
||
|
||
test '.save_rdf_feed no exception even if all rdf elements are nil' do | ||
user = users(:hatsuno) | ||
rdf_item = Minitest::Mock.new | ||
rdf_item.expect(:link, nil) | ||
rdf_item.expect(:title, nil) | ||
rdf_item.expect(:link, nil) | ||
rdf_item.expect(:description, nil) | ||
rdf_item.expect(:dc_date, nil) | ||
|
||
assert ExternalEntry.save_rdf_feed(user, rdf_item) | ||
assert ExternalEntry.save_rdf_feed(user, rdf_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc) | ||
end | ||
|
||
test '.save_rss_feed' do | ||
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. test名の最初に 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. @sochi419 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. なるほど、クラスメソッドに対するテストには
PR上ではまだ修正されていないので、これから修正する感じですかね?よろしくお願いします🙏 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. すみませんが、メソッド名を修正しましたので、念のためこちらだけ確認をお願いできればと思います。 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. 修正ありがとうございます! |
||
user = users(:hatsuno) | ||
rss_item = Minitest::Mock.new | ||
rss_item.expect(:link, 'https://test.com/rss') | ||
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
rss_item.expect(:title, 'test title') | ||
rss_item.expect(:link, 'https://test.com/rss') | ||
rss_item.expect(:description, 'article description') | ||
rss_item.expect(:enclosure, rss_item) | ||
rss_item.expect(:url, 'https://test.com/image.png') | ||
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
|
||
assert ExternalEntry.save_rss_feed(user, rss_item) | ||
assert ExternalEntry.save_rss_feed(user, rss_item, nil) | ||
end | ||
|
||
test '.save_rss_feed with no article publish date' do | ||
user = users(:hatsuno) | ||
rss_item = Minitest::Mock.new | ||
rss_item.expect(:link, 'https://test.com/rss') | ||
rss_item.expect(:pubDate, nil) | ||
rss_item.expect(:title, 'test title') | ||
rss_item.expect(:link, 'https://test.com/rss') | ||
rss_item.expect(:description, 'article description') | ||
rss_item.expect(:enclosure, nil) | ||
|
||
assert ExternalEntry.save_rss_feed(user, rss_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc) | ||
end | ||
|
||
test '.save_rss_feed no exception even if all rss elements are nil' do | ||
user = users(:hatsuno) | ||
rss_item = Minitest::Mock.new | ||
rss_item.expect(:link, nil) | ||
rss_item.expect(:pubDate, nil) | ||
rss_item.expect(:title, nil) | ||
rss_item.expect(:link, nil) | ||
rss_item.expect(:description, nil) | ||
rss_item.expect(:enclosure, nil) | ||
|
||
assert ExternalEntry.save_rss_feed(user, rss_item, nil) | ||
end | ||
|
||
test '.save_atom_feed' do | ||
user = users(:hatsuno) | ||
atom_item = Minitest::Mock.new | ||
atom_item.expect(:link, atom_item) | ||
atom_item.expect(:href, 'https://test.com/feed') | ||
atom_item.expect(:published, atom_item) | ||
atom_item.expect(:published, atom_item) | ||
atom_item.expect(:content, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
atom_item.expect(:title, atom_item) | ||
atom_item.expect(:content, 'test title') | ||
atom_item.expect(:link, atom_item) | ||
|
@@ -65,10 +120,44 @@ class ExternalEntryTest < ActiveSupport::TestCase | |
atom_item.expect(:find, atom_item) | ||
atom_item.expect(:nil?, atom_item) | ||
atom_item.expect(:href, 'https://test.com/image.png') | ||
atom_item.expect(:published, atom_item) | ||
atom_item.expect(:content, Time.zone.local(2022, 1, 1, 0, 0, 0)) | ||
|
||
assert ExternalEntry.save_atom_feed(user, atom_item) | ||
assert ExternalEntry.save_atom_feed(user, atom_item, nil) | ||
end | ||
|
||
test '.save_atom_feed with no article publish date' do | ||
user = users(:hatsuno) | ||
atom_item = Minitest::Mock.new | ||
atom_item.expect(:link, atom_item) | ||
atom_item.expect(:href, 'https://test.com/feed') | ||
atom_item.expect(:published, nil) | ||
atom_item.expect(:updated, nil) | ||
atom_item.expect(:title, atom_item) | ||
atom_item.expect(:content, 'test title') | ||
atom_item.expect(:link, atom_item) | ||
atom_item.expect(:href, 'https://test.com/feed') | ||
atom_item.expect(:content, atom_item) | ||
atom_item.expect(:content, 'article description') | ||
atom_item.expect(:links, atom_item) | ||
atom_item.expect(:find, atom_item) | ||
atom_item.expect(:nil?, atom_item) | ||
atom_item.expect(:href, 'https://test.com/image.png') | ||
|
||
assert ExternalEntry.save_atom_feed(user, atom_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc) | ||
end | ||
|
||
test '.save_atom_feed no exception even if all atom elements are nil' do | ||
user = users(:hatsuno) | ||
atom_item = Minitest::Mock.new | ||
atom_item.expect(:link, nil) | ||
atom_item.expect(:published, nil) | ||
atom_item.expect(:updated, nil) | ||
atom_item.expect(:title, nil) | ||
atom_item.expect(:content, nil) | ||
atom_item.expect(:link, nil) | ||
atom_item.expect(:content, nil) | ||
atom_item.expect(:links, nil) | ||
|
||
assert ExternalEntry.save_atom_feed(user, atom_item, nil) | ||
end | ||
|
||
test '.fetch_and_save_rss_feeds' do | ||
|
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.
&.
を使った記述がありますが、これはどういった役割なのでしょうか?(初めて見ました)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.
レシーバーがnilのとき(今回であれば
feed.updated
の戻り値がnilのとき)、メソッドを呼び出してもNoMethodErrorが発生しなくなります。RSSやAtomの仕様上必須でない項目については、フィードに含まれていないことがあります。今回の場合はエラーを出すよりもスキップしたい(nilを返してほしい)ので、こちらの書き方にしています。
https://docs.ruby-lang.org/ja/latest/doc/news=2f2_3_0.html
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.
なるほどですね、ぼっち演算子というものなんですね~
レシーバがnilの時にエラーが出ないようにするためのものなんですね👀
ご教示ありがとうございます🙏