Open vox-vox-vox opened 2 years ago
You should not enable the real networking mode if the requests you are testing are not valid network reachable hosts unless you want to explicitly test that scenario. Simply ignore the EnableNetworking()
call.
now I have to test a func
,consists of 4 http call,let‘s call them A
,B
,C
,D
.
A
and D
are real http call, B
and C
are the requests that I want to mock. I don't want the mock in B
,C
to influence the real http request in A
,D
, and I can not separate them since I need to test the whole func
what should I do in this condition with out EnableNetworking()
thanks~
You can enable the real networking mode per specific mock instance instead of enabling it globally: https://github.com/h2non/gock/blob/master/request.go#L294
hi @h2non, in the example you provided, i saw this defer
statement.
func main() {
defer gock.Disable()
defer gock.DisableNetworking()
srv := startHTTPServer()
defer srv.Close()
// Register our local server
gock.New(srv.URL).
EnableNetworking()
and i have two questions:
gock.Disable()
? should i add it for every test case or only on TearDownSuite
? im worried if i put it on every test cases, it will interfere with other test case not that im sure if its possible tho.
type MySuite {
suite.Suite
}
func TestMySuite(t *testing.T) {suite.Run(t, new(MySuite)}
func (s *MySuite) SetupSuite {}
func (s *MySuite) TearDownSuite {}
func (s *MySuite) TestCaseOne {}
func (s *MySuite) TestCaseTwo {}
func (s *MySuite) TestCaseThree {}
2. why do we need call `defer gock.DisableNetworking()` that given that we only enable networking for the particular request only, unless we enable global `gock.EnableNetworking()`, then it makes sense for me. is there any reason for this?
Thanks.
In my project , I have to use
gock.EnableNetworking()
so that the real HTTP request will be requested if the mock can not be matched.However, in this case, the url in
gock.New(...)
has to be a real url. If I use a random url such as "http://123456/bar" , an error will happened :GET http://123456/bar Get "http://123456/bar": dial tcp 0.1.226.64:80: connect: no route to host
the whole code is as follow