prashantv / gostub Goto Github PK
View Code? Open in Web Editor NEWgostub is a library to make stubbing in unit tests easy
License: MIT License
gostub is a library to make stubbing in unit tests easy
License: MIT License
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.
../../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.
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
gostub helps me a lot. I really wanna see it in avelino/awesome-go: A curated list of awesome Go frameworks, libraries and software
Hello~ community maintainer:
the community has not released a version for a long time, can you release a version?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.