OSDN Git Service

ご恩登録機能のCRUDをscaffoldベースで生成し実装
[on-kiroku/go_on_kiroku_tyou.git] / spec / controllers / goons_controller_spec.rb
1 require 'spec_helper'
2
3 # This spec was generated by rspec-rails when you ran the scaffold generator.
4 # It demonstrates how one might use RSpec to specify the controller code that
5 # was generated by the Rails when you ran the scaffold generator.
6
7 describe GoonsController do
8
9   def mock_goon(stubs={})
10     @mock_goon ||= mock_model(Goon, stubs).as_null_object
11   end
12
13   describe "GET index" do
14     it "assigns all goons as @goons" do
15       Goon.stub(:all) { [mock_goon] }
16       get :index
17       assigns(:goons).should eq([mock_goon])
18     end
19   end
20
21   describe "GET show" do
22     it "assigns the requested goon as @goon" do
23       Goon.stub(:find).with("37") { mock_goon }
24       get :show, :id => "37"
25       assigns(:goon).should be(mock_goon)
26     end
27   end
28
29   describe "GET new" do
30     it "assigns a new goon as @goon" do
31       Goon.stub(:new) { mock_goon }
32       get :new
33       assigns(:goon).should be(mock_goon)
34     end
35   end
36
37   describe "GET edit" do
38     it "assigns the requested goon as @goon" do
39       Goon.stub(:find).with("37") { mock_goon }
40       get :edit, :id => "37"
41       assigns(:goon).should be(mock_goon)
42     end
43   end
44
45   describe "POST create" do
46     describe "with valid params" do
47       it "assigns a newly created goon as @goon" do
48         Goon.stub(:new).with({'these' => 'params'}) { mock_goon(:save => true) }
49         post :create, :goon => {'these' => 'params'}
50         assigns(:goon).should be(mock_goon)
51       end
52
53       it "redirects to the created goon" do
54         Goon.stub(:new) { mock_goon(:save => true) }
55         post :create, :goon => {}
56         response.should redirect_to(goon_url(mock_goon))
57       end
58     end
59
60     describe "with invalid params" do
61       it "assigns a newly created but unsaved goon as @goon" do
62         Goon.stub(:new).with({'these' => 'params'}) { mock_goon(:save => false) }
63         post :create, :goon => {'these' => 'params'}
64         assigns(:goon).should be(mock_goon)
65       end
66
67       it "re-renders the 'new' template" do
68         Goon.stub(:new) { mock_goon(:save => false) }
69         post :create, :goon => {}
70         response.should render_template("new")
71       end
72     end
73   end
74
75   describe "PUT update" do
76     describe "with valid params" do
77       it "updates the requested goon" do
78         Goon.stub(:find).with("37") { mock_goon }
79         mock_goon.should_receive(:update_attributes).with({'these' => 'params'})
80         put :update, :id => "37", :goon => {'these' => 'params'}
81       end
82
83       it "assigns the requested goon as @goon" do
84         Goon.stub(:find) { mock_goon(:update_attributes => true) }
85         put :update, :id => "1"
86         assigns(:goon).should be(mock_goon)
87       end
88
89       it "redirects to the goon" do
90         Goon.stub(:find) { mock_goon(:update_attributes => true) }
91         put :update, :id => "1"
92         response.should redirect_to(goon_url(mock_goon))
93       end
94     end
95
96     describe "with invalid params" do
97       it "assigns the goon as @goon" do
98         Goon.stub(:find) { mock_goon(:update_attributes => false) }
99         put :update, :id => "1"
100         assigns(:goon).should be(mock_goon)
101       end
102
103       it "re-renders the 'edit' template" do
104         Goon.stub(:find) { mock_goon(:update_attributes => false) }
105         put :update, :id => "1"
106         response.should render_template("edit")
107       end
108     end
109   end
110
111   describe "DELETE destroy" do
112     it "destroys the requested goon" do
113       Goon.stub(:find).with("37") { mock_goon }
114       mock_goon.should_receive(:destroy)
115       delete :destroy, :id => "37"
116     end
117
118     it "redirects to the goons list" do
119       Goon.stub(:find) { mock_goon }
120       delete :destroy, :id => "1"
121       response.should redirect_to(goons_url)
122     end
123   end
124
125 end