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

  • Home
  • SEARCH
  • 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 7565385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:08:29+00:00 2026-05-30T14:08:29+00:00

I’ve come across some counter intuitive behavior in RSpec (2.8.0) custom matchers functionality and

  • 0

I’ve come across some counter intuitive behavior in RSpec (2.8.0) custom matchers functionality and I wonder whether it is a bug or feature or me being confused. Let’s look at the code:

# matcher code
RSpec::Matchers.define :exist do
  chain :with_start_time do |time|
    @start_time = time
  end

  chain :with_end_time do |time|
    @end_time = time
  end

  match do |subject|
    result = true
    result &&= subject.start_time == @start_time if @start_time
    result &&= subject.end_time == @end_time if @end_time
    result
  end

  failure_message_for_should do |subject|
    "Failure!\n".tap do |msg|
      if @start_time != subject.start_time
        msg << "Expected start_time to be #@start_time but was #{subject.start_time}\n"
      end
      if @end_time != subject.end_time
        msg << "Expected end_time to be #@end_time but was #{subject.end_time}\n"
      end
    end
  end
end

#spec code
require 'ostruct'

describe 'RSpec custom matcher keeping state between tests' do
  let(:time) { Time.now }

  it 'passes the first spec' do
    o = OpenStruct.new(start_time: time)
    o.should exist.with_start_time(time)
  end

  it 'fails the second because matcher kept @start_time from the first test' do
    o = OpenStruct.new(end_time: time)
    o.should exist.with_end_time(time)
  end
end

This fails (demonstrating the issue):

avetia01:~/projects/custom_matcher_bug% rspec test_spec.rb             
.F

Failures:

  1) RSpec custom matcher keeping state between tests fails the second because matcher kept @start_time from the first test
     Failure/Error: o.should exist.with_end_time(time)
       Failure!
       Expected start_time to be 2012-02-27 12:20:25 +0000 but was 
     # ./test_spec.rb:41:in `block (2 levels) in <top (required)>'

Finished in 0.00116 seconds

2 examples, 1 failure

So the unexpected bit is that the same matcher instance seems to be used across multiple specs. Which, in this particular case leads, to @start_time being initialized with the value from the first spec, causing incorrect failure of the second spec.

  • 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-05-30T14:08:30+00:00Added an answer on May 30, 2026 at 2:08 pm

    This has reported and fixed, but not yet released:

    https://github.com/rspec/rspec-expectations/issues/104

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.