Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:44:51+00:00 2026-06-15T08:44:51+00:00

I had an issue today with defining custom RSpec matchers that I resolved, but

  • 0

I had an issue today with defining custom RSpec matchers that I resolved, but couldn’t actually see any reasoning behind why one of the approaches works and the other doesn’t, here’s the code:

Approach 1 — if + else:

RSpec::Matchers.define :have_success_message do |message|
  match do |page|
    if message.nil?
      page.should have_selector('div.alert.alert-success')
    else
      page.should have_selector('div.alert.alert-success', text: message)
    end
  end
end

Approach 2 — if followed by unless

RSpec::Matchers.define :have_success_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-success') if message.nil?
    page.should have_selector('div.alert.alert-success', text: message) unless message.nil?
  end
end

I figure the first approach is better because it checks the condition only once, but nevertheless, the result should be the same, right?

Well, it turns out the tests with the first approach pass, while the tests with the second don’t. I am completely clueless as to why that is and would love if anyone could shed some light on that.

Edit:

Forgot to add the actual tests (with approach 2):

With the following HTML tag present:

<div class="alert alert-success">Profile updated</div>

I run 4 separate tests:

it { should have_success_message } # fails
it { should have_success_message('Profile updated') } # passes
it { should have_selector('div.alert.alert-success') } # passes
it { should have_selector('div.alert.alert-success', text: "Profile updated") } # passes

The failure is with following message:

1) User pages edit with valid information 
 Failure/Error: it { should have_success_message }
   expected #<Capybara::Session> to have success message
 # ./spec/requests/user_pages_spec.rb:80:in `block (5 levels) in <top (required)>'

When the HTML tag is not present, all 4 tests fail.

Edit 2:

I’ve tried another approach to verify whether control flow is correct:

Approach 3:

if message.nil?
  puts "In if, message is: #{message.inspect}"
  page.should(have_selector('div.alert.alert-success'))
end
unless message.nil?
  puts "In unless, message is: #{message.inspect}"
  page.should(have_selector('div.alert.alert-success', text: message))
end

With this approach the behavior is the same as with approach 2 – first test fails, following 3 pass.

Output is the following:

In if, message is: nil
In unless, message is: “Profile updated”

So the control flow looks alright, but

page.should(have_selector('div.alert.alert-success'))

fails, even though it passes outside the matcher. This truly is a mystery.

Final edit:

Just in response to the approved answer – when I switch the code like this:

page.should have_selector('div.alert.alert-success', text: message) unless message.nil? 
page.should have_selector('div.alert.alert-success') if message.nil?

The tests look like this:

it { should have_success_message } # passes
it { should have_success_message('Profile updated') } # fails
it { should have_selector('div.alert.alert-success') } # passes
it { should have_selector('div.alert.alert-success', text: "Profile updated") } # passes

So I think that indeed the last line, when it’s not true, gets evaluated to nil, and that causes the whole mess. The first approach is better anyway, but I’m glad I have this problem out of my mind 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:44:53+00:00Added an answer on June 15, 2026 at 8:44 am

    This is correct behavior for RSpec, even though it seems unexpected.

    Consider this code:

    x = nil
    "foo" if x.nil?
    "bar" unless x.nil?
    #=> 
    "foo"
    nil
    

    The ...unless statement returns nil when the condition is falsey.

    In your custom matcher, the ...unless statement returns nil when your message is nil.

    That’s the last line in your match block, so your match block returns nil.

    Then RSpec sees your match block return nil, which RSpec considers the same as false, so RSpec reports your custom matcher failed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had an issue with my game that allowed people to easily fake their
I've had an issue that I thought was tinyMCE, then my db, now I
Today I discovered that when I added album artwork to iTunes, it had only
Today I ran into an issue with LINQ to objects (not SQL) that popped
Background: today, an issue arose while using SQL Server Reporting Services. It seems that
So I encountered a strange issue today - I had a simple creation of
Im getting issue with redirect_uri, but everything is set up properly. I had left
I've done this hundreds of times before without issue, but today I've got a
Earlier today I had an issue with my server and it crashed so I
Today I was debugging an issue a customer had remotely, and rather than build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.