How to mock one of the struct methods? - unit-testing

I have
type Service struct {
config *config.Config
model *model.DB
gates *config.Gates
rdb *redis.Client
}
where
type DB struct {
*sql.DB
}
How to mock one of the struct methods? Without touching the other methods.
func (e *Service) FindGateErrorId(gate, errorCode, errorMessage string, eventObj model.EventObj) (string, error) {
if errorFormat, err := e.model.GetServiceParam(eventObj.ServiceId, "error_format", eventObj.IsTest); err != nil {
return "", err
//some code
}

Make the type that you want to mock implement an interface. Then wherever you use that type replace the variable to that type with a variable to the interface that the type implements. Now any type implementing that interface can be swapped in. To mock just one method, but not the others wrap the non-mock type with the mock. Let all other implemented methods in the mock simply call the wrapped type. For the method you want to mock implement the logic you desire.
type Animal interface {
Say()
Eat()
}
type Cat struct {}
func (c Cat) Say() {
fmt.Println("meow")
}
func (c Cat) Eat() {
fmt.Println("yum cat food meow")
}
type FunkyCatMock struct {
wrapped Cat
}
func (m FunkyCatMock) Say() {
fmt.Println("woof")
}
func (m FunkyCatMock) Eat() {
m.wrapped.Eat()
}
func main() {
var cat Animal
var mock Animal
cat = Cat{}
mock = FunkyCatMock {
wrapped: Cat{},
}
cat.Say()
cat.Eat()
mock.Say()
mock.Eat()
}
Another thing to keep in mind is that if you find that you are putting many methods into one interface you should consider how you could break that interface into smaller ones. This would be more idiomatic Go.
For example:
type Writer interface {
Write(p []byte) (n int, err error)
}
Go uses structural typing which is similar to duck typing in that if it looks like a duck and it quacks like a duck then it must be a duck.
The reason I bring this up is that it may be the case that you could define a very narrow interface and not have to worry about the mock implementing the other methods.

Related

golang interfaces for testing

I am trying to create a database mock in my code, then i am introducing interfaces to my code, to create the mock:
This is my code (I don't know if it's the correct approach)
package interfaces
type ObjectAPI interface {
FindSomethingInDatabase(ctx context.Context, name string) (e.Response, error)
}
And my implementation of the interface is:
package repositories
func FindSomethingInDatabase(ctx context.Context, name string) (e.Response, error) {
statement, err := db.SqlStatementWithCtx(ctx,
`SELECT *
FROM table
WHERE name = ? LIMIT 1`)
row := statement.QueryRowContext(ctx, name)
if err != nil {
return e.Response{}, err
}
statement.Close()
return toResponse(row), nil //this method convert row database to e.Response struct
}
Now I need call from one method the implementation of my FindSomethingInDatabase, then i am receiving an object type interface:
func CallImplementation(request *dto.XRequest, repo i.ObjectAPI) dto.XResponse{
result := repo.FindSomethingInDatabase(request.Ctx, request.Name)
// more code
}
But now I don't know how can I call CallImplementation` to pass an object with the implementation.
Call the method passing the implementation of the interface
An interface describes a type. Since your FindSomethingInDatabase implementation is just a func without a receiver, there is no type that implements interface ObjectAPI.
You can pass a value of type func(ctx context.Context, name string) (e.Response, error) into CallImplementation as a callback and get rid of the interface altogether. Alternatively, keep the interface, define a type, and make that type the receiver for your current FindSomethingInDatabase implementation. You can then pass the type to CallImplementation, since it will now implement the ObjectAPI interface. An example of the latter (which would be my preferred option for extensibility):
type database struct {}
func (d *database) FindSomethingInDatabase(ctx context.Context, name string) (e.Response, error) {
// ...
}
func CallImplementation(request *dto.XRequest, repo i.ObjectAPI) dto.XResponse{
result := repo.FindSomethingInDatabase(request.Ctx, request.Name)
// more code
}
func main() {
db := &database{}
_ = Callimplementation(getRequest(), db)
}
In this case, you will probably want to store db as a member of database, rather than having it as a global variable (which appears to be the case now).
refer mockery. it provides auto generation of mocks for interfaces and can be good reference about best practices for mocking.
typically you would do this:
api.go:
type API interface {
Something(ctx context.Context, name string) (e.Response, error)
}
type ApiImpl struct {
}
func (t *ApiImpl) Something(ctx context.Context, name string) (e.Response, error) {
// impl
}
api_test.go
// mocks can be hand coded or autogenerated using mockery
type MockImpl struct {
}
func (m *MockImpl) Something(ctx context.Context, name string) (e.Response, error) {
// mock implementation
}
func TestSomething(t * testing.T) {
// test code with mocks
}

How do I mock a function from another package without using dependency injection?

Somewhat of a golang beginner, but I've worked with testing frameworks before. How do I go about mocking out and faking what a dependent method returns without injecting the dependency? The reason why I don't want to use dependency injection is because there are many external package methods that are being used and injecting all of the methods in the constructor is unwieldy.
I've searched for this online/stackoverflow and the solution is to always use dependency injection. Sometimes that is not a viable option.
Here's what I'm trying to do code-wise:
b/b_test.go
package b
func TestResults(t *testing.T) {
t.Run("Test", func(t *testing.T) {
b := NewB()
// How do I mock out and fake a.DoSomething() to be
// "complete" instead of whats in the code right now?
result = b.Results()
assert.Equal(t, "complete", result)
}
}
b/b.go
package b
import "a"
type B struct {}
func NewB() B {
return &B{}
}
func (b B) Results() {
return a.DoSomething()
}
a/a.go
package a
func DoSomething() {
return "done"
}
Thanks!
You can use conditional compilation with build tags
a/a.go
// +build !mock
package a
func DoSomething() {
return "done"
}
a/a_mock.go
// +build mock
package a
func DoSomething() { // Insert fake implementation here
return "complete"
}
$ go test -tags mock
One way to do so would be to create a variable with the function you want to call, so include the following in b/b.go:
var doSomething = a.DoSomething
func (b B) Results() {
return doSomething()
}
Now in b_test.go you can do this:
func TestPrintResults(t *testing.T) {
origDoSomething := doSomething
defer func() { doSomething = origDoSomething }
doSomething = func() {
// Insert fake implementation here
}
b := NewB()
result = b.Results()
assert.Equal(t, "complete", result)
}
I'm not sure if I don't understand your objection to dependency injection, but interfaces can be used to make dependency injection relatively painless. In particular existing code does not need to be modified.
You could try aliasing the package name to a global variable that implements an interface that matches the external package's functions. This has the advantage of not requiring inline changes where package "a" is used.
The idea revolves around creating an interface for the functions you need from the external package, a pass-through implementation of that interface for the default behavior and a mock implementation for testing. At the beginning of a test just replace the global variable with the mock.
b/a_interface.go
package b
import (
aa "a" // alias the "a" package
)
// global variable that mimics the external package "a"
var a aType
// internal interface for `a` package functions (i.e. `DoSomething()`)
type aDoer interface {
DoSomething() string
}
// default implementation of the aDoer interface
type aType struct{}
func (aType) DoSomething() string {
// just pass-through to package "a"
return aa.DoSomething()
}
b/b.go - is unmodified other then removing the import:
package b
type B struct{}
func NewB() B {
return B{}
}
func (b B) Results() string{
// now `a` is a global variable not a package.
return a.DoSomething()
}
b/b_test.go
package b
import (
"testing"
"github.com/stretchr/testify/assert"
)
// mock implementation of aDoer interface
type aMock struct{}
func (aMock) DoSomething() string {
return "complete"
}
func TestResults(t *testing.T) {
a = aMock{} // <- replace the default with the mock
b := NewB()
result = b.Results()
assert.Equal(t, "complete", result)
}
It's a bit on the sneaky side, so you'll probably want to make clear comments about what's going on.

How to properly test a handler that calls another function inside of it

I am looking to test the PostUser function that looks something like this (error handling omitted for simplicity):
func PostUser(env *Env, w http.ResponseWriter, req *http.Request) error {
decoder := json.NewDecoder(req.Body)
decoder.Decode(&user)
if len(user.Username) < 2 || len(user.Username) > 30 {
return StatusError{400, errors.New("usernames need to be more than 2 characters and less than 30 characters")}
}
emailRe := regexp.MustCompile(`^[a-z0-9._%+\-]+#[a-z0-9.\-]+\.[a-z]{2,4}$`)
if !emailRe.MatchString(user.Email) {
return StatusError{400, errors.New("invalid email address")}
}
if len(user.Password) < 8 {
return StatusError{400, errors.New("passwords need to be more at least 8 characters")}
}
hashedPassword,_ := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
env.DB.InsertUser(user.Username, hashedPassword, user.Email) // need to mock this out
userData,_ := json.Marshal(user)
defer req.Body.Close()
w.Write(userData)
return nil
}
My env.go file looks like this:
type Env struct {
DB *db.DB
}
My db.go file looks like this:
type DB struct {
Session *mgo.Session
}
How do I mock the InsertUser call by my DB struct, so that I can unit test the PostUser?
To use mocks for testing you need to create an interface that your mock can implement. Naturally, the struct you're replacing the mock with also needs to implement all the methods of the interface so that they are freely interchangeable.
For example, you could have an interface:
type DBInterface interface {
InsertUser(string, string, string)
//all other methods on the DB struct here
}
Then your DB struct already implements all the methods of the interface. From there you can create a mock struct that also implements the interface.
type DBMock struct {}
func (dbm *DBMock) InsertUser(username, password, email string) {
//whatever mock functionality you want here
return
}
//all other methods also implemented.
Then you can alter env to have a pointer to a DBInterface instead of a DB. When you setup the env to be passed into the handler, in the production version use the DB struct and in testing use the DBMock struct.

How to mock single function in Swift?

My question is simple, how to mock a function (not a method) in Swift.
i.e., a standalone function not inside of a class.
Thank you.
EDIT:
Let's say I have the following function:
func distance(c1: CLLocation, c2: CLLocation) {
...
}
And I want to test my class:
class MyClass {
func selectedLocation(location: CLLocation) {
let text = "\(distance(self.currentLocation, location)) meters"
self.view.showText(text)
}
}
How do I mock the distance function?
To mock the distance function, you would need to do something like this
func distance(c1: CLLocation, c2: CLLocation) -> CLLocationDistance {
// ...
}
class MyClass {
var calculateDistance = distance
func selectedLocation(location: CLLocation) {
let text = "\(calculateDistance(self.currentLocation, location)) meters"
self.view.showText(text)
}
}
And in your test code you would need to do this:
func testCalculateDistanceFromLocation() {
let thing = MyClass()
thing.calculateDistance = { c1, c2 in /* return mock distance here */ }
// assert correct text appeared in the view
}
This way you are providing a new implementation of the distance function when in a testing environment. As far as I know you cannot completely replace the body of a top level function dynamically such that you don't need the internal class property that stores that function value.
This is kind of cumbersome though to do this for all your functions, so I say to only do this when you feel you absolutely need to substitute this extra mocked dependency. If possible, I would encourage you to test your class as a wnole unit, if it has few or no other external dependencies and treat the distance function as an implementation detail.
Do not know if I understand this correct. Swift does support global functions.
[update: This is what I do in the unit test]
public func getNumber()->Int //add public for unit testing
{
return 1
}
class MyClass: NSObject
{
func calculate()
{
let num = getNumber()
println(num)
}
}
///unit test case
import MyModule
extension NSObject
{
public fund getNumber()->Int
{
return 5 //mock implementation
}
}
func testExample() {
let myInstance = MyClass()
myInstance.calculate()
}

Golang: Replace function unit testing

I'm working with Golang, and currently I'm doing some fun unit test with Testify, my file look like this
type myStruct struct {
field_1 string
}
func (self *myStruct) writeFirst() {
//doing something
//modify field_1
self.writeSecond()
}
func (self *myStruct) writeSecond() {
//doing something
}
In this case I'm testing writeFirst() but I'm trying to replace writeSecond() because it is using http stuff that I don't want to use because it access to internet.
I think that use a second struct and set myStruct as anonymous field will be the solution, but it's not working because me second struct and myStruct have a diferent context.
In this case I can't use mocks cause writeSecond is a method of the struct.
My test case looks like this:
func TestWriteFirst(t *testing.T) {
myStc := myStruct{}
assert.Equal(t,"My response", myStc.field_1)
}
All that I want is testing writeFirst without pass to writeSecond()
To illustrate the kind of refactoring mentioned by Not-a-Golfer in the comments, you could consider calling your second function only on an instance that is an interface:
type F2er interface {
Func2()
}
type S struct{ _f2 F2er }
var s = &S{}
func (s *S) f2() F2er {
if s._f2 == nil {
return s
}
return s._f2
}
func (s *S) Func1() {
fmt.Println("s.Func1")
s.f2().Func2()
}
Here: Func1 calls Func2 on s.f2(), not directly s.
If nothing has been set in s, s.f2() returns... itself: s
if s._f2 was replaced by any other struct which implements Func2, s.f2() returns that instance instead of itself.
See a complete example in this playground script.
Output:
TestFunc1
s.Func1
s.Func2
TestFunc1bis
s.Func1
testS.Func2 <=== different Func2 call