follow_categories_controller_tests.rb
3.51 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
require_relative "../test_helper"
require 'follow_categories_controller'
class FollowCategoriesControllerTest < ActionController::TestCase
def setup
@controller = FollowCategoriesController.new
@person = create_user('person').person
login_as(@person.identifier)
end
should 'return all categories of a profile' do
category1 = Circle.create(:name => "category1", :person => @person)
category2 = Circle.create(:name => "category2", :person => @person)
get :index, :profile => @person.identifier
assert_equivalent [category1, category2], assigns[:categories]
end
should 'initialize an empty category for creation' do
get :new, :profile => @person.identifier
assert_nil assigns[:follow_category].id
assert_nil assigns[:follow_category].name
end
should 'create a new category' do
assert_difference '@person.follow_categories.count' do
post :create, :profile => @person.identifier,
:follow_category => { :name => 'category' }
end
assert_redirected_to :action => :index
end
should 'not create a category without a name' do
assert_no_difference '@person.follow_categories.count' do
post :create, :profile => @person.identifier, :follow_category => { :name => nil }
end
assert_template :new
end
should 'retrieve an existing category when editing' do
category = Circle.create(:name => "category", :person => @person)
get :edit, :profile => @person.identifier, :id => category.id
assert_equal category.name, assigns[:follow_category].name
end
should 'return 404 when editing a category that does not exist' do
get :edit, :profile => @person.identifier, :id => "nope"
assert_response 404
end
should 'update an existing category' do
category = Circle.create(:name => "category", :person => @person)
get :update, :profile => @person.identifier, :id => category.id,
:follow_category => { :name => "new name" }
category.reload
assert_equal "new name", category.name
assert_redirected_to :action => :index
end
should 'not update an existing category without a name' do
category = Circle.create(:name => "category", :person => @person)
get :update, :profile => @person.identifier, :id => category.id,
:follow_category => { :name => nil }
category.reload
assert_equal "category", category.name
assert_template :edit
end
should 'return 404 when updating a category that does not exist' do
get :update, :profile => @person.identifier, :id => "nope", :name => "new name"
assert_response 404
end
should 'destroy an existing category and update related profiles' do
category = Circle.create(:name => "category", :person => @person)
follower = fast_create(ProfileFollower, :profile_id => fast_create(Person).id,
:follower_id => @person.id, :follow_category_id => category.id)
assert_difference "@person.follow_categories.count", -1 do
get :destroy, :profile => @person.identifier, :id => category.id
end
follower.reload
assert_nil follower.follow_category
end
should 'return 404 when deleting and category that does not exist' do
get :destroy, :profile => @person.identifier, :id => "nope"
assert_response 404
end
should 'display notice when ' do
category = Circle.create(:name => "category", :person => @person)
Circle.any_instance.stubs(:destroy).returns(false)
get :destroy, :profile => @person.identifier, :id => category.id
assert_not_nil session[:notice]
end
end