Commit 476a4f1184277502983c660aa80f539e9ab96a17
Committed by
Rafael Manzo
1 parent
938e63fe
Exists in
colab
and in
4 other branches
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,7 +114,10 @@ describe ProjectsController do | ||
| 114 | before do | 114 | before do |
| 115 | @user = FactoryGirl.create(:user) | 115 | @user = FactoryGirl.create(:user) |
| 116 | @subject = FactoryGirl.build(:project) | 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 | sign_in @user | 122 | sign_in @user |
| 120 | end | 123 | end |
| @@ -122,6 +125,8 @@ describe ProjectsController do | @@ -122,6 +125,8 @@ describe ProjectsController do | ||
| 122 | context 'when the user owns the project' do | 125 | context 'when the user owns the project' do |
| 123 | before :each do | 126 | before :each do |
| 124 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) | 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 | get :edit, :id => @subject.id | 130 | get :edit, :id => @subject.id |
| 126 | end | 131 | end |
| 127 | 132 | ||
| @@ -135,6 +140,7 @@ describe ProjectsController do | @@ -135,6 +140,7 @@ describe ProjectsController do | ||
| 135 | context 'when the user does not own the project' do | 140 | context 'when the user does not own the project' do |
| 136 | before do | 141 | before do |
| 137 | @subject = FactoryGirl.build(:another_project) | 142 | @subject = FactoryGirl.build(:another_project) |
| 143 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil) | ||
| 138 | 144 | ||
| 139 | get :edit, :id => @subject.id | 145 | get :edit, :id => @subject.id |
| 140 | end | 146 | end |
| @@ -152,6 +158,7 @@ describe ProjectsController do | @@ -152,6 +158,7 @@ describe ProjectsController do | ||
| 152 | describe 'update' do | 158 | describe 'update' do |
| 153 | before do | 159 | before do |
| 154 | @user = FactoryGirl.create(:user) | 160 | @user = FactoryGirl.create(:user) |
| 161 | + | ||
| 155 | sign_in @user | 162 | sign_in @user |
| 156 | end | 163 | end |
| 157 | 164 | ||
| @@ -159,9 +166,11 @@ describe ProjectsController do | @@ -159,9 +166,11 @@ describe ProjectsController do | ||
| 159 | before :each do | 166 | before :each do |
| 160 | @subject = FactoryGirl.build(:project) | 167 | @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 | 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 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) | 174 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) |
| 166 | Project.any_instance.expects(:update).with(@subject_params).returns(true) | 175 | Project.any_instance.expects(:update).with(@subject_params).returns(true) |
| 167 | end | 176 | end |
| @@ -191,9 +200,11 @@ describe ProjectsController do | @@ -191,9 +200,11 @@ describe ProjectsController do | ||
| 191 | before :each do | 200 | before :each do |
| 192 | @subject = FactoryGirl.build(:project) | 201 | @subject = FactoryGirl.build(:project) |
| 193 | @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 | 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 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) | 208 | Project.expects(:find).with(@subject.id.to_s).returns(@subject) |
| 198 | Project.any_instance.expects(:update).with(@subject_params).returns(false) | 209 | Project.any_instance.expects(:update).with(@subject_params).returns(false) |
| 199 | 210 |