1 # -*- encoding: utf-8 -*-
5 describe ArtistsController do
8 @user = Factory( :user_yas)
10 @artist = Factory :artist_yas, :author_id => @author.id
13 describe '一覧表示に於いて' do
15 Artist.stub(:list).and_return([@artist, @artist, @artist])
19 it '与えられたpageがセットされている' do
20 get :index, :page => 5
21 assigns(:page).should eq 5
23 it '省略されると@pageに1値が入る' do
25 assigns(:page).should eq 1
27 it '与えられたpage_sizeがセットされている' do
28 get :index, :page_size => 15
29 assigns(:page_size).should eq 15
31 it '省略されると@page_sizeにデフォルト値が入る' do
33 assigns(:page_size).should eq Artist.default_page_size
35 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
36 get :index, :page_size => 1500
37 assigns(:page_size).should eq Artist.max_page_size
39 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
40 get :index, :page_size => 0
41 assigns(:page_size).should eq Artist.default_page_size
44 context 'つつがなく終わるとき' do
45 it 'ステータスコード200 OKを返す' do
47 response.should be_success
49 it '絵師モデルに一覧を問い合わせている' do
50 Artist.should_receive(:list).exactly(1)
53 it '@artistsにリストを取得している' do
55 assigns(:artists).should have_at_least(3).items
58 it 'indexテンプレートを描画する' do
60 response.should render_template("index")
65 get :index, :format => :json
66 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
68 it 'データがリスト構造になっている' do
69 get :index, :format => :json
70 json = JSON.parse response.body
71 json.should have_at_least(3).items
73 it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
74 get :index, :format => :json
75 json = JSON.parse response.body
76 json.first.has_key?("name").should be_true
80 context '作家権限がないとき' do
85 it 'ステータスコード302 Foundを返す' do
87 response.status.should eq 302
91 response.should redirect_to '/users/sign_in'
95 it 'ステータスコード401 Unauthorizedを返す' do
96 get :index, :format => :json
97 response.status.should eq 401
99 it '応答メッセージにUnauthorizedを返す' do
100 get :index, :format => :json
101 response.message.should match(/Unauthorized/)
109 Artist.stub(:show).and_return(@artist)
112 context 'つつがなく終わるとき' do
113 it 'ステータスコード200 OKを返す' do
114 get :show, :id => @artist.id
115 response.should be_success
117 it '絵師モデルに単体取得を問い合わせている' do
118 Artist.should_receive(:show).exactly(1)
121 it '@artistにアレを取得している' do
122 get :show, :id => @artist.id
123 assigns(:artist).should eq(@artist)
126 it 'showテンプレートを描画する' do
127 get :show, :id => @artist.id
128 response.should render_template("show")
133 get :show, :id => @artist.id, :format => :json
134 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
137 get :show, :id => @artist.id, :format => :json
138 json = JSON.parse response.body
139 json["name"].should match(/yas/)
143 context '作家権限がないとき' do
148 it 'ステータスコード302 Foundを返す' do
149 get :show, :id => @artist.id
150 response.status.should eq 302
152 it 'サインインページへ遷移する' do
153 get :show, :id => @artist.id
154 response.body.should redirect_to '/users/sign_in'
158 it 'ステータスコード401 Unauthorizedを返す' do
159 get :show, :id => @artist.id, :format => :json
160 response.status.should eq 401
162 it '応答メッセージにUnauthorizedを返す' do
163 get :show, :id => @artist.id, :format => :json
164 response.message.should match(/Unauthorized/)
169 context '対象作家がないとき' do
171 it '例外404 not_foundを返す' do
174 }.should raise_error(ActiveRecord::RecordNotFound)
178 it '例外404 not_foundを返す' do
180 get :show, :id => 0, :format => :json
181 }.should raise_error(ActiveRecord::RecordNotFound)
188 describe '絵師数取得に於いて' do
190 Artist.should_receive(:visible_count).and_return(3)
193 context 'つつがなく終わるとき' do
194 it 'ステータスコード200 OKを返す' do
195 get :count, :format => :json
196 response.should be_success
200 get :count, :format => :json
201 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
203 it 'データがHash構造になっていてコミック数が1である' do
204 get :count, :format => :json
205 json = JSON.parse response.body
206 json["count"].should == 3