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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:37:45+00:00 2026-05-29T12:37:45+00:00

I’ve searched far and wide and I hope someone can answer this question. I’m

  • 0

I’ve searched far and wide and I hope someone can answer this question. I’m using the following code to stub out the ‘exists?’ method for FileTest in an rspec spec:

it "returns no error if file does exist" do
  @loader = MovieLoader.new
  lambda {
    FileTest.stub!(:exists?).and_return(true)
    @loader.load_file
  }.should_not raise_error("File Does Not Exist")
end

What I really want to do is to ensure that the existence of a very specific file is stubbed out. I was hoping something like this would do the job:

it "returns no error if file does exist" do
  @loader = MovieLoader.new
  lambda {
    FileTest.stub!(:exists?).with(MovieLoader.data_file).and_return(true)
    @loader.load_file
  }.should_not raise_error("File Does Not Exist")
end

However, this doesn’t seem to be working. I am having a very difficult time finding documentation on what the ‘with’ method actually does. Perhaps I’m barking up the wrong tree entirely.

Can someone please provide some guidance?

  • 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-29T12:37:48+00:00Added an answer on May 29, 2026 at 12:37 pm

    The RSpec stubbing framework leaves a bit to be desired, and this is one of those things. The stub!(:something).with("a thing") ensures that each time the something method is called that it receives "a thing" as the input. If it receives something other than "a thing", RSpec will stop the test and report an error.

    I think you can achieve what you want, you’ll just have to approach this a little differently. Instead of stubbing out FileTest, you should be stubbing out a method on your @loader instance, and that method would normally call FileTest.exists?. Hopefully this demonstrates what I’m getting at:

    class MovieLoader
      def load_file
        perform_loading if file_exists?(file_path)
      end
    
      def file_exists?(path)
        FileTest.exists? path
      end
    end
    

    Your test would then look like:

    it "returns no error if file does exist" do
      @loader = MovieLoader.new
      lambda {
        @loader.stub!(:file_exists?).with(MovieLoader.data_file).and_return(true)
        @loader.load_file
      }.should_not raise_error("File Does Not Exist")
    end
    

    Now you are only stubbing one instance of the loader, so other instances will not inherit the stubbed version of file_exists?. If you need to be more fine-grained than that, you’ll probably need to use a different stubbing framework, which RSpec supports (stubba, mocha, etc).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.