質問編集履歴

1

モデルを追記しました

2019/12/07 06:02

投稿

ketyan
ketyan

スコア22

test CHANGED
File without changes
test CHANGED
@@ -34,6 +34,64 @@
34
34
 
35
35
  ```
36
36
 
37
+
38
+
39
+ **モデル**
40
+
41
+ ```SouvenirModel
42
+
43
+ class Souvenir < ApplicationRecord
44
+
45
+ belongs_to :user
46
+
47
+ mount_uploader :picture ,PictureUploader
48
+
49
+ validates:name, presence: true,length: {maximum:70}
50
+
51
+ validates:comment, presence: true,length: {maximum:250}
52
+
53
+ validates:spot, presence: true
54
+
55
+ validates:price, presence: true
56
+
57
+ validates:genre, presence: true
58
+
59
+ validate :picture_size
60
+
61
+
62
+
63
+ private
64
+
65
+
66
+
67
+ def picture_size
68
+
69
+ if picture.size > 5.megabytes
70
+
71
+ errors.add(:picture, "should be less than 5MB")
72
+
73
+ end
74
+
75
+ end
76
+
77
+
78
+
79
+ def souvenir_params
80
+
81
+ params.require(:souvenirs).permit(:name, :spot, :price, :genre, :picture, :comment)
82
+
83
+ end
84
+
85
+
86
+
87
+
88
+
89
+ end
90
+
91
+
92
+
93
+ ```
94
+
37
95
  **コントローラー**
38
96
 
39
97
  ```SouvenirController
@@ -134,6 +192,38 @@
134
192
 
135
193
  ```
136
194
 
195
+ ```UserFactory
196
+
197
+ FactoryBot.define do
198
+
199
+
200
+
201
+ factory :user, class: User do
202
+
203
+ name { "MyString" }
204
+
205
+ email { "MyString@gmail.com" }
206
+
207
+ password {"MyString"}
208
+
209
+ password_confirmation {"MyString"}
210
+
211
+
212
+
213
+ trait :failure do
214
+
215
+ name {' '}
216
+
217
+ end
218
+
219
+
220
+
221
+ end
222
+
223
+ end
224
+
225
+ ```
226
+
137
227
  ###試したこと
138
228
 
139
229
  RequestSpec中にsouvenirインスタンスが生成されたことはdebuggerで確認できました。