/
characters_controller_test.rb
151 lines (112 loc) · 3.55 KB
/
characters_controller_test.rb
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'controllers/test_helper'
class CharactersControllerTest < ActionController::TestCase
tests CharactersController
describe "access control" do
it "requires a user for #index" do
get :index
must_redirect_to login_path
end
end
it "sets the site section to :characters" do
login_as_user
get :index
assigns(:current_navigation).must_equal :characters
end
describe "#index" do
before do
login_as_user
end
it "renders the list of characters owned by the current user" do
characters = [
Character.new(id: 1),
Character.new(id: 2),
Character.new(id: 3)
]
ListCharacters.expects(:all_for_user).with(@user).returns(characters)
get :index
assigns(:characters_by_guild).wont_be_nil
end
it "redirects to #new if there are no characters" do
ListCharacters.expects(:all_for_user).with(@user).returns([])
get :index
assert_redirected_to new_character_path
end
end
describe "#new" do
before do
login_as_user
end
it "renders the new character page" do
get :new
must_render_template "new"
assigns(:character).wont_be_nil
end
end
describe "#create" do
before do
login_as_user
end
it "creates a new character for the current user and redirects to index" do
AddCharacter.any_instance.expects(:run).with(
'name' => "Weemuu", 'character_class' => "shaman", 'guild_id' => "10"
).returns(true)
post :create, :character => {
:name => "Weemuu", :character_class => "shaman", :guild_id => 10
}
must_redirect_to characters_path
end
it "re-renders the form if creation failed" do
AddCharacter.any_instance.expects(:run).returns(false)
AddCharacter.any_instance.expects(:character).returns(Character.new)
post :create, :character => { }
must_render_template "new"
assigns(:character).wont_be_nil
end
end
describe "#edit" do
before do
login_as_user
end
it "renders the edit form for the given character" do
character = Character.new
FindCharacter.any_instance.expects(:by_id).with(10).returns(character)
get :edit, :id => 10
must_render_template "edit"
assigns(:character).must_equal character
end
end
describe "#update" do
before do
login_as_user
end
it "updates the given character with new information" do
character = Character.new
FindCharacter.any_instance.expects(:by_id).with(10).returns(character)
UpdateCharacter.any_instance.expects(:run).with(
'name' => "New Char Name", 'character_class' => "deathknight").returns(true)
put :update, :id => 10, :character => {
:name => "New Char Name",
:character_class => "deathknight"
}
must_redirect_to characters_path
end
it "re-renders the form if there were errors" do
character = Character.new
FindCharacter.any_instance.expects(:by_id).with(10).returns(character)
UpdateCharacter.any_instance.expects(:run).returns(false)
put :update, :id => 10
must_render_template "edit"
assigns(:character).must_equal character
end
end
describe "#make_main" do
it "triggers a main change for the give character" do
login_as_user
character = Character.new
FindCharacter.any_instance.expects(:by_id).with(10).returns(character)
SelectMainCharacter.expects(:run).with(character)
put :make_main, :id => 10
must_redirect_to characters_path
end
end
end