3

I have been reading Capybara docs and while it is a great tool for BDD, I could not figure out how to reset state between Scenarios.

I have seen some references to Before/After steps, but they seem to apply between each individual step and not scenarios. I was not able to find any definitive documentation on this topic online.

Note: I am not a ruby developer, dabbling just with Capybara/Cucumber stack, so my exposure to ruby technologies is limited and I might be missing something obvious.

Vasily
  • 461
  • 1
  • 6
  • 16

2 Answers2

5

I have seen some references to Before/After steps, but they seem to apply between each individual step and not scenarios

This is not true. Take a look at the documentation:

Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered.

After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps.

As of cleaning up state, 3rd-party gems like database_cleaner might help. But, again, you can clean the state without relying on any gem, using solely Before and After hooks.

DNNX
  • 6,215
  • 2
  • 27
  • 33
0

Regarding state, I answered this other SO post about seeds. Copied here, this yields discrete scenario testing:


There are various thoughts on if you should or should not use seeds for this.

I want to know if each discrete scenario works or does not work, with no interplay between them. This can make the suite take longer, but provides confidence in your testing that another scenario did not cause a chain reaction. Therefore, I choose to use seeds for this.

I have a support/seeds.rb with contents:

Before do |scenario|
  load Rails.root.join('db/seeds.rb')
end

Note, you might want to combine this with something like:

begin
  # start off entire run with with a full truncation
  #  DatabaseCleaner.clean_with :truncation, {:except => %w[plans]}
  DatabaseCleaner.clean_with :truncation
  # continue with the :transaction strategy to be faster while running tests.
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Community
  • 1
  • 1
kross
  • 3,627
  • 2
  • 32
  • 60