Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 1.9% on proxy.golang.org
Top 0.3% dependent packages on proxy.golang.org
Top 0.3% dependent repos on proxy.golang.org
Top 4.2% forks on proxy.golang.org
Top 1.1% docker downloads on proxy.golang.org

proxy.golang.org : github.com/rafaeljusto/redigomock

Package redigomock is a mock for redigo library (redis client) Redigomock basically register the commands with the expected results in a internal global variable. When the command is executed via Conn interface, the mock will look to this global variable to retrieve the corresponding result. To start a mocked connection just do the following: Now you can inject it whenever your system needs a redigo.Conn because it satisfies all interface requirements. Before running your tests you need beyond of mocking the connection, registering the expected results. For that you can generate commands with the expected results. As the Expect method from Command receives anything (interface{}), another method was created to easy map the result to your structure. For that use ExpectMap: You should also test the error cases, and you can do it in the same way of a normal result. Sometimes you will want to register a command regardless the arguments, and you can do it with the method GenericCommand (mainly with the HMSET). All commands are registered in a global variable, so they will be there until all your test cases ends. So for good practice in test writing you should in the beginning of each test case clear the mock states. Let's see a full test example. Imagine a Person structure and a function that pick up this person in Redis using redigo library (file person.go): Now we need to test it, so let's create the corresponding test with redigomock (fileperson_test.go): When you use redis as a persistent list, then you might want to call the same redis command multiple times. For example: To test it, you can chain redis responses. Let's write a test case: In the first iteration of the loop redigomock would return "www.some.url.com", then "www.another.url.com" and finally redis.ErrNil. Sometimes providing expected arguments to redigomock at compile time could be too constraining. Let's imagine you use redis hash sets to store some data, along with the timestamp of the last data update. Let's expand our Person struct: And add a function updating personal data (phone number for example). Please notice that the update timestamp can't be determined at compile time: Unit test: As you can see at the position of current timestamp redigomock is told to match AnyInt struct created by NewAnyInt() method. AnyInt struct will match any integer passed to redigomock from the tested method. Please see fuzzyMatch.go file for more details.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/rafaeljusto/redigomock
License: GPL-2.0
Latest release: almost 4 years ago
First release: over 4 years ago
Namespace: github.com/rafaeljusto
Dependent packages: 143
Dependent repositories: 915
Stars: 198 on GitHub
Forks: 29 on GitHub
Docker dependents: 4
Docker downloads: 759
See more repository details: repos.ecosyste.ms
Funding links: https://github.com/sponsors/rafaeljusto
Last synced: 4 days ago

    Loading...
    Readme
    Loading...