This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
announcements_controller_test.rb
80 lines (70 loc) · 2.74 KB
/
announcements_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
require 'test_helper'
class AnnouncementsControllerTest < ActionDispatch::IntegrationTest
setup do
@announcements = announcements
@announcement = announcements(:one)
end
test 'should get index' do
get '/'
assert_response :success
assert_select 'div.announcements', {:count=>1}
assert_select 'div.announcement', {:count=>2}
assert_match /My first announcement/, response.parsed_body, 'Failed to find first announcement'
assert_match /My other announcement/, response.parsed_body, 'Failed to find other announcement'
end
test 'unauthenticated user should not create announcement' do
assert_no_difference('Announcement.count') do
post announcements_url, params: { announcement: { text: 'My new announcement' } }
end
assert_redirected_to '/users/sign_in'
end
test 'authenticated user should create announcement' do
sign_in users(:one)
assert_difference('Announcement.count', +1) do
post announcements_url, params: { announcement: { text: 'My new announcement' } }
end
end
test 'authenticated user should not create blank announcement' do
sign_in users(:one)
assert_no_difference('Announcement.count') do
post announcements_url, params: { announcement: { text: '' } }
end
end
test 'unauthenticated user should not destroy announcement' do
get '/'
assert_no_match /Delete/, response.parsed_body, 'Authenticated user is not seeing link to delete announcement'
assert_no_difference('Announcement.count') do
delete announcement_url(@announcement)
end
assert_redirected_to '/users/sign_in'
end
test 'authenticated user should destroy announcement' do
sign_in users(:one)
get '/'
assert_match /Delete/, response.parsed_body, 'Authenticated user is not seeing link to delete announcement'
assert_difference('Announcement.count', -1) do
delete announcement_url(@announcement)
end
end
test 'unauthenticated user should not update announcement' do
assert_no_changes('Announcement.find(@announcement.id).text') do
patch announcement_url(@announcement), params: { announcement: {id: 1, text: 'Modified text'} }
end
assert_redirected_to '/users/sign_in'
end
test 'authenticated user should update announcement' do
sign_in users(:one)
assert_changes('Announcement.find(@announcement.id).text') do
patch announcement_url(@announcement), params: { announcement: {id: 1, text: 'Modified text'} }
end
assert_redirected_to announcements_url
end
test 'should offer JSON endpoints' do
get '/index.json'
assert_response :success
assert JSON.parse(response.body)
get "/announcements/#{@announcement.id}.json"
assert_response :success
assert JSON.parse(response.body)
end
end