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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:03:15+00:00 2026-06-17T18:03:15+00:00

I have a question on how to use rspec to mock a file input.

  • 0

I have a question on how to use rspec to mock a file input. I have a following code for the class, but not exactly know a why to mock a file input. filepath is /path/to/the/file

I did my search on Google and usually turns out to be loading the actual file instead of mocking, but I’m actually looking the opposite where only mock, but not using the actual file.

module Service
  class Signing
    def initialize(filepath)
      @config = YAML.load_file(filepath)
      raise "Missing config file." if @config.nil?
    end

    def sign()      …
    end

    private
    def which()     …
    end

  end 
end

Is it possible to use EOF delimiter for this file input mocking?

file =  <<EOF
A_NAME: ABC
A_ALIAS: my_alias
EOF
  • 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-17T18:03:17+00:00Added an answer on June 17, 2026 at 6:03 pm

    You could stub out YAML.load_file and return parsed YAML from your text, like this:

    yaml_text = <<-EOF
      A_NAME: ABC
      A_ALIAS: my_alias
    EOF
    yaml = YAML.load(yaml_text)
    filepath = "bogus_filename.yml"
    YAML.stub(:load_file).with(filepath).and_return(yaml)
    

    This doesn’t quite stub out the file load itself, but to do that you’d have to make assumptions about what YAML.load_file does under the covers, and that’s not a good idea. Since it’s safe to assume that the YAML implementation is already tested, you can use the code above to replace the entire call with your parsed-from-text fixture.

    If you want to test that the correct filename is passed to load_file, replace the stub with an expectation:

    YAML.should_receive(:load_file).with(filepath).and_return(yaml)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have question from programming pearls problem is following show how to use Lomuto's
i am want to use startMonitoringSignificantLocationChanges in my app,but i have some question,i hope
I always use the code below to get all the informations But I have
After reading this , I still have a question : I use this code
I have question regarding the use of function parameters. In the past I have
I have a question regarding use of FFT. Using function getBand(int i) with Minim
i have an question how to use an alias column at the where/having clause.
I have a question about the use of multidimensional std::intializer_list in C++. I have
i have a question how to use an alias from a select query: $timecheck
I have a question about the use of data structures such as ArrayLists in

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.