Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD with Capybara (Rails); difference between page.should and expect(page).to?

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!

like image 548
milchschaum Avatar asked Jul 19 '13 10:07

milchschaum


1 Answers

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'

like image 72
Joshua Cheek Avatar answered Nov 02 '22 18:11

Joshua Cheek