{"id":13694134,"url":"https://github.com/prashantv/gostub","last_synced_at":"2025-05-15T09:04:41.715Z","repository":{"id":34472609,"uuid":"38409973","full_name":"prashantv/gostub","owner":"prashantv","description":"gostub is a library to make stubbing in unit tests easy","archived":false,"fork":false,"pushed_at":"2025-01-09T04:03:58.000Z","size":35,"stargazers_count":316,"open_issues_count":0,"forks_count":13,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-07T03:17:42.337Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prashantv.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-07-02T03:51:35.000Z","updated_at":"2025-03-07T10:48:00.000Z","dependencies_parsed_at":"2024-01-13T22:59:51.573Z","dependency_job_id":"dfab445f-5ea9-412b-9b7a-464145aac1d0","html_url":"https://github.com/prashantv/gostub","commit_stats":{"total_commits":37,"total_committers":6,"mean_commits":6.166666666666667,"dds":"0.29729729729729726","last_synced_commit":"9529e671b031c4a693f2d4c7861efbd3ba9152e5"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashantv%2Fgostub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashantv%2Fgostub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashantv%2Fgostub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashantv%2Fgostub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prashantv","download_url":"https://codeload.github.com/prashantv/gostub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248890810,"owners_count":21178511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T17:01:25.121Z","updated_at":"2025-04-14T13:48:08.261Z","avatar_url":"https://github.com/prashantv.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library"],"sub_categories":["测试","Test"],"readme":"# gostub\n\n[![Build Status](https://travis-ci.org/prashantv/gostub.svg?branch=master)](https://travis-ci.org/prashantv/gostub)\n[![GoDoc](https://godoc.org/github.com/prashantv/gostub?status.svg)](https://godoc.org/github.com/prashantv/gostub)\n[![Coverage Status](https://coveralls.io/repos/github/prashantv/gostub/badge.svg?branch=master)](https://coveralls.io/github/prashantv/gostub?branch=master)\n\ngostub is a library to make stubbing in unit tests easy.\n\n## Getting started\n\nImport the following package:\n`github.com/prashantv/gostub`\n\nClick [here](https://godoc.org/github.com/prashantv/gostub) to read the [API documentation](https://godoc.org/github.com/prashantv/gostub).\n\n## Package overview\n\nPackage gostub is used for stubbing variables in tests, and resetting the\noriginal value once the test has been run.\n\nThis can be used to stub static variables as well as static functions. To stub a\nstatic variable, use the Stub function:\n\n```go\nvar configFile = \"config.json\"\n\nfunc GetConfig() ([]byte, error) {\n    return ioutil.ReadFile(configFile)\n}\n\n// Test code\nstubs := gostub.Stub(\u0026configFile, \"/tmp/test.config\")\n\ndata, err := GetConfig()\n// data will now return contents of the /tmp/test.config file\n```\n\ngostub can also stub static functions in a test by using a variable to reference\nthe static function, and using that local variable to call the static function:\n\n```go\nvar timeNow = time.Now\n\nfunc GetDate() int {\n    return timeNow().Day()\n}\n```\n\nYou can test this by using gostub to stub the timeNow variable:\n\n```go\nstubs := gostub.Stub(\u0026timeNow, func() time.Time {\n    return time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC)\n})\ndefer stubs.Reset()\n\n// Test can check that GetDate returns 6\n```\n\nIf you are stubbing a function to return a constant value like in the above\ntest, you can use StubFunc instead:\n\n```go\nstubs := gostub.StubFunc(\u0026timeNow, time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC))\ndefer stubs.Reset()\n```\n\nStubFunc can also be used to stub functions that return multiple values:\n\n```go\nvar osHostname = osHostname\n// [...] production code using osHostname to call it.\n\n// Test code:\nstubs := gostub.StubFunc(\u0026osHostname, \"fakehost\", nil)\ndefer stubs.Reset()\n```\nStubEnv can be used to setup environment variables for tests, and the\nenvironment values are reset to their original values upon Reset:\n\n```go\nstubs := gostub.New()\nstubs.SetEnv(\"GOSTUB_VAR\", \"test_value\")\ndefer stubs.Reset()\n```\n\nThe Reset method should be deferred to run at the end of the test to reset all\nstubbed variables back to their original values.\n\nYou can set up multiple stubs by calling Stub again:\n\n```go\nstubs := gostub.Stub(\u0026v1, 1)\nstubs.Stub(\u0026v2, 2)\ndefer stubs.Reset()\n```\n\nFor simple cases where you are only setting up simple stubs, you can condense\nthe setup and cleanup into a single line:\n\n```go\ndefer gostub.Stub(\u0026v1, 1).Stub(\u0026v2, 2).Reset()\n```\n\nThis sets up the stubs and then defers the Reset call.\n\nYou should keep the return argument from the Stub call if you need to change\nstubs or add more stubs during test execution:\n\n```go\nstubs := gostub.Stub(\u0026v1, 1)\ndefer stubs.Reset()\n\n// Do some testing\nstubs.Stub(\u0026v1, 5)\n\n// More testing\nstubs.Stub(\u0026b2, 6)\n```\n\nThe Stub call must be passed a pointer to the variable that should be stubbed,\nand a value which can be assigned to the variable.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashantv%2Fgostub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprashantv%2Fgostub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashantv%2Fgostub/lists"}