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 7954435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:25:40+00:00 2026-06-04T03:25:40+00:00

While going through Ruby On Rails Tutorial by Michael Hartl,in the section where the

  • 0

While going through Ruby On Rails Tutorial by Michael Hartl,in the section where the author writes integration test to validate his Signup page, he has used code spinet bellow. I got what the code does but couldn’t get my head around the ‘how’ part i.e. couldn’t understand order of execution.

expect { click_button "Create my account" }.not_to change(User, :count)

Can someone please explain the semantics of the above chain of methods and blocks and how they fit together?

  • 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-04T03:25:42+00:00Added an answer on June 4, 2026 at 3:25 am

    You’d use expect ... change to verify that a particular method call changes — or does not change — some other value. In this case:

    expect { click_button "Create my account" }.not_to change(User, :count)
    

    will cause rspec to do the following:

    1. Run User.count and note the value returned. (This can be specified as a receiver and a method name, like (User, :count) in your example, or as an arbitrary block of code, like { User.count }.
    2. Run click_button "Create my account" which is a Capybara method that simulates a mouse click on a link.
    3. Run User.count again.
    4. Compare the results of #1 and #3. If they differ, the example fails. If they’re the same, it passes.

    Other ways to use expect ... change:

    expect { thing.destroy }.to change(Thing, :count).from(1).to(0)
    expect { thing.tax = 5 }.to change { thing.total_price }.by(5)
    expect { thing.save! }.to raise_error
    expect { thing.symbolize_name }.to change { thing.name }.from(String).to(Symbol)
    

    Some docs are here.

    How it does this is a bit arcane, and it’s not at all necessary to understand how it works in order to use it. A call to expect is defining a structure for rspec to execute, using rspec’s own custom DSL and system of “matchers.” Gary Bernhardt has a rather neat screencast in which he argues that the mystery of rspec actually falls out naturally from a dynamic language like ruby. It’s not a good introduction to using rspec, but if you’re curious about how it all works, you might find it interesting.

    UPDATE

    After seeing your comment on another answer, I’ll add a bit about the order of operations. The unintuitive trick is that it’s the matcher (change in this case) that executes all of the blocks. expect has a lambda, not_to is an alias for should_not whose job is to pass the lambda on to the matcher. The matcher in this case is change which knows to execute its own argument once, then execute the lambda that it was passed (the one from expect), then run its own argument again to see if things changed. It’s tricky because the line looks like it should execute left to right, but since most of the pieces are just passing around blocks of code, they can and do shuffle them into whatever order makes the most sense to the matcher.

    I’m not an expert on the rspec internals, but that’s my understanding of the basic idea.

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

Sidebar

Related Questions

While going through a Zend tutorial , I came across the following statement: Note
While Going through the java tutorial on sun site, I see following piece of
While going through the Intermediate Perl book I noticed a section on Schwartzian Transforms
I have questions on the statements I found while going through hibernate caching tutorial.
While going through many resources on multithreaded programming, reference to volatile specifier usually comes
While going through Effective C++ (by Scott Meyers), I came across the following code
While going through the syslog file, I found a message like shutdown via gdm
While going through the book Scala for the Impatient , I came across this
Yesterday while going through this question, I found a curious case of passing and
i'm using powerplot to create a dynamic graph in iphone. while going through http://www.field-theory.org/articles/powerplot/example.html

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.