RSpec custom matches in implementation-dependent Cucumber tests for DRY, is this possible?

I am reading the new version of Michael Hartle's Rails Tutorial, and since I really love BDD with Cucumber, I was worried that the author points here: http://ruby.railstutorial.org/chapters/sign-in-sign-out? version = 3.2 # sec: rspec_custom_matchers

In a few words, the main problem with Cucumber is that it is not possible to execute implementation-dependent DRYs, such as:

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

writing custom RSpec matches as follows:

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

Since such a custom connector must be placed in spec / support / utilities.rb and can be called from RSpec integration tests, but not from the Cucumber step definition.

Are you sure you are thinking about this?

Thanks.

+4
source share
3 answers

You can use implementation-specific methods or reusable locators in Cucumber World.

An example for your scenario:

 # step_definitions/general_steps.rb Then /^he should see an error message "(.+)"$/ do |text| within(error_message) do page.should have_content(text) end end # support/general_helpers.rb module GeneralHelpers def error_message page.first('div.alert.alert-error') end end World(GeneralHelpers) 

Here are some articles that relate to this approach:

+3
source

You can, of course, create RSpec matches and use them in your Cucumber steps - I do this quite often. I just put them in features/support/matchers and they are instantly available for use in my step definitions.

If you want to share it with your RSpec tests, you can extract them to a separate location like shared_test , and then you can require this folder in both cucumber env.rb files and your RSpec spec_helper.rb file, then they will be available in both test environments.

+5
source

An example of John M's answer on how you can use custom RSpec mappings in Cucumber:

 # spec/support/matchers/http.rb RSpec::Matchers.define :return_http_success do match do |actual| actual >= 200 && actual <= 299 end end # features/support/matchers.rb Dir[Rails.root.join('spec/support/matchers/*.rb')].each { |file| require file } 
0
source

Source: https://habr.com/ru/post/1402248/


All Articles