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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:46:31+00:00 2026-06-16T16:46:31+00:00

I have a silly queue-class[1] with the following method, that I want to spec

  • 0

I have a silly “queue-class[1]” with the following method, that I want to spec out with Rspec. I am not interested in testing if writing to the file-system works (It works, my computer works) but in whether or not the correct data gets written away.

def write transaction
  File.open("messages/#{@next_id}", "w") {|f| f.puts transaction }
  @next_id += 1
end

The spec for testing this is:

describe TransactionQueue do
  context "#write" do
    it "should write positive values" do
      open_file = mock File
      open_file.stub(:puts)

      File.any_instance.stub(:open).and_yield(open_file)
      File.any_instance.should_receive(:open)
      open_file.should_receive(:puts).with("+100")

      @queue = TransactionQueue.new
      @queue.write("+100")
    end
  end
end

Running this, fails, because my Mocks never receive the expected “open” and “puts” messages.

Can I mock File this way? Did I use the any_instance correctly; is my attempt to stub a “block-yield” correct?

I’d rather not use extra gems like FakeFS when it can be avoided; this is not so much about getting it to work; bu mostly about actually understanding what is going on. Hence my attempt to avoid extra gems/layers of complexity.

[1] Class is from The Cucumber Book; but these tests have littel to do with Cucumber itself. I somehow broke the code when following the book; and want to find out what, by writing unit-tests for the parts that the book does not write tests for: the helper classes.

  • 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-16T16:46:32+00:00Added an answer on June 16, 2026 at 4:46 pm

    It’s not “any instance” of the File class that you expect to receive the open method; it’s the File class itself:

      File.stub(:open).and_yield(open_file)
      File.should_receive(:open)
    

    Furthermore, don’t use both a stub and an expectation. If you want to verify that File.open is actually called:

      File.should_receive(:open).and_yield(open_file)
    

    If you merely want to stub the open method in case it gets called, but don’t want to require it as behaviour of the @queue.write method:

      File.stub(:open).and_yield(open_file)
    

    (This is from memory, I haven’t used RSpec for a few months.)

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

Sidebar

Related Questions

I have a silly, little class FileSystemSize which can be used both as an
I have a silly error that I am unable to resolve. I try to
I have a silly question here. I define a class with many data members,
Did not upgrade anything, nothing should have changed, but I may have a silly
I have a really silly problem that has cost me a load of time
Please excuse (or improve the title) but I have a silly little problem that's
I have a silly problem i haven't been able to figure out. Can anyone
I have a silly question. If I have an array in C++, that I
Suppose I have a number of related classes that all have a method like
I have a silly confusion here. I have the following main() in my code

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.