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 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 which contains something like this: Hello, my name is
I have text file which has text with newline char like this. I read
I have a text file which contains many (over 5000) links. Like this: http://www.website.com/rgfdefvrggh
in my test android app I intend to create and access database file, which
I have a text file which goes like this : 125 126 127 {
I have a text file which has a particular line something like sometext sometext
I have a text file which I am reading and storing the data in
I have a text file which contains data seperated by '|'. I need to
I have a text file which I want to filter using awk. The text
I have a text file which needs to be constantly updated (regular intervals). All

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.