4

I want to write unit tests in golang for a struct which accepts an io.Reader in the constructor. Usually the io.Reader interface is coming from a TCP connection.

Now I want to use a predefined string and use this as input to the io.Reader interface.

Something like:

s := "this is my input"
b := io.NewReader(s)
t := NewTestStruct(b)
t.doSomething()

2 Answers 2

6

strings.Reader implements the io.Reader interface. You can construct a new instance of it using strings.NewReader:

s := "this is my input"
b := strings.NewReader(s)
t := NewTestStruct(b)
t.doSomething()
Sign up to request clarification or add additional context in comments.

Comments

5

This should be correct way:

reader := bufio.NewReader(strings.NewReader("some string"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.