Question

I wrote a feature spec and that tests when a user signs up. I've got it working, but I feel like the expect block is quite weird.

feature 'Visitor signs up' do
  scenario 'with a valid email and password' do
     visit new_account_path

     fill_in_account_fields
     fill_in_user_fields('valid@example.com', 'foobar123')

     expect{click_button 'Sign up'}.to change{Account.count}.by(1) and
                                       change{User.count}.by(1)
  end
end

In particular:

expect{click_button 'Sign up'}.to change{Account.count}.by(1) and
                                  change{User.count}.by(1)

Is there a better way of doing this? Do you think I'm approaching this the right way?

Was it helpful?

Solution

Hmm I don't know about this. Since this is an integration test I would just put what the user can see. For example I'd put `expect(page).to have_content("Hello user"). Then you can make a unit test to the controller or model that changes the User.count by 1.

You want to do outside-in testing. Test features with the Integration test (simulate the user literally filling in stuff and clicking) while testing the web-appy things with unit tests.

  scenario 'with a valid email and password' do
     visit new_account_path    
     fill_in_account_fields
     fill_in_user_fields('valid@example.com', 'foobar123')
     click_button 'Sign Up'

     expect(page).to have_content 'Hi Guy'
  end

Check these out. The Everyday Rails book is worth every penny.

http://everydayrails.com/2014/01/15/outside-in-example-ruby-tapas.html

This is the screencast from the talk:

http://www.rubytapas.com/episodes/120-Outside-In

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top