Skip to content

Commit 6b9929b

Browse files
committed
First step towards RSpec tests.
1 parent f479136 commit 6b9929b

File tree

12 files changed

+285
-10
lines changed

12 files changed

+285
-10
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--colour

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ gem 'zerg_support'
3232

3333
## Bundle gems for certain environments:
3434
# gem 'rspec', :group => :test
35-
group :test do
36-
gem 'mocha'
35+
group :development, :test do
36+
gem "rspec-rails", ">= 2.0.0.beta.20"
3737
end

Gemfile.lock

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ GEM
4343
daemonz (0.3.2)
4444
simple-daemon
4545
zerg_support
46+
diff-lcs (1.1.2)
4647
erubis (2.6.6)
4748
abstract (>= 1.0.0)
4849
eventmachine (0.12.10)
@@ -57,8 +58,6 @@ GEM
5758
memcache-client (1.8.5)
5859
mime-types (1.16)
5960
mit_stalker (1.0.3)
60-
mocha (0.9.8)
61-
rake
6261
mysql2 (0.2.3)
6362
pdf-reader (0.8.6)
6463
Ascii85 (>= 0.9)
@@ -83,6 +82,16 @@ GEM
8382
thor (~> 0.14.0)
8483
rake (0.8.7)
8584
rmagick (2.13.1)
85+
rspec (2.0.0.beta.20)
86+
rspec-core (= 2.0.0.beta.20)
87+
rspec-expectations (= 2.0.0.beta.20)
88+
rspec-mocks (= 2.0.0.beta.20)
89+
rspec-core (2.0.0.beta.20)
90+
rspec-expectations (2.0.0.beta.20)
91+
diff-lcs (>= 1.1.2)
92+
rspec-mocks (2.0.0.beta.20)
93+
rspec-rails (2.0.0.beta.20)
94+
rspec (= 2.0.0.beta.20)
8695
simple-daemon (0.1.2)
8796
starling (0.10.1)
8897
eventmachine (>= 0.12.0)
@@ -103,11 +112,11 @@ DEPENDENCIES
103112
gravtastic
104113
json
105114
mit_stalker
106-
mocha
107115
mysql2
108116
prawn!
109117
rails (= 3.0.0)
110118
rmagick
119+
rspec-rails (>= 2.0.0.beta.20)
111120
simple-daemon
112121
starling
113122
system_timer

autotest/discover.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Autotest.add_discovery { "rails" }
2+
Autotest.add_discovery { "rspec2" }

config/application.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class Application < Rails::Application
3636
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
3737

3838
# Configure generators values. Many other options are available, be sure to check the documentation.
39-
# config.generators do |g|
40-
# g.orm :active_record
41-
# g.template_engine :erb
42-
# g.test_framework :test_unit, :fixture => true
43-
# end
39+
config.generators do |g|
40+
g.orm :active_record
41+
g.template_engine :erb
42+
g.test_framework :rspec, :fixture => true
43+
end
4444
config.action_mailer.smtp_settings = {
4545
:address => "outgoing.mit.edu",
4646
:port => 25,
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
require 'spec_helper'
2+
3+
describe UsersController do
4+
5+
def mock_user(stubs={})
6+
@mock_user ||= mock_model(User, stubs).as_null_object
7+
end
8+
9+
describe "GET index" do
10+
it "assigns all users as @users" do
11+
User.stub(:all) { [mock_user] }
12+
get :index
13+
assigns(:users).should eq([mock_user])
14+
end
15+
end
16+
17+
describe "GET show" do
18+
it "assigns the requested user as @user" do
19+
User.stub(:find).with("37") { mock_user }
20+
get :show, :id => "37"
21+
assigns(:user).should be(mock_user)
22+
end
23+
end
24+
25+
describe "GET new" do
26+
it "assigns a new user as @user" do
27+
User.stub(:new) { mock_user }
28+
get :new
29+
assigns(:user).should be(mock_user)
30+
end
31+
end
32+
33+
describe "GET edit" do
34+
it "assigns the requested user as @user" do
35+
User.stub(:find).with("37") { mock_user }
36+
get :edit, :id => "37"
37+
assigns(:user).should be(mock_user)
38+
end
39+
end
40+
41+
describe "POST create" do
42+
43+
describe "with valid params" do
44+
it "assigns a newly created user as @user" do
45+
User.stub(:new).with({'these' => 'params'}) { mock_user(:save => true) }
46+
post :create, :user => {'these' => 'params'}
47+
assigns(:user).should be(mock_user)
48+
end
49+
50+
it "redirects to the created user" do
51+
User.stub(:new) { mock_user(:save => true) }
52+
post :create, :user => {}
53+
response.should redirect_to(user_url(mock_user))
54+
end
55+
end
56+
57+
describe "with invalid params" do
58+
it "assigns a newly created but unsaved user as @user" do
59+
User.stub(:new).with({'these' => 'params'}) { mock_user(:save => false) }
60+
post :create, :user => {'these' => 'params'}
61+
assigns(:user).should be(mock_user)
62+
end
63+
64+
it "re-renders the 'new' template" do
65+
User.stub(:new) { mock_user(:save => false) }
66+
post :create, :user => {}
67+
response.should render_template("new")
68+
end
69+
end
70+
71+
end
72+
73+
describe "PUT update" do
74+
75+
describe "with valid params" do
76+
it "updates the requested user" do
77+
User.should_receive(:find).with("37") { mock_user }
78+
mock_user.should_receive(:update_attributes).with({'these' => 'params'})
79+
put :update, :id => "37", :user => {'these' => 'params'}
80+
end
81+
82+
it "assigns the requested user as @user" do
83+
User.stub(:find) { mock_user(:update_attributes => true) }
84+
put :update, :id => "1"
85+
assigns(:user).should be(mock_user)
86+
end
87+
88+
it "redirects to the user" do
89+
User.stub(:find) { mock_user(:update_attributes => true) }
90+
put :update, :id => "1"
91+
response.should redirect_to(user_url(mock_user))
92+
end
93+
end
94+
95+
describe "with invalid params" do
96+
it "assigns the user as @user" do
97+
User.stub(:find) { mock_user(:update_attributes => false) }
98+
put :update, :id => "1"
99+
assigns(:user).should be(mock_user)
100+
end
101+
102+
it "re-renders the 'edit' template" do
103+
User.stub(:find) { mock_user(:update_attributes => false) }
104+
put :update, :id => "1"
105+
response.should render_template("edit")
106+
end
107+
end
108+
109+
end
110+
111+
describe "DELETE destroy" do
112+
it "destroys the requested user" do
113+
User.should_receive(:find).with("37") { mock_user }
114+
mock_user.should_receive(:destroy)
115+
delete :destroy, :id => "37"
116+
end
117+
118+
it "redirects to the users list" do
119+
User.stub(:find) { mock_user }
120+
delete :destroy, :id => "1"
121+
response.should redirect_to(users_url)
122+
end
123+
end
124+
125+
end

spec/fixtures/users.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# == Schema Information
2+
# Schema version: 20100504203833
3+
#
4+
# Table name: users
5+
#
6+
# id :integer(4) not null, primary key
7+
# name :string(64) not null
8+
# password_salt :string(16) not null
9+
# password_hash :string(64) not null
10+
# email :string(64) not null
11+
# active :boolean(1) not null
12+
# admin :boolean(1) not null
13+
# created_at :datetime
14+
# updated_at :datetime
15+
#
16+
17+
admin:
18+
name: admin
19+
password_salt: 1234
20+
password_hash: <%= User.hash_password('password', '1234').inspect %>
21+
22+
active: true
23+
admin: true
24+
25+
dexter:
26+
name: dexter
27+
password_salt: 5678
28+
password_hash: <%= User.hash_password('pa55w0rd', '5678').inspect %>
29+
30+
active: true
31+
admin: false
32+
33+
inactive:
34+
name: lurker
35+
password_salt: 1234
36+
password_hash: <%= User.hash_password('password', '1234').inspect %>
37+
38+
active: false
39+
admin: false
40+
41+
solo:
42+
name: solo
43+
password_salt: 1234
44+
password_hash: <%= User.hash_password('password', '1234').inspect %>
45+
46+
active: true
47+
admin: false
48+

spec/helpers/users_helper_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
3+
# Specs in this file have access to a helper object that includes
4+
# the User2sHelper. For example:
5+
#
6+
# describe User2sHelper do
7+
# describe "string concat" do
8+
# it "concats two strings with spaces" do
9+
# helper.concat_strings("this","that").should == "this that"
10+
# end
11+
# end
12+
# end
13+
describe UsersHelper do
14+
pending "add some examples to (or delete) #{__FILE__}"
15+
end

spec/models/user_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe User do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end

spec/requests/users_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'spec_helper'
2+
3+
describe "Users" do
4+
describe "GET /users" do
5+
it "works! (now write some real specs)" do
6+
get users_path
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)