1 # -*- encoding: utf-8 -*-
5 describe AuthorsController do
7 FactoryGirl.create :admin
8 @sp = FactoryGirl.create :system_picture
9 @lg = FactoryGirl.create :license_group
10 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11 @user = FactoryGirl.create( :user_yas)
12 @author = @user.author
13 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16 describe '一覧表示に於いて' do
18 Author.stub(:list).and_return([@author, @author, @author])
22 it '与えられたpageがセットされている' do
23 get :index, :page => 5
24 assigns(:page).should eq 5
26 it '省略されると@pageに1値が入る' do
28 assigns(:page).should eq 1
30 it '与えられたpage_sizeがセットされている' do
31 get :index, :page_size => 15
32 assigns(:page_size).should eq 15
34 it '省略されると@page_sizeにデフォルト値が入る' do
36 assigns(:page_size).should eq Author.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq Author.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq Author.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it '作家モデルに一覧を問い合わせている' do
53 Author.should_receive(:list).exactly(1)
56 it '@authorsにリストを取得している' do
58 assigns(:authors).should have_at_least(3).items
61 it 'indexテンプレートを描画する' do
63 response.should render_template("index")
68 get :index, :format => :json
69 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71 it 'データがリスト構造になっている' do
72 get :index, :format => :json
73 json = JSON.parse response.body
74 json.should have_at_least(3).items
76 it 'リストの先頭くらいは作家っぽいものであって欲しい' do
77 get :index, :format => :json
78 json = JSON.parse response.body
79 json.first.has_key?("name").should be_true
83 context '作家権限がないとき' do
88 it 'ステータスコード302 Foundを返す' do
90 response.status.should eq 302
94 response.should redirect_to '/users/sign_in'
98 it 'ステータスコード401 Unauthorizedを返す' do
99 get :index, :format => :json
100 response.status.should eq 401
102 it '応答メッセージにUnauthorizedを返す' do
103 get :index, :format => :json
104 response.message.should match(/Unauthorized/)
112 Author.stub(:show).and_return(@author)
115 context 'つつがなく終わるとき' do
116 it 'ステータスコード200 OKを返す' do
117 get :show, :id => @author.id
118 response.should be_success
120 it '作家モデルに単体取得を問い合わせている' do
121 Author.should_receive(:show).exactly(1)
124 it '@authorにアレを取得している' do
125 get :show, :id => @author.id
126 assigns(:author).should eq(@author)
129 it 'showテンプレートを描画する' do
130 get :show, :id => @author.id
131 response.should render_template("show")
136 get :show, :id => @author.id, :format => :json
137 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
140 get :show, :id => @author.id, :format => :json
141 json = JSON.parse response.body
142 json["name"].should match(/name/)
146 context '作家権限がないとき' do
151 it 'ステータスコード302 Foundを返す' do
152 get :show, :id => @author.id
153 response.status.should eq 302
155 it 'サインインページへ遷移する' do
156 get :show, :id => @author.id
157 response.body.should redirect_to '/users/sign_in'
161 it 'ステータスコード401 Unauthorizedを返す' do
162 get :show, :id => @author.id, :format => :json
163 response.status.should eq 401
165 it '応答メッセージにUnauthorizedを返す' do
166 get :show, :id => @author.id, :format => :json
167 response.message.should match(/Unauthorized/)
172 context '対象作家がないとき' do
174 it '例外404 not_foundを返す' do
177 }.should raise_error(ActiveRecord::RecordNotFound)
181 it '例外404 not_foundを返す' do
183 get :show, :id => 0, :format => :json
184 }.should raise_error(ActiveRecord::RecordNotFound)
191 describe '作家数取得に於いて' do
193 Author.should_receive(:visible_count).and_return(3)
196 context 'つつがなく終わるとき' do
197 it 'ステータスコード200 OKを返す' do
198 get :count, :format => :json
199 response.should be_success
203 get :count, :format => :json
204 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
206 it 'データがHash構造になっていてコミック数が1である' do
207 get :count, :format => :json
208 json = JSON.parse response.body
209 json["count"].should == 3