質問編集履歴
3
文字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,6 +42,7 @@
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
+
factories/master_costs.rb
|
45
46
|
```ruby
|
46
47
|
FactoryGirl.define do
|
47
48
|
factory :master_cost do
|
2
説明変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
Rspecでコントローラーのテストを行っていたところ、Ransack
|
1
|
+
Rspecでコントローラーのテストを行っていたところ、Ransackの検索テストでエラーが発生し、自分一人では解決に至らないので質問させていただきます。
|
2
2
|
|
3
3
|
http://biboroku.megaya.net/entry/2015/02/02/150619
|
4
|
-
上記の記事を参考に
|
4
|
+
上記の記事を参考にコードを書いています。
|
5
5
|
|
6
6
|
|
7
7
|
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
いつも大変お世話になっております。
|
2
1
|
Rspecでコントローラーのテストを行っていたところ、Ransack周りのテストだけ全く上手くいかず質問させていただきます。
|
3
2
|
|
4
3
|
http://biboroku.megaya.net/entry/2015/02/02/150619
|
@@ -17,20 +16,25 @@
|
|
17
16
|
```
|
18
17
|
rspec実行時のエラーログ
|
19
18
|
|
19
|
+
Failures:
|
20
|
+
|
20
|
-
1) MasterCostsController GET #index
|
21
|
+
1) MasterCostsController GET #index when master_costs searched
|
21
22
|
Failure/Error: @params[:q][:name_cont] = 'あああ'
|
23
|
+
|
22
24
|
NoMethodError:
|
23
|
-
undefined method `[]' for nil:NilClass
|
25
|
+
undefined method `[]' for nil:NilClass
|
26
|
+
# ./spec/controllers/master_costs_controller_spec.rb:28:in `block (4 levels) in <top (required)>'
|
24
27
|
|
25
|
-
|
28
|
+
Finished in 0.34403 seconds (files took 8.81 seconds to load)
|
29
|
+
3 examples, 1 failure
|
26
30
|
|
27
31
|
Failed examples:
|
32
|
+
|
28
|
-
rspec ./spec/controllers/master_costs_controller_spec.rb:
|
33
|
+
rspec ./spec/controllers/master_costs_controller_spec.rb:27 # MasterCostsController GET #index when master_costs searched
|
29
34
|
```
|
30
35
|
```
|
31
36
|
app/controllers/master_costs_controller.rb
|
32
37
|
|
33
|
-
|
34
38
|
def index
|
35
39
|
parameters = params[:q] ? params.require(:q).permit(:code_eq, :name_cont, :cost_class_eq, :budger_class_eq).to_h : {}
|
36
40
|
@query = MasterCost.search(parameters)
|
@@ -39,13 +43,27 @@
|
|
39
43
|
```
|
40
44
|
|
41
45
|
```ruby
|
46
|
+
FactoryGirl.define do
|
42
|
-
|
47
|
+
factory :master_cost do
|
43
48
|
|
49
|
+
sequence(:code)
|
50
|
+
sequence(:name) { |i| "MasterCostName#{ i }" }
|
51
|
+
cost_class { MasterCost.cost_classes.values.sample }
|
52
|
+
budger_class { MasterCost.budget_classes.values.sample }
|
53
|
+
|
54
|
+
@params = Hash.new,
|
55
|
+
@params[:q] = Hash.new
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
44
60
|
FactoryGirl.define do
|
45
|
-
|
61
|
+
before :each do
|
46
|
-
@
|
62
|
+
@data1 = create(:data, code: 1, name: 'あああ', cost_class: 1, budger_class: 'A')
|
63
|
+
@data2 = create(:data, code: 2, name: 'いいい', cost_class: 2, budger_class: 'AB')
|
64
|
+
@data3 = create(:data, code: 3, name: 'ううう', cost_class: 3, budger_class: 'A')
|
47
|
-
@params = Hash.new
|
65
|
+
@params = Hash.new
|
48
|
-
@params[:q] = Hash.new
|
66
|
+
@params[:q][] = Hash.new
|
49
67
|
end
|
50
68
|
end
|
51
69
|
```
|
@@ -56,22 +74,17 @@
|
|
56
74
|
|
57
75
|
describe MasterCostsController do
|
58
76
|
|
59
|
-
|
77
|
+
context 'when master_costs searched' do
|
78
|
+
before do
|
79
|
+
create_list(:master_cost, 10) #検索用のデータ
|
80
|
+
end
|
60
81
|
|
61
|
-
|
82
|
+
specify do
|
62
|
-
|
83
|
+
@params[:q][:name_cont] = 'あああ'
|
63
|
-
|
84
|
+
get :index, @params
|
64
|
-
|
85
|
+
expect(assigns(:master_costs)).to match_array([@data1])
|
65
|
-
|
86
|
+
end
|
66
87
|
end
|
67
88
|
```
|
68
89
|
|
69
|
-
説明が長くなってしまい申し訳ありません。
|
70
|
-
エラーログにある
|
71
|
-
```
|
72
|
-
NoMethodError:
|
73
|
-
undefined method `[]' for nil:NilClass
|
74
|
-
```
|
75
|
-
が
|
90
|
+
[:name_cont]がパラメータを受け取るにはどのようにしたらよいでしょうか?....
|
76
|
-
ヒント等頂けましたら幸いです。
|
77
|
-
何卒よろしくお願いいたします。
|