CSC/ECE 517 Spring 2022 - E2206: Testing for users controller
About Expertiza
Expertiza is the software benefits for both instructors and students by supporting various types of submissions and providing reusable objects for peer review. It is an open-source project based on Ruby on Rails framework. It allows the instructors not only to create and customize new or existing assignments but also to create a list of topics the students can sign up for. Students can form teams to work on various projects and assignments. Expertiza also lets students peer-review other students' submissions, enabling them to work together to improve others' learning experiences.
Current Project Description
This Expertiza OSS project "E2206: Testing for users_controller" is focused on adding unit tests for users_controller.rb.
Files Involved
- users_controller.rb
- users_controller_spec.rb
Running Tests
rspec ./spec/controllers/users_controller_spec.rb
Testing
users_controller had 5 methods for which unit tests were missing. The unit tests added for these methods in the spec file 'users_controller_spec.rb' are discussed below.
action_allowed?
context '#set_anonymized_view' do it 'admin list_pending_requested' do params = { action: 'list_pending_requested' } allow(controller).to receive(:params).and_return(params) expect(controller.action_allowed?).to be false stub_current_user(student7, student7.role.name, student7.role) allow(controller).to receive(:params).and_return(params) expect(controller.action_allowed?).to be false stub_current_user(instructor, instructor.role.name, instructor.role) allow(controller).to receive(:params).and_return(params) expect(controller.action_allowed?).to be false stub_current_user(admin, admin.role.name, admin.role) allow(controller).to receive(:params).and_return(params) expect(controller.action_allowed?).to be true end end
auto_complete_for_user_name
context '#auto_complete_for_user_name' do it 'checks if auto_complete returns actionview error' do stub_current_user(student1, student1.role.name, student1.role) session = { user: student1 } @params = {user: student1} allow(controller).to receive(:params).and_return(@params) expect{controller.auto_complete_for_user_name}.to raise_error(ActionView::Template::Error) end it 'checks if get auto_complete redirects to test host' do stub_current_user(student1, student1.role.name, student1.role) session = { user: student1 } @params = {user: student1} allow(controller).to receive(:params).and_return(@params) get :auto_complete_for_user_name, @params, session expect(response).to redirect_to("http://test.host/") end end
destroy
context '#destroy' do it 'check if user was successfully destroyed' do allow(User).to receive(:find).with(any_args).and_return(student1) allow(AssignmentParticipant).to receive(:delete).with(any_args).and_return(true) allow(TeamsUser).to receive(:delete).with(any_args).and_return(true) allow(AssignmentQuestionnaire).to receive(:destroy).with(any_args).and_return(true) allow(User).to receive(:destroy).with(any_args).and_return(true) post :destroy expect(flash[:note]).to be_present end it 'check if user was not successfully destroyed' do allow(User).to receive(:find).with(any_args).and_return(nil) allow(AssignmentParticipant).to receive(:delete).with(any_args).and_return(true) allow(TeamsUser).to receive(:delete).with(any_args).and_return(true) allow(AssignmentQuestionnaire).to receive(:destroy).with(any_args).and_return(true) allow(User).to receive(:destroy).with(any_args).and_return(true) post :destroy expect(flash[:error]).to be_present end it 'check if successful destroy leads to redirect' do allow(User).to receive(:find).with(any_args).and_return(student1) allow(AssignmentParticipant).to receive(:delete).with(any_args).and_return(true) allow(TeamsUser).to receive(:delete).with(any_args).and_return(true) allow(AssignmentQuestionnaire).to receive(:destroy).with(any_args).and_return(true) allow(User).to receive(:destroy).with(any_args).and_return(true) post :destroy expect(response).to redirect_to :action=> :list end it 'check if error during destroy leads to redirect' do allow(User).to receive(:find).with(any_args).and_return(nil) allow(AssignmentParticipant).to receive(:delete).with(any_args).and_return(true) allow(TeamsUser).to receive(:delete).with(any_args).and_return(true) allow(AssignmentQuestionnaire).to receive(:destroy).with(any_args).and_return(true) allow(User).to receive(:destroy).with(any_args).and_return(true) post :destroy expect(response).to redirect_to :action=> :list end end
role
context '#edit' do it 'checks if role renders through edit' do new_student = User.new new_student = student1 new_student.role_id = nil allow(User).to receive(:find).with(any_args).and_return(new_student) get :edit expect(response).to render_template(:edit) end it 'checks if role fails through edit' do new_student = User.new new_student = student1 new_student.role_id = nil allow(User).to receive(:find).with(any_args).and_return(new_student) expect{controller.edit}.not_to raise_error end end