users_controller_spec.rb
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'spec_helper'
describe UsersController do
it_requires_authentication
it_requires_admin
context 'Signed in as an admin' do
before do
sign_in Factory(:admin)
end
context "GET /users" do
it 'paginates all users' do
users = 3.times.inject(WillPaginate::Collection.new(1,30)) {|page,_| page << Factory.build(:user)}
User.should_receive(:paginate).and_return(users)
get :index
assigns(:users).should == users
end
end
context "GET /users/:id" do
it 'finds the user' do
user = Factory(:user)
get :show, :id => user.id
assigns(:user).should == user
end
end
context "GET /users/new" do
it 'assigns a new user' do
get :new
assigns(:user).should be_a(User)
assigns(:user).should be_new_record
end
end
context "GET /users/:id/edit" do
it 'finds the user' do
user = Factory(:user)
get :edit, :id => user.id
assigns(:user).should == user
end
end
context "POST /users" do
context "when the create is successful" do
before do
@user = Factory(:user)
User.should_receive(:new).and_return(@user)
@user.should_receive(:save).and_return(true)
end
it "sets a message to display" do
post :create
request.flash[:success].should include('part of the team')
end
it "redirects to the user's page" do
post :create
response.should redirect_to(user_path(@user))
end
end
context "when the create is unsuccessful" do
before do
@user = Factory(:user)
User.should_receive(:new).and_return(@user)
@user.should_receive(:save).and_return(false)
end
it "renders the new page" do
post :create
response.should render_template(:new)
end
end
end
context "PUT /users/:id" do
context "when the update is successful" do
before do
@user = Factory(:user)
end
it "sets a message to display" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
request.flash[:success].should include('updated')
end
it "redirects to the user's page" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
response.should redirect_to(user_path(@user))
end
end
context "when the update is unsuccessful" do
before do
@user = Factory(:user)
end
it "renders the edit page" do
put :update, :id => @user.to_param, :user => {:name => nil}
response.should render_template(:edit)
end
end
end
context "DELETE /users/:id" do
before do
@user = Factory(:user)
end
it "destroys the user" do
delete :destroy, :id => @user.id
User.where(:id => @user.id).first.should be_nil
end
it "redirects to the users index page" do
delete :destroy, :id => @user.id
response.should redirect_to(users_path)
end
it "sets a message to display" do
delete :destroy, :id => @user.id
request.flash[:notice].should include('no longer part of your team')
end
end
end
end