I want to make sure that an element on my website is only displayed when logging in.
This is how I achieve it at the moment:
it 'displays statistics when logged in' do
expect {
login_as(create :user)
refresh_page
}.to change {
page.has_content? 'Statistics'
}.from(false).to true # I know, "to true" is redundant here, but I like it to be explicit
end
This somehow feels clumsy. Especially, when the spec fails, I don't get the nice error message I usually get when doing an expect(page).to have_content 'Statistics', I just get something like "expected result to have changed from false to true, but did not change" which isn't very informative.
I know there are shared examples, but they feel a bit too much for this case.
I tried something like the following, but didn't succeed either:
it 'displays statistics when logged in' do
expect(expect_to_have_content_statistics).to raise_error
login_as(create :user)
refresh_page
expect_to_have_content_statistics
end
def expect_to_have_content_statistics
expect(page).to have_content 'Statistics'
end
Any ideas? I don't want to write the expectation 2 times, as this is very error prone.
You're testing two different cases - would suggest to separate them.
describe 'statistics' do
def have_statistics
have_content('Statistics')
end
before { visit_statistics_page }
it { expect(page).to_not have_statistics }
it 'displays statistics when logged in' do
login_as(create :user)
expect(page).to have_statistics
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With