Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec custom matchers in Cucumber to DRY implementation-dependent tests, is it possible?

I'm reading the new version of "Rails Tutorial" by Michael Hartl and, since I'm pretty fond in BDD with Cucumber, I found myself concerned about what the author points out here: http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#sec:rspec_custom_matchers

In few words the main hassle with Cucumber is that it's impossible to DRY implementation-dependent tests like this:

Then /^he should see an error message$/ do
  page.should have_selector('div.alert.alert-error', text: 'Invalid')
end

writing RSpec custom matchers like this:

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: message)
  end
end 

Because such a custom matcher must be put in spec/support/utilities.rb and can be called from RSpec integration tests but not from Cucumber step definitions.

Are you positive / what you think about it?

Thank you.

like image 735
Darme Avatar asked Feb 22 '23 02:02

Darme


1 Answers

You can certainly create RSpec matchers and use them in your Cucumber steps - I do this fairly frequently. I just place them in features/support/matchers and they are instantly available to use in my step definitions.

If you wanted to share them with your RSpec tests as well, you may want to extract them to a separate shared_test type location, and then you could require that folder in both your Cucumber env.rb file, and your RSpec spec_helper.rb file, then they would be available in both test frameworks.

like image 142
Jon M Avatar answered Apr 26 '23 05:04

Jon M