1 # -*- encoding: utf-8 -*-
4 describe SpeechBalloonTemplate do
7 @f = Rails.root + 'spec/json/speech_balloon_template.json'
8 @t = File.open(@f, 'r').read
10 @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
11 @ts = File.open(@fs, 'r').read
13 @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
14 @tes = File.open(@fes, 'r').read
15 @jes = JSON.parse @tes
17 @admin = Factory :admin
18 @license = Factory :license
25 it 'オーソドックスなデータなら通る' do
26 @st = Factory.build :speech_balloon_template
30 context 'nameを検証するとき' do
32 @st = Factory.build :speech_balloon_template
35 @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
40 @st.should_not be_valid
44 @st.should_not be_valid
47 context 'classnameを検証するとき' do
49 @st = Factory.build :speech_balloon_template
52 @st.classname = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
57 @st.should_not be_valid
60 @st.classname = 'a'*51
61 @st.should_not be_valid
64 context 'captionを検証するとき' do
66 @st = Factory.build :speech_balloon_template
69 @st.caption = 'abcdefghi0abcdefghi0abcdefghi0'
74 @st.should_not be_valid
78 @st.should_not be_valid
83 @st = Factory.build :speech_balloon_template
91 @st.should_not be_valid
95 @st.should_not be_valid
99 @st.should_not be_valid
102 context 'settingsを検証するとき' do
104 @st = Factory.build :speech_balloon_template
112 @st.should_not be_valid
117 describe 'データ補充に於いて' do
123 describe 'Json解析に於いて' do
126 context 'テキストを渡されたとき' do
128 JSON.should_receive(:parse).with(@t).exactly(1)
129 r = SpeechBalloonTemplate.parse @t
132 r = SpeechBalloonTemplate.parse @t
133 r.is_a?(Hash).should be_true
136 context 'パース失敗したとき' do
138 JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
139 r = SpeechBalloonTemplate.parse @t
145 describe '対象フキダシテンプレートの取得に於いて' do
147 @st = Factory :speech_balloon_template
149 context 'nameが一致するフキダシテンプレートがないとき' do
150 it '新規フキダシテンプレートを準備して返す' do
151 r = SpeechBalloonTemplate.update_speech_balloon_template 'newtemplate', {:classname => 'New'}
152 r.should be_a_new SpeechBalloonTemplate
153 r[:classname].should eq 'New'
156 context 'nameが一致するフキダシテンプレートがあるとき' do
157 it '該当フキダシテンプレートを値設定して返す' do
158 r = SpeechBalloonTemplate.update_speech_balloon_template @st.name, {:classname => 'Update'}
159 r.is_a?(SpeechBalloonTemplate).should be_true
160 r.should_not be_a_new SpeechBalloonTemplate
161 r[:classname].should eq 'Update'
166 describe 'フキダシテンプレート保存に於いて' do
168 @st = Factory.build :speech_balloon_template
170 context 'つつがなく終わるとき' do
172 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
175 it 'フキダシテンプレートが作成されている' do
178 }.should change SpeechBalloonTemplate, :count
183 describe 'フキダシテンプレート更新に於いて' do
185 @st = Factory :speech_balloon_template
186 @attr = {:name => @st.name, :classname => 'Update', :t => 0, :caption => 'yp', :settings => '{}'}
187 @newattr = {:name => @st.name, :classname => 'New', :t => 1, :caption => 'nw', :settings => '{}'}
189 context 'つつがなく終わるとき' do
190 it '対象フキダシテンプレートを問い合わせている' do
191 SpeechBalloonTemplate.stub(:update_speech_balloon_template).with(any_args).and_return(SpeechBalloonTemplate.new(@attr))
192 SpeechBalloonTemplate.should_receive(:update_speech_balloon_template).exactly(1)
193 SpeechBalloonTemplate.store @st.name, @attr
196 it 'フキダシテンプレートを保存しようとしている' do
197 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
198 SpeechBalloonTemplate.store 'newtemplate', @newattr
200 it 'フキダシテンプレートが作成されている' do
202 SpeechBalloonTemplate.store 'newtemplate', @newattr
203 }.should change SpeechBalloonTemplate, :count
207 it 'フキダシテンプレートを保存しようとしている' do
208 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
209 SpeechBalloonTemplate.store @st.name, @attr
211 it 'フキダシテンプレートの数に変化がない' do
213 SpeechBalloonTemplate.store @st.name, @attr
214 }.should_not change SpeechBalloonTemplate, :count
218 r = SpeechBalloonTemplate.store 'newtemplate', @newattr
219 r.classname.should eq 'New'
221 it '保存されたSpeechBalloonTemplateオブジェクトを返す' do
222 r = SpeechBalloonTemplate.store 'newtemplate', @newattr
223 p r.errors.full_messages
224 r.should_not be_a_new SpeechBalloonTemplate
227 context 'フキダシテンプレートの作成に失敗するとき' do
229 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
232 it 'フキダシテンプレートに変化がない' do
234 r = SpeechBalloonTemplate.store 'newtemplate', @newattr
235 }.should_not change License, :count
239 it 'フキダシテンプレート属性に変化がない' do
241 r = SpeechBalloonTemplate.store @st.name, @attr
242 }.should_not change License.find(@st.id), :classname
248 describe 'テキスト取り込みに於いて' do
249 #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
252 context 'つつがなく終わるとき' do
254 SpeechBalloonTemplate.should_receive(:parse).with(any_args).exactly(1)
255 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(@j)
256 SpeechBalloonTemplate.import(@t)
258 it 'フキダシテンプレート更新を一回依頼する' do
259 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
260 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
261 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(1)
262 SpeechBalloonTemplate.import(@t)
264 it 'フキダシテンプレートが追加される' do
266 SpeechBalloonTemplate.import(@t)
267 }.should change SpeechBalloonTemplate, :count
270 SpeechBalloonTemplate.import(@t).should eq []
273 context '複数データがつつがなく終わるとき' do
274 it 'フキダシテンプレート更新を二回依頼する' do
275 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
276 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
277 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(2)
278 SpeechBalloonTemplate.import(@ts)
280 it 'フキダシテンプレートが二個追加される' do
282 SpeechBalloonTemplate.import(@ts)
283 }.should change(SpeechBalloonTemplate, :count).by 2
286 SpeechBalloonTemplate.import(@ts).should eq []
290 context 'Json解析に失敗したとき' do
292 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(false)
294 it 'フキダシテンプレートの数に変化がない' do
296 SpeechBalloonTemplate.import(@t)
297 }.should_not change SpeechBalloonTemplate, :count
300 SpeechBalloonTemplate.import(@t).should be_false
303 context 'フキダシテンプレート作成に失敗したとき' do
305 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
306 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(false)
308 it 'フキダシテンプレートの数に変化がない' do
310 SpeechBalloonTemplate.import(@t)
311 }.should_not change SpeechBalloonTemplate, :count
314 r = SpeechBalloonTemplate.import(@t)
315 r.is_a?(Array).should be_true
318 r = SpeechBalloonTemplate.import(@t)
319 r.should have(1).items
321 it 'フキダシテンプレートオブジェクトが入っている' do
322 r = SpeechBalloonTemplate.import(@t)
323 r.first.is_a?(SpeechBalloonTemplate).should be_true
326 context '複数のフキダシテンプレート作成に失敗したとき' do
327 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
328 it 'フキダシテンプレートの数に変化がない' do
330 SpeechBalloonTemplate.import(@tes)
331 }.should_not change SpeechBalloonTemplate, :count
333 it '途中で保存に失敗しても全件更新依頼する' do
334 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
335 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(3)
336 SpeechBalloonTemplate.import(@tes)
339 r = SpeechBalloonTemplate.import(@tes)
340 r.is_a?(Array).should be_true
343 r = SpeechBalloonTemplate.import(@tes)
344 r.should have(2).items
346 it '配列の中身は失敗したフキダシテンプレートオブジェクトが入っている' do
347 r = SpeechBalloonTemplate.import(@tes)
348 r[0].is_a?(SpeechBalloonTemplate).should be_true
349 r[0]["name"].should eq 'fail1'
350 r[1].is_a?(SpeechBalloonTemplate).should be_true
351 r[1]["name"].should eq 'fail2'
356 describe 'インポートエラーの表示に於いて' do
359 describe 'ファイル取り込みに於いて' do
361 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
363 context 'つつがなく終わるとき' do
365 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
367 it 'ファイルを開いてテキストを読む' do
368 File.should_receive(:open).with(@f, 'r').exactly(1)
369 SpeechBalloonTemplate.import_file(@f)
371 it 'テキスト取り込みを依頼する' do
372 SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
373 SpeechBalloonTemplate.import_file(@f)
377 SpeechBalloonTemplate.import_file(@f).should be_true
380 context 'ファイルが開けないとき' do
382 File.stub(:open).with(any_args).and_raise('StandardError')
384 it 'ファイルエラーのメッセージを出力する' do
388 SpeechBalloonTemplate.import_file(@f).should be_false
391 #失敗したときは、失敗したフキダシテンプレートが配列で返る
392 context 'テキスト取り込みが失敗したとき' do
394 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
396 it '各フキダシテンプレートのエラーメッセージを出力する' do
400 SpeechBalloonTemplate.import_file(@f).should be_false