New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/larrybotha/learn-go-with-tests/09-mocking

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/larrybotha/learn-go-with-tests/09-mocking

  • v0.0.0-20201207201419-fff6533ead54
  • Source
  • Go
  • Socket score

Version published
Created
Source

Dependency Injection

Table of Contents

  • Takeaways
  • Resources

Takeaways

Go

  • functions that return multiple values define those values in parenthesis:

    func MyFunc() (n int, s string, xs []int) {}
    
  • to implement an interface on a struct, the method must be implemented with a receiver:

    type MyStruct struct {}
    
    // MyStruct implements Write on the io.Writer interface
    func (m MyStruct) Write(p byte[]) (n int, err error) {}
    
  • like sleep in bash, Go has a time.Sleep method which accepts a value of type time.Duration:

    var sleepTime time.Duration = 5 * time.Second
    time.Sleep(sleepTime)
    
  • one can use a struct to encapsulate configurations:

    type CustomSleeper struct {
      duration time.Duration
      sleep func(time.Duration)
    }
    
    func (c CustomSleeper) sleep() {
      c.sleep(c.duration)
    }
    
    func main() {
      sleeper := CustomSleeper{duration: 5, sleep: time.Sleep}
    }
    
  • to implement an interface method, one requires a struct on which to implement the method

  • an interface method can't be defined on an anonymous struct, as interface methods have to be defined using receivers, while functions on structs are equivalent to assigning a function to a variable

  • one can either explicitly pass a value by its address to a function, or initialise the variable with the address:

    buffer := bytes.Buffer{}
    fmt.Fprint(&buffer, "foo")
    
    // or
    bufferAddress = &bytes.Buffer{}
    fmt.Fprint(bufferAddress, "foo")
    
  • a single struct may implement multiple interfaces

Tests

  • a simple mock can be implemented with a struct that mutates an internal property each time the interface it implemnts is called:

    type MySpy struct {
      Calls []string
    }
    
    // create a mock for time.Sleep
    func (m *MySpy) Sleep(duration time.Duration) {
      m.Calls = append(m.Calls, "called")
    }
    

Resources

FAQs

Package last updated on 07 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc