Remove duplication in test setup

This commit is contained in:
Scott Albertson 2013-11-01 11:22:52 -07:00
parent 2e7696630b
commit 58f96bdfb5
1 changed files with 21 additions and 10 deletions

View File

@ -836,11 +836,10 @@ describe UsersController do
context 'with authenticated user' do
context 'with permission to update' do
it 'allows the update' do
user = Fabricate(:user, name: 'Billy Bob')
log_in_user(user)
guardian = Guardian.new(user)
guardian.stubs(:ensure_can_edit!)
Guardian.stubs(new: guardian).with(user)
user = create_authenticated_user('Billy Bob')
stub_guardian(user) do |guardian|
guardian.stubs(:ensure_can_edit!).with(user)
end
put :update, username: user.username, name: 'Jim Tom'
@ -851,11 +850,11 @@ describe UsersController do
context 'without permission to update' do
it 'does not allow the update' do
user = Fabricate(:user, name: 'Billy Bob')
log_in_user(user)
guardian = Guardian.new(user)
guardian.stubs(:ensure_can_edit!).raises(Discourse::InvalidAccess.new)
Guardian.stubs(new: guardian).with(user)
user = create_authenticated_user('Billy Bob')
stub_guardian(user) do |guardian|
guardian.stubs(:ensure_can_edit!).
with(user).raises(Discourse::InvalidAccess.new)
end
put :update, username: user.username, name: 'Jim Tom'
@ -1105,4 +1104,16 @@ describe UsersController do
end
end
private
def create_authenticated_user(name)
log_in_user(Fabricate(:user, name: name))
end
def stub_guardian(user)
guardian = Guardian.new(user)
yield(guardian)
Guardian.stubs(new: guardian).with(user)
end
end