AUP-SE / syllabus

0 stars 0 forks source link

Step definition question #5

Open efbmachine opened 7 years ago

efbmachine commented 7 years ago

Given /^(?:|I )am on (.+)$/ do |page_name| visit path_to(page_name) end

Henry and I were trying to figure out where the "page_name" argument was coming from. Can you help us with this and also explain a little bit about the cucumber step definitions ? Also can you specify what is the function of the path.rb file in the RottenPottatosDemo

shaolintl commented 7 years ago

Hi,

The page_name argument comes from the regular expression. You can see the (.+) group in the expression. What probably happens (I didn't look on the source code of cucumber) is that Given is a function taking a regexp as argument and calling 'yield' (higher-order functions, see slides on ruby) on the groups returned from the regexp.

For example, Given as above will try to match /^(?:|I )am on (.+)$/ with the text in your feature file. If succeeding, then the value after the 'on ' will be returned (it is grouped using parentheses) and be supplied as argument (via the yield mechanism) to do |page_name| visit path_to(page_name) end

if you remember, I mentioned that teaching yield, etc. is mainly done in order to allow you to understand the code of rails (and cucumber, etc). There are many such examples.

The role of step definitions is to translate the English of the feature into Ruby code which is used to test your code.

In the above example, the page_name is extracted from the string and is given as argument to visit path_to. This brings us to the second question: path_to is defined in path.rb and contains the mappings of strings (or regexp) to actual routes.

To summarize:

On 4 November 2016 at 15:08, efbmachine notifications@github.com wrote:

Given /^(?:|I )am on (.+)$/ do |page_name| visit path_to(page_name) end

Henry and I were trying to figure out where the "page_name" argument was coming from. Can you help us with this and also explain a little bit about the cucumber step definitions ? Also can you specify what is the function of the path.rb file in the RottenPottatosDemo

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/AUP-SE/syllabus/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/AFj8Va3RLUUYStyG2RJFf7ZssQcjzxsdks5q6zxfgaJpZM4Kpk4F .