Coder Social home page Coder Social logo

Comments (4)

h2non avatar h2non commented on June 13, 2024

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.

from gock.

vox-vox-vox avatar vox-vox-vox commented on June 13, 2024

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~

from gock.

h2non avatar h2non commented on June 13, 2024

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

from gock.

RychEmrycho avatar RychEmrycho commented on June 13, 2024

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:

  1. supposed my SIT looks like this, where should i put the 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 {}
  1. 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.

from gock.

Related Issues (20)

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.