go-mocket icon indicating copy to clipboard operation
go-mocket copied to clipboard

API Improvement

Open reisraff opened this issue 5 years ago • 0 comments

I'd like to propose a new API, take a look at this example:

import "testing"
import mocket "github.com/selvatico/go-mocket"
import "app/app/entity"

func TestDelete(t *testing.T) {
    var expectedId int = 1
    notification := entity.Notification{}
    notification.SetId(expectedId)

    notificationService := GetNotificationService()

    definitions := map[string]interface{}{
        "type": "update",
        "table": "notifications",
        "set": map[string]interface{}{"deleted_at": mocket.Any},
        "where": map[string]interface{}{"id": expectedId},
    }
    query := mocket.NewQueryMock(definitions) // create the query mock
    mocket.WatchQuery(query) // start the watcher

    notificationService.Delete(&notification) // Trigger query

    // assertion, it can be made of two ways

    // it can be:
    mocket.assertWasCalled(query, t) // assert it was called

    // or:
    if mocket.assertWasCalled(query) == false {
        t.Fail()
    }
}

I think this is more readable, and easy to write.

reisraff avatar Mar 27 '19 13:03 reisraff