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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:14:37+00:00 2026-05-22T02:14:37+00:00

In my simplified example of what I’m actually doing, let’s say I have 2

  • 0

In my simplified example of what I’m actually doing, let’s say I have 2 calls to the database:

Repo.add( something_stringy )
Repo.remove( something_floaty )

and I want to use mocks for the database calls, as the real calls will be tested elsewhere:

let(:repo){
  repo = double("Repo")
  repo.should_receive(:add).with(instance_of(String))
  repo.should_receive(:remove).with(instance_of(Float))
  repo
}

before { FakeKlass.const_set :Repo, repo }

that’s all fine and dandy, but now if I wrap the calls in a transaction I’m a bit stumped:

Repo.transaction do
  # ... some error checking in here somewhere...
  Repo.add( something_stringy )
  Repo.remove( something_floaty )
end

because if I write a mock that receives transaction it will receive the call, but everything in the block won’t get called, and I get:

expected: 1 time
received: 0 times

for all of the other mocks. Would anyone be able to show me how I should be writing my spec to deal with this? I’ve tried reading the relevant page in the RSpec book on around(:each) but that was about as clear as mud to me.

Any help is much appreciated.

  • 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-22T02:14:37+00:00Added an answer on May 22, 2026 at 2:14 am

    You can use #and_yield to yield from an expectation chain:

    repo.should_receive( :transaction ).and_yield
    

    You also don’t need to set up a double to stub out methods on your Repo class. For example, your setup could be written:

    before( :each ) do
        Repo.should_receive( :transaction ).and_yield
        Repo.should_receive( :add ).with( instance_of(String) )
        Repo.should_receive( :remove ).with( instance_of(Float) )
    end
    

    You might also consider using stub instead of should_receive, as it doesn’t set up expectations:

    before( :each ) do
        Repo.stub( :transaction ).and_yield
        Repo.stub( :add )
        Repo.stub( :remove )
    end
    

    In general, you should only use should_receive when you want to test the interaction between two objects. My own personal rule of thumb is that if it appears in a before, use stub; if it’s in an example, especially with a specific value, use should_receive.

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

Sidebar

Related Questions

Let's say I have a table like this (this is just a simplified example,
Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
let's say my table schema is like bellow (it's only a simplified example): MyTable
In the simplified example below I have a DataContext and Repository which I think
As a simplified example, I have the following data classes: public class Employee {
Here's a simplified example of an object I have. What I would like to
Simplified example: I have an object that models a user. Users have a first
This is a simplified example of the problem I have: #include <stdio.h> #include <stdlib.h>
[This is a simplified example] I have a collection (myCollection) in which exists three
I have an order entry page and a corresponding ViewModel (simplified example): public class

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.