github.com/go-kivik/kivik/v4@v4.3.2/mockdb/README.md (about)

     1  [![Go Reference](https://pkg.go.dev/badge/github.com/go-kivik/kivik/v4/x/mockdb.svg)](https://pkg.go.dev/github.com/go-kivik/kivik/v4/x/mockdb)
     2  
     3  # MockDB
     4  
     5  Package **mockdb** is a mock library implementing a Kivik driver.
     6  
     7  This package is heavily influenced by [github.com/DATA-DOG/go-sqlmock](https://github.com/DATA-DOG/go-sqlmock), the SQL mock driver from [Datadog](https://www.datadoghq.com/).
     8  
     9  # Usage
    10  
    11  To use this package, in your `*_test.go` file, create a mock Kivik connection:
    12  
    13      client, mock, err := mockdb.New()
    14      if err != nil {
    15          panic(err)
    16      }
    17  
    18  The returned `client` object is a `*kivik.Client`, and can be passed to your
    19  methods to be tested.   `mock` is used to control the execution of the mock
    20  driver, by setting expectations.  To test a function which fetches a user,
    21  for example, you might do something like this:
    22  
    23      func TestGetUser(t *testing.T) {
    24          client, mock, err := mockdb.New()
    25          if err != nil {
    26              t.Fatal(err)
    27          }
    28  
    29          mock.ExpectDB().WithName("_users").WillReturn(mock.NewDB().
    30              ExpectGet().WithDocID("bob").
    31                  WillReturn(mockdb.DocumentT(t, `{"_id":"org.couchdb.user:bob"}`)),
    32          )
    33          user, err := GetUser(client, "bob")
    34          if err != nil {
    35              t.Error(err)
    36          }
    37          // other validation
    38      }
    39  
    40  # Versions
    41  
    42  This package targets the unstable release of Kivik.
    43  
    44  ## License
    45  
    46  This software is released under the terms of the Apache 2.0 license. See
    47  LICENCE.md, or read the [full license](http://www.apache.org/licenses/LICENSE-2.0).