Commit 3ac840f823529ecc580e0b361c66d92ffd1aaccb
Committed by
Rafael Manzo
1 parent
476a4f11
Exists in
colab
and in
4 other branches
Refactored projects controller unit tests for update action
Showing
1 changed file
with
51 additions
and
34 deletions
Show diff stats
spec/controllers/projects_controller_spec.rb
| ... | ... | @@ -157,61 +157,78 @@ describe ProjectsController do |
| 157 | 157 | |
| 158 | 158 | describe 'update' do |
| 159 | 159 | before do |
| 160 | - @user = FactoryGirl.create(:user) | |
| 161 | - | |
| 162 | - sign_in @user | |
| 160 | + @subject = FactoryGirl.build(:project) | |
| 161 | + @subject_params = Hash[FactoryGirl.attributes_for(:project).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 | |
| 163 | 162 | end |
| 164 | 163 | |
| 165 | - context 'with valid fields' do | |
| 166 | - before :each do | |
| 167 | - @subject = FactoryGirl.build(:project) | |
| 168 | - @subject_params = Hash[FactoryGirl.attributes_for(:project).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 | |
| 169 | - @ownership = FactoryGirl.build(:project_ownership) | |
| 170 | - @ownerships = [] | |
| 171 | - | |
| 172 | - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
| 173 | - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
| 174 | - Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
| 175 | - Project.any_instance.expects(:update).with(@subject_params).returns(true) | |
| 164 | + context 'when the user is logged in' do | |
| 165 | + before do | |
| 166 | + sign_in FactoryGirl.create(:user) | |
| 176 | 167 | end |
| 177 | 168 | |
| 178 | - context 'rendering the show' do | |
| 179 | - before :each do | |
| 180 | - Project.expects(:exists?).returns(true) | |
| 169 | + context 'when user owns the project' do | |
| 170 | + before do | |
| 171 | + @ownership = FactoryGirl.build(:project_ownership) | |
| 172 | + @ownerships = [] | |
| 181 | 173 | |
| 182 | - post :update, :id => @subject.id, :project => @subject_params | |
| 174 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
| 175 | + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
| 183 | 176 | end |
| 184 | 177 | |
| 185 | - it 'should redirect to the show view' do | |
| 186 | - response.should redirect_to project_path(@subject) | |
| 178 | + context 'with valid fields' do | |
| 179 | + before :each do | |
| 180 | + Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
| 181 | + Project.any_instance.expects(:update).with(@subject_params).returns(true) | |
| 182 | + end | |
| 183 | + | |
| 184 | + context 'rendering the show' do | |
| 185 | + before :each do | |
| 186 | + Project.expects(:exists?).returns(true) | |
| 187 | + | |
| 188 | + post :update, :id => @subject.id, :project => @subject_params | |
| 189 | + end | |
| 190 | + | |
| 191 | + it 'should redirect to the show view' do | |
| 192 | + response.should redirect_to project_path(@subject) | |
| 193 | + end | |
| 194 | + end | |
| 195 | + | |
| 196 | + context 'without rendering the show view' do | |
| 197 | + before :each do | |
| 198 | + post :update, :id => @subject.id, :project => @subject_params | |
| 199 | + end | |
| 200 | + | |
| 201 | + it { should respond_with(:redirect) } | |
| 202 | + end | |
| 203 | + end | |
| 204 | + | |
| 205 | + context 'with an invalid field' do | |
| 206 | + before :each do | |
| 207 | + Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
| 208 | + Project.any_instance.expects(:update).with(@subject_params).returns(false) | |
| 209 | + | |
| 210 | + post :update, :id => @subject.id, :project => @subject_params | |
| 211 | + end | |
| 212 | + | |
| 213 | + it { should render_template(:edit) } | |
| 187 | 214 | end |
| 188 | 215 | end |
| 189 | 216 | |
| 190 | - context 'without rendering the show view' do | |
| 217 | + context 'when the user does not own the project' do | |
| 191 | 218 | before :each do |
| 192 | 219 | post :update, :id => @subject.id, :project => @subject_params |
| 193 | 220 | end |
| 194 | 221 | |
| 195 | - it { should respond_with(:redirect) } | |
| 222 | + it { should redirect_to projects_path } | |
| 196 | 223 | end |
| 197 | 224 | end |
| 198 | 225 | |
| 199 | - context 'with an invalid field' do | |
| 226 | + context 'with no user logged in' do | |
| 200 | 227 | before :each do |
| 201 | - @subject = FactoryGirl.build(:project) | |
| 202 | - @subject_params = Hash[FactoryGirl.attributes_for(:project).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 | |
| 203 | - @ownership = FactoryGirl.build(:project_ownership) | |
| 204 | - @ownerships = [] | |
| 205 | - | |
| 206 | - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
| 207 | - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
| 208 | - Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
| 209 | - Project.any_instance.expects(:update).with(@subject_params).returns(false) | |
| 210 | - | |
| 211 | 228 | post :update, :id => @subject.id, :project => @subject_params |
| 212 | 229 | end |
| 213 | 230 | |
| 214 | - it { should render_template(:edit) } | |
| 231 | + it { should redirect_to new_user_session_path } | |
| 215 | 232 | end |
| 216 | 233 | end |
| 217 | 234 | ... | ... |