0

I have a scenario test:

scenario 'displays the form' do
  # Build the setup for user coming from an external site.
  expect(page).to have_selector('#braintree-dropin-frame', count: 1)

  # User reloads the page
  expect(page).to have_selector('#braintree-dropin-frame', count: 1)

  # User visits page from within the website.
  expect(page).to have_selector('#braintree-dropin-frame', count: 1)
end

First off, is this the proper usage of a scenario test? I'm essentially testing the same thing but in different scenarios. I feel like this should really be three separate scenario tests inside a context block. Am I misusing scenario?

thank_you
  • 11,001
  • 19
  • 101
  • 185
  • you might want to add some more context, how is `page` set, for example? – Kris Sep 16 '16 at 17:15
  • `page` is just the html of the page. It's provided by Capybara I believe. My main concern with this test is that I'm running different scenarios within one scenario test. At the same time, I'm testing for the same thing, "Does #braintree-dropin-frame exist on the page?" – thank_you Sep 16 '16 at 17:18
  • I don't understand how any of the three tests differ, aren't the testing the same thing on the same page? – Kris Sep 16 '16 at 17:19
  • If this isn't one user flow then they should be separate features/scenarios. Additionally since you're using a CSS selector here you probably want to be using `have_css` rather than `have_selector` here – Thomas Walpole Sep 16 '16 at 17:20
  • @Tom, that's what I'm looking for. Post as an answer and I'll give you credit. – thank_you Sep 16 '16 at 17:21

1 Answers1

1

If this isn't one user flow then they should be separate features/scenarios. You also need to be careful when setting expectations for the same selector multiple times in a test that you've checked it has gone away between (if you are checking it reappears again), otherwise you can end up with tests passing when they shouldn't due to timing and asynchronous behavior issues.

Additionally - since you're checking for a CSS selector you probably want to be using have_css rather than have_selector since it reads nicer and will mean the tests keep working if the default selector type is ever changed from :css

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78