質問編集履歴

3

誤字修正

2022/03/22 00:22

投稿

senseIY
senseIY

スコア281

test CHANGED
@@ -1 +1 @@
1
- Rspecで音声ファイルの設定が出来ず、hashを渡すことができない
1
+ Rspecで音声ファイルの設定が出来ず、hashを渡すことができない。また、user can't be blankが解消できない
test CHANGED
@@ -8,21 +8,9 @@
8
8
  Failure/Error: gogaku.file = fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'Night_Sea.mp3'), 'audio/mpeg', :binary)
9
9
 
10
10
  NoMethodError:
11
- undefined method `fixture_file_upload' for #<FactoryBot::SyntaxRunner:0x00005618e00c2638>
12
- # ./spec/factories/dictum_users.rb:20:in `block (3 levels) in <main>'
13
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/callback.rb:12:in `instance_exec'
14
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/callback.rb:12:in `run'
15
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/callbacks_observer.rb:11:in `block in update'
16
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/callbacks_observer.rb:10:in `each'
17
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/callbacks_observer.rb:10:in `update'
18
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/evaluation.rb:24:in `notify'
19
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy/create.rb:10:in `block in result'
20
- # <internal:kernel>:90:in `tap'
11
+ ...
21
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy/create.rb:9:in `result'
12
+
22
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory.rb:43:in `run'
23
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory_runner.rb:29:in `block in run'
24
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/factory_runner.rb:28:in `run'
25
- # /usr/local/bundle/gems/factory_bot-6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method'
13
+ 6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method'
26
14
  # ./spec/models/dictum_user_spec.rb:115:in `block (5 levels) in <top (required)>'
27
15
 
28
16
  Finished in 0.12316 seconds (files took 1.32 seconds to load)
@@ -46,14 +34,16 @@
46
34
  password_confirmation {"foobar"}
47
35
  end
48
36
 
37
+ #/spec/factories/gogakus.rb
38
+
39
+ FactoryBot.define do
49
- factory :music, class: Gogaku do
40
+ factory :gogaku do
50
- subject {"English"}
41
+ subject {"English"}
51
- body {"in trip"}
42
+ body {"in trip"}
52
- after(:build) do |gogaku|
43
+ after(:build) do |gogaku|
53
- gogaku.file = fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'Night_Sea.mp3'), 'audio/mpeg', :binary)
44
+ gogaku.file = fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'Night_Sea.mp3'), 'audio/mp3', :binary)
54
- end
45
+ end
55
-
56
- answer {"none"}
46
+ answer {"none"}
57
47
  end
58
48
  end
59
49
 
@@ -131,17 +121,37 @@
131
121
 
132
122
  context "when user is deleted" do
133
123
  user = FactoryBot.build(:kobe)
134
- user.gogakus.build(FactoryBot.build(:music))
124
+ gogaku = FactoryBot.build(:gogaku)
125
+
135
126
  it "gogaku is deleted too" do
136
127
  user.save
128
+ gogaku.file = fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'Night_Sea.mp3'), 'audio/mpeg')
129
+ pending
130
+
131
+ gogaku.save
132
+ user.gogakus.create(gogaku)
137
133
  expect(Gogaku.count).to eq 1
138
134
  user.destroy
139
135
  expect(Gogaku.count).to eq 0
140
136
  end
141
- end
142
- end
143
137
  end
144
138
  end
139
+
140
+ #/spec/models/gogaku_spec.rb
141
+
142
+ require 'rails_helper'
143
+
144
+ RSpec.describe Gogaku, type: :model do
145
+ describe "test for gogaku" do
146
+ dictum_user = FactoryBot.build(:kobe)
147
+ gogaku = dictum_user.gogakus.build({subject: "a",body: "a",file: Rack::Test::UploadedFile.new('spec/fixtures/files/Night_Sea.mp3', 'audio/mp3'),answer: "a"})
148
+ it "gogaku should be valid" do
149
+ expect(gogaku).to be_valid
150
+ end
151
+ end
152
+ end
153
+
154
+ #### ここまで
145
155
 
146
156
   #gogaku.rb
147
157
 
@@ -200,6 +210,18 @@
200
210
  0 examples, 0 failures, 1 error occurred outside of examples
201
211
  ```
202
212
  どうやらエラーによると、引数はハッシュで渡さないといけないというエラーのようです。しかし、私の場合、user.gogakus.build(FactoryBot.build(:music))のようにしているので:musicのシンボルがキーでその中身がバリューのハッシュの構造になっていると考えたのですが、間違っているでしょうか?
213
+ また、なぜか「when user is deleted gogaku is deleted too」の部分でsaveしているにもかかわらず下記のエラーが出ます
214
+ ```
215
+ Pending: (Failures listed here are expected and do not affect your suite's status)
216
+
217
+ 1) DictumUser test for DictumUser when user input invalid parameter when user is deleted gogaku is deleted too
218
+ # No reason given
219
+ Failure/Error: user.gogakus.create(gogaku)
220
+
221
+ ActiveRecord::RecordNotSaved:
222
+ You cannot call create unless the parent is saved
223
+ # ./spec/models/dictum_user_spec.rb:125:in `block (5 levels) in <top (required)>'
224
+ ```
203
225
 
204
226
  仕方なくfactrybotを使わないで直接書き込むことにしました。
205
227
  以下のように記述しました
@@ -239,7 +261,7 @@
239
261
  rspec ./spec/models/dictum_user_spec.rb:19 # DictumUser test for DictumUser should be valid create instance accurately
240
262
  rspec ./spec/models/gogaku_spec.rb:12 # Gogaku test for gogaku gogaku should be valid
241
263
  ```
242
- Dictum user can't be blankとエラーが返ってきたので一応ハッシュで値をわたせているようです。ですが、今度はDictum Userが空ではいけないというエラーが出てしまいました。これはモデルでhas_manyの関係になっていることが原因かと考えられます(DictumUserが親Gogakuが子)。しかも、ここで前はパスしていたテストが失敗してしまいました。理由は恐らく以下のことも試したことが原因だと考えられます。
264
+ Dictum user can't be blankとエラーが返ってきたので一応ハッシュで値をわたせているようです。ですが、今度はDictum Userが空ではいけないというエラーが出てしまいました。これはモデルでhas_manyの関係になっていることが原因かと考えられます(DictumUserが親Gogakuが子)。しかも、ここで前はパスしていたテスト(DictumUser正しく作れるか確認するテスト)が失敗してしまいました。理由は恐らく以下のことも試したことが原因だと考えられます。
243
265
  ・FactoryBot.createを使ったから
244
266
  ・dictum_user = DictumUser.new(name: "abc",email: "abc@example.com", password: "foobar", password_confirmation: "foobar")のように直接書き込んだから?作成したuserのemailが違うので可能性は極めて低いが念のため記述
245
267
  これらの操作しかしていないのになぜemailのバリデーションに引っかかってしまったのか分かりません。テスト終了後に自動的にロールバックされるとの情報があったのでそう思い込んでいましたが、どうなのでしょうか?

2

追記

2022/03/21 23:18

投稿

senseIY
senseIY

スコア281

test CHANGED
File without changes
test CHANGED
@@ -199,7 +199,51 @@
199
199
  Finished in 0.00007 seconds (files took 0.88052 seconds to load)
200
200
  0 examples, 0 failures, 1 error occurred outside of examples
201
201
  ```
202
- どうやらエラーによると、引数はハッシュで渡さないといけないというエラーのようです。しかし、私の場合、user.gogakus.build(FactoryBot.build(:music))のようにしているので:musicのシンボルがキーでその中身がバリューのハッシュの構造になっていると考えたのですが、間違っているでしょうか?ここで詰まってしまいました。
202
+ どうやらエラーによると、引数はハッシュで渡さないといけないというエラーのようです。しかし、私の場合、user.gogakus.build(FactoryBot.build(:music))のようにしているので:musicのシンボルがキーでその中身がバリューのハッシュの構造になっていると考えたのですが、間違っているでしょうか?
203
+
204
+ 仕方なくfactrybotを使わないで直接書き込むことにしました。
205
+ 以下のように記述しました
206
+ ```
207
+ require 'rails_helper'
208
+
209
+ RSpec.describe Gogaku, type: :model do
210
+ describe "test for gogaku" do
211
+ dictum_user = FactoryBot.build(:kobe)
212
+ gogaku = dictum_user.gogakus.build({subject: "a",body: "a",file: Rack::Test::UploadedFile.new('spec/fixtures/files/Night_Sea.mp3', 'audio/mp3'),answer: "a"})
213
+ it "gogaku should be valid" do
214
+ expect(gogaku).to be_valid
215
+ end
216
+ end
217
+ end
218
+
219
+ ```
220
+ このように記述したところ、エラーが変わりました。
221
+ ```
222
+ Failures:
223
+
224
+ 1) DictumUser test for DictumUser should be valid create instance accurately
225
+ Failure/Error: expect(user).to be_valid
226
+ expected #<DictumUser id: nil, name: "kobe", email: "kobe@example.com", created_at: nil, updated_at: nil, password_digest: [FILTERED], admin: false> to be valid, but got errors: Email has already been taken
227
+ # ./spec/models/dictum_user_spec.rb:24:in `block (4 levels) in <top (required)>'
228
+
229
+ 2) Gogaku test for gogaku gogaku should be valid
230
+ Failure/Error: expect(gogaku).to be_valid
231
+ expected #<Gogaku id: nil, subject: "a", body: "a", file: nil, answer: "a", dictum_user_id: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Dictum user can't be blank
232
+ # ./spec/models/gogaku_spec.rb:16:in `block (3 levels) in <main>'
233
+
234
+ Finished in 0.2429 seconds (files took 1.65 seconds to load)
235
+ 11 examples, 2 failures, 1 pending
236
+
237
+ Failed examples:
238
+
239
+ rspec ./spec/models/dictum_user_spec.rb:19 # DictumUser test for DictumUser should be valid create instance accurately
240
+ rspec ./spec/models/gogaku_spec.rb:12 # Gogaku test for gogaku gogaku should be valid
241
+ ```
242
+ Dictum user can't be blankとエラーが返ってきたので一応ハッシュで値をわたせているようです。ですが、今度はDictum Userが空ではいけないというエラーが出てしまいました。これはモデルでhas_manyの関係になっていることが原因かと考えられます(DictumUserが親Gogakuが子)。しかも、ここで前はパスしていたテストが失敗してしまいました。理由は恐らく以下のことも試したことが原因だと考えられます。
243
+ ・FactoryBot.createを使ったから
244
+ ・dictum_user = DictumUser.new(name: "abc",email: "abc@example.com", password: "foobar", password_confirmation: "foobar")のように直接書き込んだから?作成したuserのemailが違うので可能性は極めて低いが念のため記述
245
+ これらの操作しかしていないのになぜemailのバリデーションに引っかかってしまったのか分かりません。テスト終了後に自動的にロールバックされるとの情報があったのでそう思い込んでいましたが、どうなのでしょうか?
246
+ ここで詰まってしまいました。
203
247
  ### その他
204
248
  ・ruby3.0.3 rails6.1.5 環境構築はDockerで行いました。wsl2のUbuntuを使っています。
205
249
 

1

タイトル変更

2022/03/21 11:24

投稿

senseIY
senseIY

スコア281

test CHANGED
@@ -1 +1 @@
1
- Rspecで音声ファイルの設定が出来ず、ArgumentError:にってしまう
1
+ Rspecで音声ファイルの設定が出来ず、hashを渡すことができ
test CHANGED
File without changes