One option is to adjust the method to take an io.ReadCloser instead of a
path so you could pass in anything that supported the io.ReadCloser
interface (which is easily adapted to via ioutil.NoCloser(io.Reader)).

Another option is to generate a temporary file with data you want to test
with and pass that in.

Which one to use (or both for that matter) depend on many external factors.

On Tue, Sep 27, 2016 at 9:37 AM <[email protected]> wrote:

> Hello
>
> may be it is a trivial question but I could not find any usable answer to
> the following requirement therefore I thought that
> surely some of you have the answer
>
> Let's assume I have some code like follows:
>
> func (obj *myObjStruct) MyFn(fsPath string) (myRetType, error) {
>     cpath := path.Join(fsPath, "subdir", "input.txt")
>     fh, err := os.Open(cpath)
>     if err != nil {
>         log.Error(fmt.Sprintf("Got error %#v", err))
>         return nil, err
>     }
>     defer fh.Close()
>     scanner := bufio.NewScanner(fh)
>     for scanner.Scan() {
>         inLine := scanner.Text()
>         ...
>
>
> How should I proceed to mock the os.Open and scanner.xxx calls so that I
> can exercise the remaining parts of the code (... above)
>
> May be I need to rewrite this function a little bit before being able to
> do so but I obviously would not agree to lose the benefits of defering
> fh.Close() call
>
> Thanks for any advice/pointer/...
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to