Description:
add announcement controller test
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r642:a8976e9a07cd - - 2 files changed: 4 inserted, 4 deleted

@@ -18,99 +18,99
18 # GET /announcements/1
18 # GET /announcements/1
19 # GET /announcements/1.xml
19 # GET /announcements/1.xml
20 def show
20 def show
21 @announcement = Announcement.find(params[:id])
21 @announcement = Announcement.find(params[:id])
22
22
23 respond_to do |format|
23 respond_to do |format|
24 format.html # show.html.erb
24 format.html # show.html.erb
25 format.xml { render :xml => @announcement }
25 format.xml { render :xml => @announcement }
26 end
26 end
27 end
27 end
28
28
29 # GET /announcements/new
29 # GET /announcements/new
30 # GET /announcements/new.xml
30 # GET /announcements/new.xml
31 def new
31 def new
32 @announcement = Announcement.new
32 @announcement = Announcement.new
33
33
34 respond_to do |format|
34 respond_to do |format|
35 format.html # new.html.erb
35 format.html # new.html.erb
36 format.xml { render :xml => @announcement }
36 format.xml { render :xml => @announcement }
37 end
37 end
38 end
38 end
39
39
40 # GET /announcements/1/edit
40 # GET /announcements/1/edit
41 def edit
41 def edit
42 @announcement = Announcement.find(params[:id])
42 @announcement = Announcement.find(params[:id])
43 end
43 end
44
44
45 # POST /announcements
45 # POST /announcements
46 # POST /announcements.xml
46 # POST /announcements.xml
47 def create
47 def create
48 @announcement = Announcement.new(announcement_params)
48 @announcement = Announcement.new(announcement_params)
49
49
50 respond_to do |format|
50 respond_to do |format|
51 if @announcement.save
51 if @announcement.save
52 flash[:notice] = 'Announcement was successfully created.'
52 flash[:notice] = 'Announcement was successfully created.'
53 format.html { redirect_to(@announcement) }
53 format.html { redirect_to(@announcement) }
54 format.xml { render :xml => @announcement, :status => :created, :location => @announcement }
54 format.xml { render :xml => @announcement, :status => :created, :location => @announcement }
55 else
55 else
56 format.html { render :action => "new" }
56 format.html { render :action => "new" }
57 format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
57 format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
58 end
58 end
59 end
59 end
60 end
60 end
61
61
62 # PUT /announcements/1
62 # PUT /announcements/1
63 # PUT /announcements/1.xml
63 # PUT /announcements/1.xml
64 def update
64 def update
65 @announcement = Announcement.find(params[:id])
65 @announcement = Announcement.find(params[:id])
66
66
67 respond_to do |format|
67 respond_to do |format|
68 if @announcement.update_attributes(announcement_params)
68 if @announcement.update_attributes(announcement_params)
69 flash[:notice] = 'Announcement was successfully updated.'
69 flash[:notice] = 'Announcement was successfully updated.'
70 format.html { redirect_to(@announcement) }
70 format.html { redirect_to(@announcement) }
71 format.js {}
71 format.js {}
72 format.xml { head :ok }
72 format.xml { head :ok }
73 else
73 else
74 format.html { render :action => "edit" }
74 format.html { render :action => "edit" }
75 format.js {}
75 format.js {}
76 format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
76 format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
77 end
77 end
78 end
78 end
79 end
79 end
80
80
81 def toggle
81 def toggle
82 @announcement = Announcement.find(params[:id])
82 @announcement = Announcement.find(params[:id])
83 @announcement.update_attributes( published: !@announcement.published? )
83 @announcement.update_attributes( published: !@announcement.published? )
84 respond_to do |format|
84 respond_to do |format|
85 format.js { render partial: 'toggle_button',
85 format.js { render partial: 'toggle_button',
86 locals: {button_id: "#announcement_toggle_#{@announcement.id}",button_on: @announcement.published? } }
86 locals: {button_id: "#announcement_toggle_#{@announcement.id}",button_on: @announcement.published? } }
87 end
87 end
88 end
88 end
89
89
90 def toggle_front
90 def toggle_front
91 @announcement = Announcement.find(params[:id])
91 @announcement = Announcement.find(params[:id])
92 @announcement.update_attributes( frontpage: !@announcement.frontpage? )
92 @announcement.update_attributes( frontpage: !@announcement.frontpage? )
93 respond_to do |format|
93 respond_to do |format|
94 format.js { render partial: 'toggle_button',
94 format.js { render partial: 'toggle_button',
95 locals: {button_id: "#announcement_toggle_front_#{@announcement.id}",button_on: @announcement.frontpage? } }
95 locals: {button_id: "#announcement_toggle_front_#{@announcement.id}",button_on: @announcement.frontpage? } }
96 end
96 end
97 end
97 end
98
98
99 # DELETE /announcements/1
99 # DELETE /announcements/1
100 # DELETE /announcements/1.xml
100 # DELETE /announcements/1.xml
101 def destroy
101 def destroy
102 @announcement = Announcement.find(params[:id])
102 @announcement = Announcement.find(params[:id])
103 @announcement.destroy
103 @announcement.destroy
104
104
105 respond_to do |format|
105 respond_to do |format|
106 format.html { redirect_to(announcements_url) }
106 format.html { redirect_to(announcements_url) }
107 format.xml { head :ok }
107 format.xml { head :ok }
108 end
108 end
109 end
109 end
110
110
111 private
111 private
112
112
113 def announcement_params
113 def announcement_params
114 - params.require(:announcement).permit(:author, :body, :published, :frontpage, :contest_only,:title, :note)
114 + params.require(:announcement).permit(:author, :body, :published, :frontpage, :contest_only, :title)
115 end
115 end
116 end
116 end
@@ -1,50 +1,50
1 require 'test_helper'
1 require 'test_helper'
2
2
3 class AnnouncementsControllerTest < ActionController::TestCase
3 class AnnouncementsControllerTest < ActionController::TestCase
4 setup do
4 setup do
5 @announcement = announcements(:one)
5 @announcement = announcements(:one)
6 - @request.session[:user_id] = user(:admin).id
6 + @request.session[:user_id] = users(:admin).id
7 end
7 end
8
8
9 test "should get index" do
9 test "should get index" do
10 get :index
10 get :index
11 assert_response :success
11 assert_response :success
12 assert_not_nil assigns(:announcements)
12 assert_not_nil assigns(:announcements)
13 end
13 end
14
14
15 test "should get new" do
15 test "should get new" do
16 get :new
16 get :new
17 assert_response :success
17 assert_response :success
18 end
18 end
19
19
20 test "should create announcement" do
20 test "should create announcement" do
21 assert_difference('Announcement.count') do
21 assert_difference('Announcement.count') do
22 - post :create, announcement: { }
22 + post :create, announcement: { author: 'aa',body: 'bb', published: true, frontpage: true, title: 'test'}
23 end
23 end
24
24
25 assert_redirected_to announcement_path(assigns(:announcement))
25 assert_redirected_to announcement_path(assigns(:announcement))
26 end
26 end
27
27
28 test "should show announcement" do
28 test "should show announcement" do
29 get :show, id: @announcement
29 get :show, id: @announcement
30 assert_response :success
30 assert_response :success
31 end
31 end
32
32
33 test "should get edit" do
33 test "should get edit" do
34 get :edit, id: @announcement
34 get :edit, id: @announcement
35 assert_response :success
35 assert_response :success
36 end
36 end
37
37
38 test "should update announcement" do
38 test "should update announcement" do
39 - patch :update, id: @announcement, announcement: { }
39 + patch :update, id: @announcement, announcement: { author: 'aa',body: 'bb', published: true, frontpage: true, title: 'test'}
40 assert_redirected_to announcement_path(assigns(:announcement))
40 assert_redirected_to announcement_path(assigns(:announcement))
41 end
41 end
42
42
43 test "should destroy announcement" do
43 test "should destroy announcement" do
44 assert_difference('Announcement.count', -1) do
44 assert_difference('Announcement.count', -1) do
45 delete :destroy, id: @announcement
45 delete :destroy, id: @announcement
46 end
46 end
47
47
48 assert_redirected_to announcements_path
48 assert_redirected_to announcements_path
49 end
49 end
50 end
50 end
You need to be logged in to leave comments. Login now