From f3027748b3180d24a30822dfe7cc506e79e444d7 Mon Sep 17 00:00:00 2001 From: Heitor Reis Date: Mon, 17 Nov 2014 15:44:48 -0200 Subject: [PATCH] Finished tests for reading groups controller --- spec/controllers/reading_groups_controller_spec.rb | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 file changed, 160 insertions(+), 166 deletions(-) diff --git a/spec/controllers/reading_groups_controller_spec.rb b/spec/controllers/reading_groups_controller_spec.rb index 208bd2a..71b9546 100644 --- a/spec/controllers/reading_groups_controller_spec.rb +++ b/spec/controllers/reading_groups_controller_spec.rb @@ -1,271 +1,265 @@ require 'rails_helper' describe ReadingGroupsController, :type => :controller do - pending 'waiting for kalibro configurations integration' do - describe 'new' do - before :each do - sign_in FactoryGirl.create(:user) - get :new - end - - it { is_expected.to respond_with(:success) } - it { is_expected.to render_template(:new) } + describe 'new' do + before :each do + sign_in FactoryGirl.create(:user) + get :new end - describe 'create' do - before do - sign_in FactoryGirl.create(:user) - end - - context 'with valid fields' do - let(:reading_group) { FactoryGirl.build(:reading_group) } - let(:subject_params) { Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers + it { is_expected.to respond_with(:success) } + it { is_expected.to render_template(:new) } + end - before :each do - ReadingGroup.any_instance.expects(:save).returns(true) - end + describe 'create' do + before do + sign_in FactoryGirl.create(:user) + end - context 'rendering the show' do - before :each do - ReadingGroup.expects(:exists?).returns(true) + context 'with valid fields' do + let(:reading_group) { FactoryGirl.build(:reading_group) } + let(:subject_params) { Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers - post :create, :reading_group => subject_params - end + before :each do + ReadingGroup.any_instance.expects(:save).returns(true) + end - it 'should redirect to the show view' do - expect(response).to redirect_to reading_group_path(reading_group) - end + context 'rendering the show' do + before :each do + post :create, :reading_group => subject_params end - context 'without rendering the show view' do - before :each do - post :create, :reading_group => subject_params - end - - it { is_expected.to respond_with(:redirect) } + it 'should redirect to the show view' do + expect(response).to redirect_to reading_group_path(reading_group.id) end end - context 'with an invalid field' do + context 'without rendering the show view' do before :each do - @subject = FactoryGirl.build(:reading_group) - @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers - - ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject) - ReadingGroup.any_instance.expects(:save).returns(false) - - post :create, :reading_group => @subject_params + post :create, :reading_group => subject_params end - it { is_expected.to render_template(:new) } + it { is_expected.to respond_with(:redirect) } end end - describe 'show' do - let!(:reading_group) { FactoryGirl.build(:reading_group) } - let(:reading) { FactoryGirl.build(:reading) } + context 'with an invalid field' do before :each do - subject.expects(:find_resource).with(ReadingGroup, reading_group.id).returns(reading_group) - get :show, :id => reading_group.id - end + @subject = FactoryGirl.build(:reading_group) + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers - it { is_expected.to render_template(:show) } - end + ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject) + ReadingGroup.any_instance.expects(:save).returns(false) - describe 'destroy' do - before do - @subject = FactoryGirl.build(:reading_group) + post :create, :reading_group => @subject_params end - context 'with an User logged in' do - before do - sign_in FactoryGirl.create(:user) - @ownership = FactoryGirl.build(:reading_group_ownership) - @ownerships = [] + it { is_expected.to render_template(:new) } + end + end - end + describe 'show' do + let!(:reading_group) { FactoryGirl.build(:reading_group) } + let(:reading) { FactoryGirl.build(:reading) } + before :each do + subject.expects(:find_resource).with(ReadingGroup, reading_group.id).returns(reading_group) + get :show, :id => reading_group.id + end - context 'when the user owns the reading group' do - before :each do - @ownership.expects(:destroy) - @subject.expects(:destroy) + it { is_expected.to render_template(:show) } + end - #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :( - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) - @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership) + describe 'destroy' do + before do + @subject = FactoryGirl.build(:reading_group) + end - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + context 'with an User logged in' do + before do + sign_in FactoryGirl.create(:user) + @ownership = FactoryGirl.build(:reading_group_ownership) + @ownerships = [] - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) - delete :destroy, :id => @subject.id - end + end - it 'should redirect to the reading groups page' do - expect(response).to redirect_to reading_groups_url - end + context 'when the user owns the reading group' do + before :each do + @ownership.expects(:destroy) + @subject.expects(:destroy) - it { is_expected.to respond_with(:redirect) } - end + #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :( + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) + @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership) - context "when the user doesn't own the reading group" do - before :each do - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) - delete :destroy, :id => @subject.id - end + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) + delete :destroy, :id => @subject.id + end - it { is_expected.to redirect_to(reading_group_path) } + it 'should redirect to the reading groups page' do + expect(response).to redirect_to reading_groups_url end + + it { is_expected.to respond_with(:redirect) } end - context 'with no User logged in' do + context "when the user doesn't own the reading group" do before :each do + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + delete :destroy, :id => @subject.id end - it { is_expected.to redirect_to new_user_session_path } + it { is_expected.to redirect_to(reading_group_path) } end end - describe 'index' do + context 'with no User logged in' do before :each do - @subject = FactoryGirl.build(:reading_group) - ReadingGroup.expects(:all).returns([@subject]) - get :index + delete :destroy, :id => @subject.id end - it { is_expected.to render_template(:index) } + it { is_expected.to redirect_to new_user_session_path } end + end - describe 'edit' do - before do - @subject = FactoryGirl.build(:reading_group) - end + describe 'index' do + before :each do + @subject = FactoryGirl.build(:reading_group) + ReadingGroup.expects(:all).returns([@subject]) + get :index + end - context 'with an User logged in' do - before do - @user = FactoryGirl.create(:user) - @ownership = FactoryGirl.build(:reading_group_ownership) - @ownerships = [] + it { is_expected.to render_template(:index) } + end - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + describe 'edit' do + before do + @subject = FactoryGirl.build(:reading_group) + end - sign_in @user - end + context 'with an User logged in' do + before do + @user = FactoryGirl.create(:user) + @ownership = FactoryGirl.build(:reading_group_ownership) + @ownerships = [] - context 'when the user owns the reading group' do - before :each do - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) - get :edit, :id => @subject.id - end + sign_in @user + end - it { is_expected.to render_template(:edit) } + context 'when the user owns the reading group' do + before :each do + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) - it 'should assign to @reading group the @subject' do - expect(assigns(:reading_group)).to eq(@subject) - end + get :edit, :id => @subject.id end - context 'when the user does not own the reading group' do - before do - @subject = FactoryGirl.build(:another_reading_group) - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) - - get :edit, :id => @subject.id - end + it { is_expected.to render_template(:edit) } - it { is_expected.to redirect_to(reading_group_path) } - it { is_expected.to set_the_flash[:notice].to("You're not allowed to do this operation") } + it 'should assign to @reading group the @subject' do + expect(assigns(:reading_group)).to eq(@subject) end end - context 'with no user logged in' do - before :each do + context 'when the user does not own the reading group' do + before do + @subject = FactoryGirl.build(:another_reading_group) + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) + get :edit, :id => @subject.id end - it { is_expected.to redirect_to new_user_session_path } + it { is_expected.to redirect_to(reading_group_path) } + it { is_expected.to set_the_flash[:notice].to("You're not allowed to do this operation") } + end + end + + context 'with no user logged in' do + before :each do + get :edit, :id => @subject.id end + + it { is_expected.to redirect_to new_user_session_path } + end + end + + describe 'update' do + before do + @subject = FactoryGirl.build(:reading_group) + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers end - describe 'update' do + context 'when the user is logged in' do before do - @subject = FactoryGirl.build(:reading_group) - @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers + sign_in FactoryGirl.create(:user) end - context 'when the user is logged in' do + context 'when user owns the reading group' do before do - sign_in FactoryGirl.create(:user) - end + @ownership = FactoryGirl.build(:reading_group_ownership) + @ownerships = [] - context 'when user owns the reading group' do - before do - @ownership = FactoryGirl.build(:reading_group_ownership) - @ownerships = [] + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + end - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) + context 'with valid fields' do + before :each do + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) + ReadingGroup.any_instance.expects(:save).with(@subject_params).returns(true) end - context 'with valid fields' do + context 'rendering the show' do before :each do - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) - ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true) - end - - context 'rendering the show' do - before :each do - ReadingGroup.expects(:exists?).returns(true) - - post :update, :id => @subject.id, :reading_group => @subject_params - end - - it 'should redirect to the show view' do - expect(response).to redirect_to reading_group_path(@subject) - end + post :update, :id => @subject.id, :reading_group => @subject_params end - context 'without rendering the show view' do - before :each do - post :update, :id => @subject.id, :reading_group => @subject_params - end - - it { is_expected.to respond_with(:redirect) } + it 'should redirect to the show view' do + expect(response).to redirect_to reading_group_path(@subject.id) end end - context 'with an invalid field' do + context 'without rendering the show view' do before :each do - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) - ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) - post :update, :id => @subject.id, :reading_group => @subject_params end - it { is_expected.to render_template(:edit) } + it { is_expected.to respond_with(:redirect) } end end - context 'when the user does not own the reading group' do + context 'with an invalid field' do before :each do + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject) + ReadingGroup.any_instance.expects(:save).with(@subject_params).returns(false) + post :update, :id => @subject.id, :reading_group => @subject_params end - it { is_expected.to redirect_to reading_group_path } + it { is_expected.to render_template(:edit) } end end - context 'with no user logged in' do + context 'when the user does not own the reading group' do before :each do post :update, :id => @subject.id, :reading_group => @subject_params end - it { is_expected.to redirect_to new_user_session_path } + it { is_expected.to redirect_to reading_group_path } end end + + context 'with no user logged in' do + before :each do + post :update, :id => @subject.id, :reading_group => @subject_params + end + + it { is_expected.to redirect_to new_user_session_path } + end end end -- libgit2 0.21.2