async testing with Gingko and Gomega

1.1k Views Asked by At

I have expanded the following code as per my requirement. https://github.com/radovskyb/watcher/blob/master/example/basics/main.go

Purpose of this code is to watch of certain events (CREATE, WRITE) and take appropriate action based on that. To test this code I am using Ginkgo and Gomega and this is where I am looking for help.

I am trying to test below peace of code (Also provided in link) and I am not sure how should I proceed.

    go func() {
        for {
            select {
            case event := <-w.Event:
                fmt.Println(event) // Print the event's info.
            case err := <-w.Error:
                log.Fatalln(err)
            case <-w.Closed:
                return
            }
        }
    }()

I checked, Gomega has support for async testing which supports "Channel" but I tried couple of things but I ended up initialising new watcher object which require actual event to be triggered for testing. I tried to create "tempDir" so I can trigger CRETE / WRITE event and then close the channel but its not helping and after running this particular test, it just wait for channel to be closed.

                w := watcher.New()
                w.FilterOps(watcher.Create, watcher.Write)

                dir, err := ioutil.TempDir("", "watcher2")
                Expect(err).To(BeNil())

                defer os.RemoveAll(dir)
                tmpfile := filepath.Join(dir, "watcherremoved")

                err = ioutil.WriteFile(tmpfile, []byte("init"), 0666)
                Expect(err).To(BeNil())

                event := <-w.Event
                Eventually(event.Path).Should(Equal(ADMIN_VAULT))
            
                <-w.Closed
1

There are 1 best solutions below

0
On

There are few types of tests

Unit testing

This method tests a single piece of code. For example function or even part of the function.
Perspective: Function user(a developer)

Behavioral testing

This is just black box testing, you testing behavior of your entire functionality (sometimes part of it).
Perspective: Module user

End to end testing

This is testing entire program workflow.
Perspective: Program User


Of course there is much more tests types, but those 3 will show you a basic difference.

First two tests types need to have some module, that can be reusable in the code. So this mean, you can use this module in real production code and in the tests scenario.

Testing looks like:

Write test -> Run tested function(pass argument and grab result) -> verify result.


The third one is just program you compile and start it, go thru all steps and verify if they are ok.

Your code is very bad in terms of testing. You cannot reuse any part of the code, because you have everything in the main function.

So testing procedure looks like:

Compile program -> run program (with some params) -> interact with program -> grab output -> analyze collected output.


So solution to test your code is to refactor it. There is very long journey ahead of you to learn writing testable code.

To improve your skills you can look to some open-source code.

But my advice is, you can buy this book and write it. This will show you basics of the testable code. Book is amazing and this is classic in IT world.

Book:

Test Driven Development 
   by Kent Beck 
ISBN 13: 9780321146533
ISBN 10: 0321146530