Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@MasamiKumazawa

Rails5.0.1で非同期(Ajax)のテストの書き方が変更されたようなので、メモ。

More than 1 year has passed since last update.

Railsで非同期処理のテストをしようとしたところ、google先生を頼りに

railsuRuby
xhr :patch, race_path(@race), race: { nickname: new_name }

と書いたら、

DEPRECATION WARNING: `xhr` and `xml_http_request` are deprecated and will be removed in Rails 5.1.
Switch to e.g. `post comments_path, params: { comment: { body: 'Honey bunny' } }, xhr: true`.

とか怒られたので、言われるがままに

patch race_path(@race), params: { race: { nickname: new_name } }:xhr => true

としてみたところ、テストが通るようになった。

でも、これでは黒魔術っぽいので、

patch( race_path(@race), {'xhr' => true, 'params' => { 'race' => { 'nickname' => new_name } } } )

とか書くと余計解り辛い?
patchメソッドに渡す引数は二つ、URLとHashの二つ、URLはrace_path(@race) => /race/ で、Hashはhash => {:xhr,:params}から構成されていている。
Hashの最初の項目、xhrのtrueは一目瞭然。

次のparamsと言うHashはraceと言うHashを持っていって、raceというHashはnickname: new_nameという構成になってすよと。

解り辛ぇ(^^;

xhrを最初に持ってきたら、なんか読みやすくなった気はするれど、Hashの階層深くね?

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?