Trying to use regex in RSpec has_selector line

I am new to Ruby / Rails / RSpec and hopefully this is a dumb / simple question. I am currently working on writing RSpec tests for our current project. I am currently writing a test to display the last time / date a page was entered.

What I want to do is just check if the date / time is present in the correct format. I use regular expression to check formatting. I create a regular expression and then try to use the string must be selector so (please excuse the ugly regular expression) -

it "should display last login date and time" do date_time_regex = /Last Login: [0-9][0-9]-[0-9][0-9]-[0-9][0-9], [0-9][0-9]:[0-9[0-9]]/ visit root_path response.should have_selector("date_time", :content => date_time_regex) end 

When I try to run the test, I get the following error:

 1) LayoutLinks when signed in should display last login date and time Failure/Error: response.should have_selector("date_time", :content => date_time_regex) NoMethodError: undefined method `include?' for #<Regexp:0xef2a40> # ./layout_links_spec.rb:60:in `block (3 levels) in <top (required)>' 

So it looks like I can't pass in the regex, but I'm not sure what to do next? Thanks in advance for your help!

+4
source share
2 answers
 date_time_regex = /Last Login: [0-9][0-9]-[0-9][0-9]-[0-9][0-9], [0-9][0-9]:[0-9[0-9]]/ response.should have_selector('data_time') do |data_time| data_time.should contain(date_time_regex) end 

or...

 response.should have_selector('data_time') do |data_time| data_time.should =~ date_time_regex end 
+7
source

One thing that will help you is that you can do \d instead of [0-9] . So you get

 /Last Login: \d\d-\d\d-\d\d, \d\d:\d\d/ 

which is more readable.

+4
source

All Articles