You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the snippet above, we intentionally skipped assigning to proper variable DB instance. One of the assumptions is that the project has one DB instance at the time, overriding it with FakeDriver will do the job.
30
31
31
-
## Simple Chain Usage
32
+
## Usage
32
33
33
34
***
34
35
36
+
There are two possible ways to use `mocket`:
37
+
38
+
* Chaining API
39
+
* Specifying `FakeResponse` object with all fields manually. Could be useful for cases when mocks stored separately as the list of FakeResponses.
In the example above, we create a new mock via `.NewMock()` and attach a query pattern which will be used to catch a matched query. `.WithReply()` specifies which response will be provided during the mock of this request.
79
90
As `Catcher` is global variable without calling `.Reset()` this mock will be applied to all subsequent tests and queries if the pattern matches.
80
91
81
-
## Usage via `FakeResponse` Object
92
+
###Usage via `FakeResponse` Object
82
93
83
94
We are taking `GetUsers` from the previous example and an example on how it can be using a FakeResponse directly attached to the Catcher object
84
95
85
96
```go
86
97
t.Run("Simple select with direct object", func(t *testing.T) {
87
98
Catcher.Reset()
99
+
Catcher.Logging = true
100
+
// Important: Use database files here (snake_case) and not struct variables (CamelCase)
101
+
// eg: first_name, last_name, date_of_birth NOT FirstName, LastName or DateOfBirth
@@ -105,13 +119,18 @@ t.Run("Simple select with direct object", func(t *testing.T) {
105
119
106
120
## GORM Example
107
121
122
+
***
123
+
108
124
Usage of a mocked GORM is completely transparent. You need to know which query will be generated by GORM and mock them or mock just by using arguments. In this case, you need to pay attention to order of arguments as GORM will not necessarily arrange them in order you provided them.
109
125
110
126
*Tip:* To identify the exact query generated by GORM you can look at the console output when running your mocked DB connection. They show up like this:
@@ -249,18 +274,18 @@ Catcher.Reset().NewMock().WithQuery(`SELECT * FROM "users" WHERE`).WithReply(co
249
274
```
250
275
251
276
### Reply Matching
252
-
When you provide a Reply to Catcher, your field names must match your database model or else, they will not be updated with the right value.
277
+
When you provide a Reply to Catcher, your *field names must match your database model* and NOT the struct object or else, they will not be updated with the right value.
mocket.Catcher.NewMock().OneTime().WithQuery(`SELECT * FROM "dummies"`).WithReply(commonReply)
259
284
260
285
result:=GetUsers(DB)
261
286
```
262
287
263
-
This will work and not error out, but `result` will have a 0 value in the field `userID`. You must make sure to match the Reply fields with the database fields and not the struct fields or else you might bang your head on your keyboard.
288
+
This will seem to work and not error out, but `result` will have a 0 value in the field `userID`. You must make sure to match the Reply fields with the database fields and not the struct fields or else you might bang your head on your keyboard.
Go-Mocket is library inspired by [DATA-DOG/go-sqlmock](https://github.com/DATA-DOG/go-sqlmock)
6
-
As inspiration library, it is the implementation of [sql/driver](https://godoc.org/database/sql/driver) interface but at the same time follows different approaches and has only a similar API.
7
-
This library helps to mock any DB connection also with [jinzhu/gorm](https://github.com/jinzhu/gorm), and it was the main goal to create it
5
+
Go-Mocket is a library inspired by [DATA-DOG/go-sqlmock](https://github.com/DATA-DOG/go-sqlmock).
6
+
7
+
As an inspiration library, it is the implementation of [sql/driver](https://godoc.org/database/sql/driver) interface but at the same time it follows a different approach and only has a similar API.
8
+
This library helps to mock any DB connection with [jinzhu/gorm](https://github.com/jinzhu/gorm), as it was the created to serve this purpose.
8
9
9
10
List of features in the library:
10
11
11
12
* Mock `INSERT`, `UPDATE`, `SELECT`, `DELETE`
12
-
* Support of transactions
13
+
* Support for transactions
13
14
* 2 API's to use - `chaining` and via specifying a whole mock object
14
15
* Matching by prepared statements arguments
15
16
* You don't require to change anything inside your code to start using this library
16
17
* Ability to trigger exceptions
17
-
* Attach callbacks to mocked response to add an additional check or modify a response
18
+
* Attach callbacks to mocked responses to add an additional check or modify a response
18
19
19
-
**NOTE**, Please be aware that driver catches SQL without DB specifics. Generating of queries is done by *SQL* package
20
+
**NOTE**, Please be aware that driver catches SQL without DB specifics. Generation of queries is done by *SQL* package
20
21
21
-
####Install
22
+
## Install
22
23
23
24
```
24
25
go get github.com/Selvatico/go-mocket
25
26
```
26
27
27
-
#### Usage
28
-
29
-
There are two possible ways to use `mocket`:
30
-
31
-
* Chaining API
32
-
* Specifying `FakeResponse` object with all fields manually. Could be useful for cases when mocks stored separately as the list of FakeResponses.
33
-
34
-
##### Enabling driver
35
-
36
-
Somewhere in your code, do this to set up a tests
37
-
38
-
```go
39
-
import (
40
-
"database/sql"
41
-
mocket "github.com/Selvatico/go-mocket"
42
-
"github.com/jinzhu/gorm"
43
-
)
44
-
45
-
funcSetupTests() {
46
-
mocket.Catcher.Register()
47
-
// GORM
48
-
db, err:= gorm.Open(mocket.DriverName, "any_string") // Could be any connection string
49
-
app.DB = db // Assumption that it will be used everywhere the same
0 commit comments