質問編集履歴
4
試したことの削除
title
CHANGED
File without changes
|
body
CHANGED
@@ -137,48 +137,4 @@
|
|
137
137
|
post destroy_like_path(@micropost), xhr: true
|
138
138
|
assert_match 'like-btn', response.body
|
139
139
|
end
|
140
|
-
```
|
141
|
-
|
142
|
-
② Capybaraの設定を変更 => 同様のエラーが発生
|
143
|
-
test/test_helper.rb
|
144
|
-
```
|
145
|
-
ENV['RAILS_ENV'] ||= 'test'
|
146
|
-
require_relative "../config/environment"
|
147
|
-
require "rails/test_help"
|
148
|
-
require "minitest/reporters"
|
149
|
-
require "capybara/rails" # 追記
|
150
|
-
Minitest::Reporters.use!
|
151
|
-
|
152
|
-
# (中略)
|
153
|
-
|
154
|
-
class ActionDispatch::IntegrationTest
|
155
|
-
include Capybara::DSL # 追記
|
156
|
-
|
157
|
-
def log_in_as(user, password: 'password', remember_me: '1')
|
158
|
-
post login_path, params: { session: { email: user.email,
|
159
|
-
password: password,
|
160
|
-
remember_me: remember_me } }
|
161
|
-
end
|
162
|
-
end
|
163
|
-
```
|
164
|
-
test/integration/likes_interface_test.rb
|
165
|
-
```
|
166
|
-
require 'test_helper'
|
167
|
-
|
168
|
-
class LikesInterfaceTest < ActionDispatch::IntegrationTest
|
169
|
-
|
170
|
-
#(中略)
|
171
|
-
|
172
|
-
# likeボタンの描画の切り替わり(ajaxリクエスト)
|
173
|
-
test "like-btn should change with ajax when pusshed " do
|
174
|
-
Capybara.current_driver = :poltergeist # 追記
|
175
|
-
assert_select 'i.like-btn'
|
176
|
-
post create_like_path(@micropost), xhr: true
|
177
|
-
sleep 1 # 追記
|
178
|
-
assert_select 'i.unlike-btn'
|
179
|
-
post destroy_like_path(@micropost), xhr: true
|
180
|
-
sleep 1 # 追記
|
181
|
-
assert_select 'i.like-btn'
|
182
|
-
Capybara.use_default_driver # 追記
|
183
|
-
end
|
184
140
|
```
|
3
試したこと②を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -125,7 +125,7 @@
|
|
125
125
|
```
|
126
126
|
|
127
127
|
### 試したこと
|
128
|
-
`assert_match`を用いて`respose.body`の中から調べる形で書くとテストが通る。
|
128
|
+
① `assert_match`を用いて`respose.body`の中から調べる形で書くとテストが通る。
|
129
129
|
|
130
130
|
|
131
131
|
```
|
@@ -137,4 +137,48 @@
|
|
137
137
|
post destroy_like_path(@micropost), xhr: true
|
138
138
|
assert_match 'like-btn', response.body
|
139
139
|
end
|
140
|
+
```
|
141
|
+
|
142
|
+
② Capybaraの設定を変更 => 同様のエラーが発生
|
143
|
+
test/test_helper.rb
|
144
|
+
```
|
145
|
+
ENV['RAILS_ENV'] ||= 'test'
|
146
|
+
require_relative "../config/environment"
|
147
|
+
require "rails/test_help"
|
148
|
+
require "minitest/reporters"
|
149
|
+
require "capybara/rails" # 追記
|
150
|
+
Minitest::Reporters.use!
|
151
|
+
|
152
|
+
# (中略)
|
153
|
+
|
154
|
+
class ActionDispatch::IntegrationTest
|
155
|
+
include Capybara::DSL # 追記
|
156
|
+
|
157
|
+
def log_in_as(user, password: 'password', remember_me: '1')
|
158
|
+
post login_path, params: { session: { email: user.email,
|
159
|
+
password: password,
|
160
|
+
remember_me: remember_me } }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
```
|
164
|
+
test/integration/likes_interface_test.rb
|
165
|
+
```
|
166
|
+
require 'test_helper'
|
167
|
+
|
168
|
+
class LikesInterfaceTest < ActionDispatch::IntegrationTest
|
169
|
+
|
170
|
+
#(中略)
|
171
|
+
|
172
|
+
# likeボタンの描画の切り替わり(ajaxリクエスト)
|
173
|
+
test "like-btn should change with ajax when pusshed " do
|
174
|
+
Capybara.current_driver = :poltergeist # 追記
|
175
|
+
assert_select 'i.like-btn'
|
176
|
+
post create_like_path(@micropost), xhr: true
|
177
|
+
sleep 1 # 追記
|
178
|
+
assert_select 'i.unlike-btn'
|
179
|
+
post destroy_like_path(@micropost), xhr: true
|
180
|
+
sleep 1 # 追記
|
181
|
+
assert_select 'i.like-btn'
|
182
|
+
Capybara.use_default_driver # 追記
|
183
|
+
end
|
140
184
|
```
|
2
質問内容とタイトルを詳細化
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
[minitest]assert_selectで指定した要素がajaxだと見つけられない
|
body
CHANGED
@@ -1,17 +1,84 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
Railsでtwitterのようなアプリを作成しています。
|
3
|
-
ユーザーの投稿に「いいね」を
|
3
|
+
ユーザーの投稿に「いいね」ボタンを実装し、ボタンを押すとajaxで`js.erb`ファイルを読み込むことでボタン(赤いハート、灰色のハート)が非同期で切り替わるように実装しています。
|
4
4
|
|
5
|
-
テ
|
5
|
+
このテンプレートが切り替わったかどうかを検証するテストを書こうと思い、
|
6
|
+
`assert_select`を用いてボタン部分の要素が存在するか確認したところ、
|
7
|
+
(1)非同期通信 => 該当する要素を見つけられずテストが失敗
|
8
|
+
(2)同期通信 => テスト成功
|
9
|
+
という結果になりました。
|
6
10
|
|
7
|
-
|
11
|
+
そこで、代わりに`assert_match`を用いると、非同期通信でもテストが通るようになりました。
|
8
12
|
|
13
|
+
なぜ`assert_select`だと、非同期通信において指定した要素を検知できなかったのでしょうか。
|
14
|
+
|
15
|
+
|
16
|
+
### 該当のソースコード
|
17
|
+
いいねボタンのテンプレート(app/views/likes/_like_form.html.erb)
|
9
18
|
```
|
19
|
+
<% if logged_in? %>
|
20
|
+
<%# いいね済みの場合 %>
|
21
|
+
<% if current_user.liked_micropost?(micropost.id) %>
|
22
|
+
<td>
|
23
|
+
<%= link_to destroy_like_path(micropost), method: :POST, remote: true do %>
|
10
|
-
|
24
|
+
<i class="fa fa-heart unlike-btn"></i>
|
25
|
+
<% end %>
|
26
|
+
<%= micropost.likes.count %>
|
27
|
+
</td>
|
28
|
+
<%# いいねしてない場合 %>
|
11
|
-
|
29
|
+
<% else %>
|
30
|
+
<td>
|
31
|
+
<%= link_to create_like_path(micropost), method: :POST, remote: true do %>
|
32
|
+
<i class="fa fa-heart like-btn"></i>
|
33
|
+
<% end %>
|
34
|
+
<%= object.likes.count %>
|
35
|
+
</td>
|
36
|
+
<% end %>
|
37
|
+
<% end %>
|
12
38
|
```
|
13
39
|
|
40
|
+
app/controllers/likes_controller.rb
|
41
|
+
```
|
42
|
+
class LikesController < ApplicationController
|
43
|
+
before_action :micropost_params
|
44
|
+
|
45
|
+
def create
|
46
|
+
@like = Like.new(user_id: current_user.id, micropost_id: params[:micropost_id])
|
47
|
+
@like.save
|
48
|
+
respond_to do |format|
|
49
|
+
format.html { redirect_back(fallback_location: root_url) } # jsをオフにしている人向け
|
50
|
+
format.js # => /app/views/likes/create.js.erb
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroy
|
55
|
+
@like = Like.find_by(user_id: current_user.id, micropost_id: params[:micropost_id])
|
56
|
+
@like.destroy
|
57
|
+
respond_to do |format|
|
58
|
+
format.html { redirect_back(fallback_location: root_url) }
|
59
|
+
format.js # => /app/views/likes/create.js.erb
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
# いいねボタンを押した投稿情報を、インスタンス変数に保持してビューへ
|
66
|
+
def micropost_params
|
67
|
+
@micropost = Micropost.find(params[:micropost_id])
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
app/views/likes/create.js.erb
|
74
|
+
```
|
75
|
+
$('#like_<%= @micropost.id %>').html("<%= j(render partial: 'likes/like_form', locals: {micropost: @micropost}) %>");
|
76
|
+
```
|
77
|
+
app/views/likes/destroy.js.erb
|
78
|
+
```
|
79
|
+
$('#like_<%= @micropost.id %>').html("<%= j(render partial: 'likes/like_form', locals: {micropost: @micropost}) %>");
|
80
|
+
```
|
14
|
-
###
|
81
|
+
### テストコード
|
15
82
|
test/integration/likes_interface_test.rb
|
16
83
|
```ここに言語名を入力
|
17
84
|
require 'test_helper'
|
@@ -50,12 +117,19 @@
|
|
50
117
|
|
51
118
|
end
|
52
119
|
```
|
120
|
+
### 発生している問題・エラーメッセージ
|
53
121
|
|
122
|
+
```
|
123
|
+
Expected at least 1 element matching "i.unlike-btn", found 0..
|
124
|
+
Expected 0 to be >= 1.
|
125
|
+
```
|
126
|
+
|
54
127
|
### 試したこと
|
55
|
-
`assert_match`を用いて`respose.body`の中から調べる形で書くとテストが通る
|
128
|
+
`assert_match`を用いて`respose.body`の中から調べる形で書くとテストが通る。
|
56
129
|
|
130
|
+
|
57
131
|
```
|
58
|
-
#
|
132
|
+
# いいねボタンの描画の切り替わり(ajaxリクエスト)
|
59
133
|
test "like-btn should change with ajax when pusshed " do
|
60
134
|
assert_select 'i.like-btn'
|
61
135
|
post create_like_path(@micropost), xhr: true
|
1
「試したこと」を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -49,4 +49,18 @@
|
|
49
49
|
# 以下略
|
50
50
|
|
51
51
|
end
|
52
|
+
```
|
53
|
+
|
54
|
+
### 試したこと
|
55
|
+
`assert_match`を用いて`respose.body`の中から調べる形で書くとテストが通るようにはなりました。
|
56
|
+
|
57
|
+
```
|
58
|
+
# likeボタンの描画の切り替わり(ajaxリクエスト)
|
59
|
+
test "like-btn should change with ajax when pusshed " do
|
60
|
+
assert_select 'i.like-btn'
|
61
|
+
post create_like_path(@micropost), xhr: true
|
62
|
+
assert_match 'unlike-btn', response.body
|
63
|
+
post destroy_like_path(@micropost), xhr: true
|
64
|
+
assert_match 'like-btn', response.body
|
65
|
+
end
|
52
66
|
```
|