Skip to content

Commit

Permalink
doc: update readme
Browse files Browse the repository at this point in the history
Signed-off-by: charankamarapu <[email protected]>
  • Loading branch information
charankamarapu committed Sep 7, 2023
1 parent 4894715 commit 69548bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,36 @@ These mocks/stubs are realistic and frees you up from writing them manually. Kep

```go
import(
"github.com/keploy/go-sdk/keploy"
"github.com/keploy/go-sdk/mock"
"github.com/keploy/go-sdk/v2/keploy"
)

// Inside your unit test
...
ctx := mock.NewContext(mock.Config{
err := keploy.New(keploy.Config{
Mode: keploy.MODE_RECORD, // It can be MODE_TEST or MODE_OFF. Default is MODE_TEST. Default MODE_TEST
TestSuite: "<test_suite_name>" // TestSuite name to record the mock or test the mocks
Path: "<local_path_for_saving_test_suite>", // optional. It can be relative(./internals) or absolute(/users/xyz/...)
Name: "<stub_name/mock_name>" // TestSuite name to record the mock or test the mocks
Path: "<local_path_for_saving_mock>", // optional. It can be relative(./internals) or absolute(/users/xyz/...)
EnableKeployLogs: false, // optional. It can be true or false. If it is true keploy logs will be shown in the unit test terminal. Default: false
delay: 10, // by default it is 5 . This delay is for running keploy
})
...
```

At the end of the test case you can add the following function which will terminate keploy if not keploy will be running even after unit test is run

```go
mock.KillProcessOnPort()
keploy.KillProcessOnPort()
```

3. **Mock**: To mock dependency as per the content of the generated file (during testing) - just set the `Mode` config to `keploy.MODE_TEST` eg:

```go
ctx := mock.NewContext(mock.Config{
err := keploy.New(keploy.Config{
Mode: keploy.MODE_TEST,
TestSuite: "<test_suite_name>"
Path: "<local_path_for_saving_test_suite>",
Name: "<stub_name/mock_name>"
Path: "<local_path_for_saving_mock>",
EnableKeployLogs: false,
delay: 10,
})
```

Expand All @@ -75,17 +76,19 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/keploy/go-sdk/keploy"
"github.com/keploy/go-sdk/v2/mock"
"github.com/keploy/go-sdk/v2/keploy"
)

func setup() {
mock.NewContext(mock.Config{
TestSuite: "test-set-5",
Mode: keploy.MODE_RECORD,
func setup(t *testing.T) {
err := keploy.New(keploy.Config{
Name: "test-set-5",
Mode: keploy.MODE_TEST,
Path: "/home/ubuntu/dont_touch/samples-go/gin-mongo",
EnableKeployLogs: true,
EnableKeployLogs: false,
})
if err != nil {
t.Fatalf("error while running keploy: %v", err)
}
dbName, collection := "keploy", "url-shortener"
client, err := New("localhost:27017", dbName)
if err != nil {
Expand All @@ -96,7 +99,7 @@ func setup() {
}

func TestGetURL(t *testing.T) {
setup()
setup(t)
// Setting up Gin and routes
r := gin.Default()
r.GET("/:param", getURL)
Expand All @@ -114,10 +117,10 @@ func TestGetURL(t *testing.T) {

// We're just checking if it can successfully redirect
if w.Code != http.StatusSeeOther {
t.Fatalf("Expected HTTP 303 See Other, but got %v", w.Code)
t.Fatalf("Expcd HTTP 303 See Other, but got %v", w.Code)
}

mock.KillProcessOnPort()
keploy.KillProcessOnPort()

}

Expand All @@ -132,7 +135,7 @@ func TestPutURL(t *testing.T) {
}
payload, err := json.Marshal(data)
if err != nil {
t.Fatalf("rre: %v\n", err)
t.Fatalf("rrdfe: %v\n", err)
}

req, err := http.NewRequest(http.MethodPost, "/url", bytes.NewBuffer(payload))
Expand Down
8 changes: 6 additions & 2 deletions keploy/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func New(conf Config) error {
return errors.New("provided keploy mode is invalid, either use MODE_RECORD/MODE_TEST/MODE_OFF")
}

if conf.Delay > 5 {
delay = conf.Delay
}

if mode == MODE_OFF {
return nil
}
Expand Down Expand Up @@ -90,8 +94,8 @@ func New(conf Config) error {

appPid := os.Getpid()

recordCmd := "sudo -E /usr/local/bin/keploy mockRecord --pid " + strconv.Itoa(appPid) + " --path " + path + " --delay 5" + " --mockName " + conf.Name
testCmd := "sudo -E /usr/local/bin/keploy mockTest --pid " + strconv.Itoa(appPid) + " --path " + path + " --delay 5" + " --mockName " + conf.Name
recordCmd := "sudo -E /usr/local/bin/keploy mockRecord --pid " + strconv.Itoa(appPid) + " --path " + path + " --mockName " + conf.Name
testCmd := "sudo -E /usr/local/bin/keploy mockTest --pid " + strconv.Itoa(appPid) + " --path " + path + " --mockName " + conf.Name

if mode == MODE_TEST {
keployCmd = testCmd
Expand Down

0 comments on commit 69548bd

Please sign in to comment.