Commit 476a4f1184277502983c660aa80f539e9ab96a17

Authored by Guilherme Rojas V. de Lima + Rafael Reggiani Manzo
Committed by Rafael Manzo
1 parent 938e63fe

Refactored project controller unit test to use mocks instead of touch the database

Showing 1 changed file with 16 additions and 5 deletions   Show diff stats
spec/controllers/projects_controller_spec.rb
... ... @@ -114,7 +114,10 @@ describe ProjectsController do
114 114 before do
115 115 @user = FactoryGirl.create(:user)
116 116 @subject = FactoryGirl.build(:project)
117   - FactoryGirl.create(:project_ownership, {user_id: @user.id, project_id: @subject.id})
  117 + @ownership = FactoryGirl.build(:project_ownership)
  118 + @ownerships = []
  119 +
  120 + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
118 121  
119 122 sign_in @user
120 123 end
... ... @@ -122,6 +125,8 @@ describe ProjectsController do
122 125 context 'when the user owns the project' do
123 126 before :each do
124 127 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
  128 + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
  129 +
125 130 get :edit, :id => @subject.id
126 131 end
127 132  
... ... @@ -135,6 +140,7 @@ describe ProjectsController do
135 140 context 'when the user does not own the project' do
136 141 before do
137 142 @subject = FactoryGirl.build(:another_project)
  143 + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil)
138 144  
139 145 get :edit, :id => @subject.id
140 146 end
... ... @@ -152,6 +158,7 @@ describe ProjectsController do
152 158 describe 'update' do
153 159 before do
154 160 @user = FactoryGirl.create(:user)
  161 +
155 162 sign_in @user
156 163 end
157 164  
... ... @@ -159,9 +166,11 @@ describe ProjectsController do
159 166 before :each do
160 167 @subject = FactoryGirl.build(:project)
161 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 = []
162 171  
163   - FactoryGirl.create(:project_ownership, {user_id: @user.id, project_id: @subject.id})
164   -
  172 + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
  173 + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
165 174 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
166 175 Project.any_instance.expects(:update).with(@subject_params).returns(true)
167 176 end
... ... @@ -191,9 +200,11 @@ describe ProjectsController do
191 200 before :each do
192 201 @subject = FactoryGirl.build(:project)
193 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 = []
194 205  
195   - FactoryGirl.create(:project_ownership, {user_id: @user.id, project_id: @subject.id})
196   -
  206 + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
  207 + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
197 208 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
198 209 Project.any_instance.expects(:update).with(@subject_params).returns(false)
199 210  
... ...