OSDN Git Service

pass test
[pettanr/pettanr.git] / spec / controllers / artists_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #絵師
4
5 describe ArtistsController do
6   before do
7     Factory :admin
8     @license = Factory :license
9     @user = Factory( :user_yas)
10     @author = @user.author
11     @artist = Factory :artist_yas, :author_id => @author.id
12   end
13   
14   describe '一覧表示に於いて' do
15     before do
16       Artist.stub(:list).and_return([@artist, @artist, @artist])
17       sign_in @user
18     end
19     context '事前チェックする' do
20       it '与えられたpageがセットされている' do
21         get :index, :page => 5
22         assigns(:page).should eq 5
23       end
24       it '省略されると@pageに1値が入る' do
25         get :index
26         assigns(:page).should eq 1
27       end
28       it '与えられたpage_sizeがセットされている' do
29         get :index, :page_size => 15
30         assigns(:page_size).should eq 15
31       end
32       it '省略されると@page_sizeにデフォルト値が入る' do
33         get :index
34         assigns(:page_size).should eq Artist.default_page_size
35       end
36       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
37         get :index, :page_size => 1500
38         assigns(:page_size).should eq Artist.max_page_size
39       end
40       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 0
42         assigns(:page_size).should eq Artist.default_page_size
43       end
44     end
45     context 'つつがなく終わるとき' do
46       it 'ステータスコード200 OKを返す' do
47         get :index
48         response.should be_success 
49       end
50       it '絵師モデルに一覧を問い合わせている' do
51         Artist.should_receive(:list).exactly(1)
52         get :index
53       end
54       it '@artistsにリストを取得している' do
55         get :index
56         assigns(:artists).should have_at_least(3).items
57       end
58       context 'html形式' do
59         it 'indexテンプレートを描画する' do
60           get :index
61           response.should render_template("index")
62         end
63       end
64       context 'json形式' do
65         it 'jsonデータを返す' do
66           get :index, :format => :json
67           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
68         end
69         it 'データがリスト構造になっている' do
70           get :index, :format => :json
71           json = JSON.parse response.body
72           json.should have_at_least(3).items
73         end
74         it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
75           get :index, :format => :json
76           json = JSON.parse response.body
77           json.first.has_key?("name").should be_true
78         end
79       end
80     end
81     context '作家権限がないとき' do
82       before do
83         sign_out @user
84       end
85       context 'html形式' do
86         it 'ステータスコード302 Foundを返す' do
87           get :index
88           response.status.should eq 302
89         end
90         it 'サインインページへ遷移する' do
91           get :index
92           response.should redirect_to '/users/sign_in'
93         end
94       end
95       context 'json形式' do
96         it 'ステータスコード401 Unauthorizedを返す' do
97           get :index, :format => :json
98           response.status.should eq 401
99         end
100         it '応答メッセージにUnauthorizedを返す' do
101           get :index, :format => :json
102           response.message.should match(/Unauthorized/)
103         end
104       end
105     end
106   end
107   
108   describe '閲覧に於いて' do
109     before do
110       Artist.stub(:show).and_return(@artist)
111       sign_in @user
112     end
113     context 'つつがなく終わるとき' do
114       it 'ステータスコード200 OKを返す' do
115         get :show, :id => @artist.id
116         response.should be_success
117       end
118       it '絵師モデルに単体取得を問い合わせている' do
119         Artist.should_receive(:show).exactly(1)
120         get :show
121       end
122       it '@artistにアレを取得している' do
123         get :show, :id => @artist.id
124         assigns(:artist).should eq(@artist)
125       end
126       context 'html形式' do
127         it 'showテンプレートを描画する' do
128           get :show, :id => @artist.id
129           response.should render_template("show")
130         end
131       end
132       context 'json形式' do
133         it 'jsonデータを返す' do
134           get :show, :id => @artist.id, :format => :json
135           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
136         end
137         it 'データがアレになっている' do
138           get :show, :id => @artist.id, :format => :json
139           json = JSON.parse response.body
140           json["name"].should match(/yas/)
141         end
142       end
143     end
144     context '作家権限がないとき' do
145       before do
146         sign_out @user
147       end
148       context 'html形式' do
149         it 'ステータスコード302 Foundを返す' do
150           get :show, :id => @artist.id
151           response.status.should eq 302
152         end
153         it 'サインインページへ遷移する' do
154           get :show, :id => @artist.id
155           response.body.should redirect_to '/users/sign_in'
156         end
157       end
158       context 'json形式' do
159         it 'ステータスコード401 Unauthorizedを返す' do
160           get :show, :id => @artist.id, :format => :json
161           response.status.should eq 401
162         end
163         it '応答メッセージにUnauthorizedを返す' do
164           get :show, :id => @artist.id, :format => :json
165           response.message.should match(/Unauthorized/)
166         end
167       end
168     end
169 =begin
170     context '対象作家がないとき' do
171       context 'html形式' do
172         it '例外404 not_foundを返す' do
173           lambda{
174             get :show, :id => 0
175           }.should raise_error(ActiveRecord::RecordNotFound)
176         end
177       end
178       context 'json形式' do
179         it '例外404 not_foundを返す' do
180           lambda{ 
181             get :show, :id => 0, :format => :json
182           }.should raise_error(ActiveRecord::RecordNotFound)
183         end
184       end
185     end
186 =end
187   end
188   
189   describe '絵師数取得に於いて' do
190     before do
191       Artist.should_receive(:visible_count).and_return(3)
192 #      sign_in @user
193     end
194     context 'つつがなく終わるとき' do
195       it 'ステータスコード200 OKを返す' do
196         get :count, :format => :json
197         response.should be_success 
198       end
199       context 'json形式' do
200         it 'jsonデータを返す' do
201           get :count, :format => :json
202           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
203         end
204         it 'データがHash構造になっていてコミック数が1である' do
205           get :count, :format => :json
206           json = JSON.parse response.body
207           json["count"].should == 3
208         end
209       end
210     end
211   end
212
213 end