Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read all content as a string using StringReader.ReadToEnd method
To me my solution seems correct but Unit test does not pass and I could not figure out why. Any help would be appreciated.

When I debugged it I got this error:

Expected: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed ..."
  But was:  "System.IO.StringReader"


What I have tried:

public static string ReadFullContent(StringReader stringReader)
        {
           
            StringReader reader = new StringReader(stringReader.ToString());

            return reader.ReadToEnd();
        }
Posted
Updated 8-Mar-22 8:02am
Comments
Maciej Los 8-Mar-22 12:57pm    
Sorry, but why do you pass StringReader into method and then create new one to return something?
LuckyChloe 8-Mar-22 13:07pm    
OMG. you are right. I overcomplicated everything. it works now. My question can be marked as solved now. Thank You!
Maciej Los 8-Mar-22 13:10pm    
Then... I'd suggest to remove that question or post a solution to it and mark it as an answer.
LuckyChloe 8-Mar-22 14:04pm    
Sure. Done

That's what you get when you call stringReader.ToString() .
 
Share this answer
 
Comments
LuckyChloe 8-Mar-22 14:03pm    
Hi Piebal. Thank you very much for your response.
I realized my mistake and solved it
Might post more questions later if I have difficulty.
public static string ReadFullContent(StringReader stringReader)
        {          
          return stringReader.ReadToEnd();
        }
 
Share this answer
 
v2
Comments
Richard MacCutchan 8-Mar-22 14:14pm    
That will also fail, since reader is not defined.
LuckyChloe 8-Mar-22 14:31pm    
it worked and test passed though 🤔
Richard MacCutchan 8-Mar-22 14:52pm    
Then why are you passing stringReader to this method? And where is reader defined? The fact that this code works does not mean it is correct.

You really need to look more closely at the use and meaning of parameters in methods, and the scope of variables.
LuckyChloe 8-Mar-22 15:26pm    
Can share source code if you like
LuckyChloe 8-Mar-22 15:25pm    
It was like that before I even started working on the exercise. Maybe reader is defined in units tests. I don't know sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900