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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:45:31+00:00 2026-05-16T02:45:31+00:00

I have a text file which I intend to convert to a CSV (in

  • 0

I have a text file which I intend to convert to a CSV (in this case) format. How do I create a proper RSpec test to see if the output will be in the proper format for the following scenario?

This is my code:

class ProcessLog
    @@log = Array.new
    def read_log(log)
        if File.exists?(log) 
            f = File.open(log, "r")
        f.each_line { |line| @@log << line.strip }
        end
    end
    def process_log
       result = Array.new
       @@log.each do |line|
          <convert to csv>
          result << <converted to csv>
       end
       result
    end
end

describe ProcessLog do
    before do
        @pl = ProcessLog.new
        <help>
    end
    it 'should pass first format' do
        text = "Item X has a price of $ per gram" # just as an example
        <help>
        @pl.read_log("file.log")
        @pl.should == 'X,$,gram'
    end
end
  • 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-16T02:45:32+00:00Added an answer on May 16, 2026 at 2:45 am

    First, you’re using a class variable (@@log) when you want an instance variable (@log). But really there’s no reason to use either.

    I would split these methods up like so, so you can test them in isolation:

    • One method that reads a file to an array, and returns the array
    • One method that takes an array and formats them how you want them, returning the formatted array

    Right now your methods are tightly coupled because they’re both using the instance variable, and it makes testing harder than it needs to be (you have to set up or fake the call to the first method to begin testing the second).

    You could test the first method with something like FakeFS to read the contents of a (fake) file. Really though this method should be fairly trivial and you might not have to test it at all.

    The second method gets easier to test when you remove the coupling to the first. Create an array of lines from a file you expect to parse, and pass it to the method, and check the result.

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

Sidebar

Related Questions

I have a text file of this format: L O A D C A
I have to create some sort of text file in which there are numbers
I have a .rc file which is used to include some text data in
I have text file which I want to erase in Python. How do I
I have a text file which all contain the following fragments of code in
I have a text file which does not confirm to standards. So I know
iPad application I have a large text file which I want to divide into
I have a text file with a large amount of data which is tab
i have a text file which contains lots of email addresses from my old
I have a text file on my local machine that is generated by a

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.