Skip to content

Commit 3911b94

Browse files
committed
Adds Character controller specs and Devise spec helpers
1 parent 8a1f050 commit 3911b94

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

app/controllers/characters_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def create
5454
:portrait_image => portrait_image
5555
})
5656

57-
format.html { redirect_to @portrait, notice: 'Character was successfully created.' }
57+
format.html { redirect_to @character, notice: 'Character was successfully created.' }
5858
format.json { render action: 'show', status: :created, location: @character }
5959
else
6060
format.html { render action: 'new' }

spec/controllers/characters_controller_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,32 @@
2020

2121
describe CharactersController do
2222

23+
login_user
24+
2325
# This should return the minimal set of attributes required to create a valid
2426
# Character. As you add validations to Character, be sure to
2527
# adjust the attributes here as well.
26-
let(:valid_attributes) { { } }
28+
let(:valid_attributes) do
29+
char = FactoryGirl.attributes_for(:character)
30+
char.delete :user_id
31+
char.each do |k,v|
32+
char[k] = v.to_s
33+
end
34+
char.stringify_keys
35+
end
36+
let(:new_attributes) do
37+
char = FactoryGirl.attributes_for(:character)
38+
char.delete :user_id
39+
char.each do |k,v|
40+
char[k] = v.to_s
41+
end
42+
char.stringify_keys
43+
end
2744

2845
# This should return the minimal set of values that should be in the session
2946
# in order to pass any filters (e.g. authentication) defined in
3047
# CharactersController. Be sure to keep this updated too.
31-
let(:valid_session) { {} }
48+
let(:valid_session) { session }
3249

3350
describe "GET index" do
3451
it "assigns all characters as @characters" do
@@ -106,8 +123,8 @@
106123
# specifies that the Character created on the previous line
107124
# receives the :update_attributes message with whatever params are
108125
# submitted in the request.
109-
Character.any_instance.should_receive(:update).with({ "these" => "params" })
110-
put :update, {:id => character.to_param, :character => { "these" => "params" }}, valid_session
126+
Character.any_instance.should_receive(:update).with(new_attributes)
127+
put :update, {:id => character.to_param, :character => new_attributes}, valid_session
111128
end
112129

113130
it "assigns the requested character as @character" do

spec/factories/users.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
FactoryGirl.define do
44
factory :user do
5+
email { Faker::Internet.email }
6+
password { "Password1234" }
57
end
68
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@
4040
# the seed, which is printed after each run.
4141
# --seed 1234
4242
config.order = "random"
43+
44+
config.include Devise::TestHelpers, :type => :controller
45+
config.extend ControllerMacros, :type => :controller
4346
end

spec/support/controller_macros.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module ControllerMacros
2+
def login_admin
3+
before(:each) do
4+
@request.env["devise.mapping"] = Devise.mappings[:admin]
5+
sign_in FactoryGirl.create(:admin)
6+
end
7+
end
8+
9+
def login_user
10+
before(:each) do
11+
@request.env["devise.mapping"] = Devise.mappings[:user]
12+
user = FactoryGirl.create(:user)
13+
# user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
14+
#TODO enable user.confirm! once the confirmable module is being used (i.e. post-alpha)
15+
sign_in user
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)