LoginSignup
0
0

More than 3 years have passed since last update.

Rspec問題2

Last updated at Posted at 2020-12-08

事前準備

ターミナル上で下記コマンドを実行してcloneする。

git clone https://github.com/Shu-Hos/pictweet_error.git
cd pictweet_error
bundle install
yarn install
rails db:create
rails db:migrate

問題1

ターミナルで下記コマンドを実行してください。

bundle exec rspec spec/models/user_spec.rb

するとNoMethodError:
undefined method name=' for #<User:xxxxxxx>’
のエラーが発生するかと思います。
まずはこちらをヒントなしで解決しましょう。

問題2

上記のエラーが改善されて再度こちらのコマンドを実行してください

bundle exec rspec spec/models/user_spec.rb

しかしテストの結果は、Failure/Error:expect(user.errors[:nickname]).to include("is too long (maximum is 6 characters)")が残ってしまっているかと思います。
こちらを修正してuserのモデル単体テストを成功させてください。

ヒント

binding.pryでuserの中身を確認すると条件分岐の結果がtrueになっているかつエラーメッセージが空になっているかと思います。
バリデーションが怪しいかも?

問題3

問題2まで解き終えたら次はtweetのmodel単体テストです。
以下のコマンドを実行してください。

bundle exec rspec spec/models/tweet_spec.rb

すると正常系のテストコードがすべてfailuresになってしまいます。
こちらの修正をお願いします。

ヒント

binding.pryで@tweetの中身を確認してみましょう。

問題4

次はtweets_controllerの単体テストです。
以下のコマンドを実行してください。

bundle exec rspec spec/requests/tweets_spec.rb

すると、ActionController::UrlGenerationErrorが発生するかと思います。
エラー文をよく読んでこちらの問題を解決しましょう。

ヒント

エラー文はPNo route matches {:action=>"show", :controller=>"tweets"}, missing required keys: [:id]と出ているはずです。必要に応じてrails routesコマンドでshowアクションのパスを確認してみましょう。

ここからはtweet機能の結合テスト問題です。

問題5

以下のコマンドを実行してください。

bundle exec rspec spec/system/tweets_spec.rb

ログインページでログインができずにCapybara::ElementNotFound:Unable to find field "email" that is not disabledというエラーが発生するかと思います。
まずこちらを解決しましょう。

ヒント

ElementNotFoundと出ているので検証ツールで要素の中身を確認してみましょう。

問題6

こちらで最後の問題になります!
再度以下のコマンドを実行してください。

bundle exec rspec spec/system/tweets_spec.rb

以下のエラーが起きたかと思います。
Failure/Error: expect(page).to_not have_no_selector 'form'expected to find css "form" but there were no matches

ヒント

テストの実行文とexpectedの実行結果の英文の意味を調べてみると良いかも知れないです。


お疲れさまです!!
何かご不明点とかあれば、今後ぜひ教えてください!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0