I'm new to the world of rails and test driven development. For TDD, I'm using RSpec and Capybara. Currently, I'm working on a tutorial to learn more about Rails and the author is using following syntaxes:
page.should have_title('All users')
expect(page).to have_selector('li', text: user.name)
Since it seems that both are interchangeable I'm wondering when to use which syntax? Because, for the described case above, I could also write:
page.should have_title('All users')
page.should have_selector('li', text: user.name)
Which basically does the same, right?
Also, when should I use "specify" instead of "it"?
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
In this case, I could also write:
it { should have_link('Sign out', href: signout_path) }
it { expect(user.reload.name).to eq new_name }
I guess the decision of which one to use is based on what I want to express. Maybe, you can help me out here?!
Thanks!
page.should have_title('All users')
expect(page).to have_selector('li', text: user.name)
Go with the latter one, it's newer, they're pushing in that direction. I don't know if they have the intent of deprecating the former, but if they do, you won't have to go update all your code.
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
They are aliases, so just choose the one that makes it clearer. If you name your tests, you will know when to use which (example).
it { should have_link('Sign out', href: signout_path) }
Frankly, I avoid the non-named spec style. It's a bit too magical, making it difficult to reason about, and often requiring acrobatic setup to get the subject to work out correctly. Also, I run my specs with --format documentation
, and the auto-generated message is never what I want. In this case, I'd want to say something like it 'has a signout link'
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