Git Product home page Git Product logo

gostub's People

Contributors

billf avatar jeffbean avatar nikolasa avatar prashantv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gostub's Issues

Not able to stub static function

Hi, I am new to GoLang, please pardon me in case this issue turns out to be lame.

Here is what I tried

import (
	"bytes"
	"testing"

	"github.com/prashantv/gostub"
)

// The function to stub
func RandomBytes(l uint8) ([]byte, error) {
	return []byte{1, 2, 3, 4}, nil
}

// The function to test
func PasswordHash(password string) ([]byte, error) {
	return RandomBytes(4)
}

// Test
func TestPasswordHash(t *testing.T) {
	var functionPtr = RandomBytes
	defer gostub.Stub(&functionPtr, func(l uint8) ([]byte, error) {
                println("Stub takes over") // This is never executed
		return []byte{5, 5, 5, 5}, nil
	}).Reset()

	hash, _ := PasswordHash("pass")
	if bytes.Compare(hash, []byte{5, 5, 5, 5}) != 0 {
		t.Error("Hash differs, stub didn't work. RandomSalt=", hash) // Test fails!
	}
}

So basically I am unable to stub the RandomBytes function.

Reproduced in playground

download fail

  1. go get -u -v github.com/prashantv/gostub
    then output is:

../../github.com/prashantv/gostub/env.go:7:15: error: reference to undefined identifier ¡®os.LookupEnv¡¯
v, ok := os.LookupEnv(k)
^
../../github.com/prashantv/gostub/env.go:24:5: error: reference to undefined identifier ¡®os.Unsetenv¡¯
os.Unsetenv(k)
^
../../github.com/prashantv/gostub/env.go:33:7: error: reference to undefined identifier ¡®os.Unsetenv¡¯
os.Unsetenv(k)

what is wrong with it ?

I can use "Stub" in my project , but when i use "go test", it shows error.

Stub a func registered in some package's init()

hi I have a question here how to substitute a func in the init of some package.
One example:

package http
func AuthenticationFilter(ctx context.Context) {
....
}
func init() {
beego.InsertFilter("/
", beego.BeforeExec, AuthenticationFilter)
}

I want to substitute AuthenticationFilter before the http package initialize.
stubs.Stub(&http.AuthenticationFilter, func (ctx *context.Context) {
...
})
How should I do? Thx a lot

can you release a version?

Hello~ community maintainer:
the community has not released a version for a long time, can you release a version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.