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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:42:58+00:00 2026-06-14T02:42:58+00:00

I recently asked how to test in RSpec if a block was called and

  • 0

I recently asked how to test in RSpec if a block was called and the answers to that question seem to work in a simple case. The problem is when the initialization with the block is more complex. Then it is done in before and reused by a number of different tests in the context, among them the one testing if the block was evaluated. See the example:

context "the node definition using block of code" do
  before do
     @n=node do
        # this block should be called
     end
     # some more complex setup concerning @n
  end

  it "should call the block" do
     # how to test it?
  end

  # here a bunch of other tests using @n

end

In this case the solution with side effect changing value of a local variable does not work. Raising an exception from the block is useless since the whole statement must be properly evaluated to be used by the other tests.

Obviously I could do the tests separately, but it seems to stink, since I must copy-paste the initialization part and since the was-the-block-called test inherently belongs to this very context.

How to test if the block was evaluated in such a case?


Explanation for question asked by @zetetic below.

The context is that I’m implementing a kind of DSL, with nodes defined by their parameters and blocks of code (that can define something else in the scope of node). Since the things defined by the node’s block can be pretty generic, at least for the first attempt I just need to be sure the block is evaluated and that what a user provides there will be considered. For now does not matter what it is.

Probably I should refactor my tests now and using mocks make them test behaviors rather then implementation. However it will be a little bit tricky, for the sake of some mixins and dynamic handling of messages sent to objects. For now the cincept of such tests is a little bit fuzzy in my head 😉

Anyway your answers and comments helped me to better understand how RSpec works and explained why what I’m trying to do looks as if it did not fit to the RSpec.

  • 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-14T02:42:59+00:00Added an answer on June 14, 2026 at 2:42 am

    Try something like this (untested by me):

    context "the node definition using block of code" do
      let(:node){
        node = Node.new "arg1", "arg2", node_block
        # more complex stuff here
        node
      }
      context "checking the block is called" do
        let(:node_block) {
          double = double("node_block")
          double.should_receive("some kind of arg").and_return("something")
          # this will now cause a fail if it isn't called
          double
        }
        it "should call the block" do
          node.blah()
        end
      end
    
      let(:node_block) {
        # some real code
      }
    
      subject { node.blah() }
      it { should == 2 }
      # ...
    end
    

    So that’s a very shaky piece of code (you’ll have to fill in the gaps as you didn’t give very much to go on, and let is obviously a lambda too, which could mean you’ve got to play around with it a bit) that uses let and a double to check it’s called, and avoids using before, which is really for side effects not setting up variables for use in the specs.

    @zetetic makes a very insightful comment that you’re not testing behaviour here. I’m not against using rspec for doing more unit test style stuff (guidelines are made to be broken), but you might ask how later tests will pass when using a real block of code if that block isn’t being called? In a way, I’m not even sure you need to check the block is called, but only you know.

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

Sidebar

Related Questions

I was asked this question recently in a test, which I didn't pass. For
Recently, I asked (and answered) a question on StackOverflow about why a unit test
I recently asked how to solve a simple SQL query . Turns out that
I recently asked this question here and got some great answers! Custom SQL GROUP
I recently asked a question about getting a specific Rewrite to work . I
I recently asked a question about IIf vs. If and found out that there
I recently asked a question about what I called method calls. The answer referred
I recently asked a question about synchronization issues among the threads of a block
I recently asked a question about tracing Linq-to-Entities I think that one of the
I recently asked a question about formatting JavaScript code in Vim. And I've also

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.