Refactor tests to complete assertions within a single it block.

This commit is contained in:
Guo Xiang Tan 2018-05-31 15:04:53 +08:00
parent 5086fdc76d
commit 2b783997fa

View File

@ -11,21 +11,13 @@ describe UsernameChanger do
context 'success' do context 'success' do
let(:new_username) { "#{user.username}1234" } let(:new_username) { "#{user.username}1234" }
before do
@result = UsernameChanger.change(user, new_username)
end
it 'returns true' do
expect(@result).to eq(true)
end
it 'should change the username' do it 'should change the username' do
@result = UsernameChanger.change(user, new_username)
expect(@result).to eq(true)
user.reload user.reload
expect(user.username).to eq(new_username) expect(user.username).to eq(new_username)
end
it 'should change the username_lower' do
user.reload
expect(user.username_lower).to eq(new_username.downcase) expect(user.username_lower).to eq(new_username.downcase)
end end
end end
@ -35,21 +27,12 @@ describe UsernameChanger do
let(:username_before_change) { user.username } let(:username_before_change) { user.username }
let(:username_lower_before_change) { user.username_lower } let(:username_lower_before_change) { user.username_lower }
before do
@result = UsernameChanger.change(user, wrong_username)
end
it 'returns false' do
expect(@result).to eq(false)
end
it 'should not change the username' do it 'should not change the username' do
@result = UsernameChanger.change(user, wrong_username)
expect(@result).to eq(false)
user.reload user.reload
expect(user.username).to eq(username_before_change) expect(user.username).to eq(username_before_change)
end
it 'should not change the username_lower' do
user.reload
expect(user.username_lower).to eq(username_lower_before_change) expect(user.username_lower).to eq(username_lower_before_change)
end end
end end
@ -57,18 +40,16 @@ describe UsernameChanger do
describe 'change the case of my username' do describe 'change the case of my username' do
let!(:myself) { Fabricate(:user, username: 'hansolo') } let!(:myself) { Fabricate(:user, username: 'hansolo') }
it 'should return true' do
expect(UsernameChanger.change(myself, "HanSolo")).to eq(true)
end
it 'should change the username' do it 'should change the username' do
UsernameChanger.change(myself, "HanSolo") expect do
expect(myself.reload.username).to eq('HanSolo') expect(UsernameChanger.change(myself, "HanSolo", myself)).to eq(true)
end end.to change { UserHistory.count }.by(1)
it "logs the action" do expect(myself.reload.username).to eq('HanSolo')
expect { UsernameChanger.change(myself, "HanSolo", myself) }.to change { UserHistory.count }.by(1)
expect { UsernameChanger.change(myself, "HanSolo", myself) }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe expect do
UsernameChanger.change(myself, "HanSolo", myself)
end.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
end end
end end