Skip to content

Instantly share code, notes, and snippets.

@dleve123
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dleve123/b5266619e65d8f20fe0b to your computer and use it in GitHub Desktop.
Save dleve123/b5266619e65d8f20fe0b to your computer and use it in GitHub Desktop.
RSpec Regex Match Woes
describe '#users_dropdown' do
subject { helper.users_dropdown(user) }
let(:team) { create(:team) }
let(:user) { create(:user, first_name: 'Current', last_name: 'User', userable: team) }
let(:same_team_user1) { create(:user, first_name: 'John', last_name: 'Friend', userable: team) }
let(:same_company_user2) { create(:user, first_name: 'Jack', last_name: 'Goodfriend') }
let(:other_company_user) { create(:user, first_name: 'Jane', last_name: 'Enemy') }
let(:deactivated_user) do
create(:user, first_name: 'Jason', last_name: 'Friend', userable: team, deactivated: true)
end
let(:authorized_users_set) { [user, same_company_user2, same_team_user1, deactivated_user] }
let(:authorized_users_relation) { User.where(id: authorized_users_set) }
before(:each) do
allow(user).to receive(:authorized_users).and_return(authorized_users_relation)
end
# passes
it "includes a label 'User name' for 'user_id'" do
actual_markup = helper.users_dropdown(user).to_s
expected_label_tag = label_tag(:user_id, 'User name').to_s
expect(actual_markup).to match(expected_label_tag)
end
# passes
it 'includes a select tag with content' do
actual_markup = helper.users_dropdown(user).to_s
expect(actual_markup).to match(/\<select/)
expect(actual_markup).to match(/select\>/)
end
# fails
it 'returns the markup for a select box labeled \'User name\' with ' \
'options for each of the user\'s authorized_users, listed last_name first, ' \
'ordered by last_name' do
actual_markup = helper.users_dropdown(user)
expect(actual_markup).to match(/John.*Jack.*Current/)
end
...
end
[47] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> actual_string
=> "<label for=\"user_id\">User name</label><select id=\"user_id\" name=\"user_id\"><option value=\"\">All Users</option><option value=\"3\">Friend, John</option>\n<option value=\"2\">Goodfriend, Jack</option>\n<option value=\"1\">User, Current</option></select>"
[48] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> actual_string.strip == actual_string
=> true
[49] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> actual_string.match(/John/)
=> #<MatchData "John">
[50] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> actual_string.match(/John.*Jack/)
=> nil
[51] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> actual_string.strip.match(/John.*Jack/)
=> nil
[52] pry(#<RSpec::ExampleGroups::DashboardHelper::UsersDropdown>)> String.new(actual_string).strip.match(/John.*Jack/)
=> nil
http://rubular.com/r/qvliy5nuL1
@dleve123
Copy link
Author

problem here involved the newline characters in the outputted HTML.

possibly more relevant info at: http://stackoverflow.com/questions/5239997/regex-how-to-match-multiple-lines/8075288#8075288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment