Skip to content
gqlxj1987's Blog
Go back

clean architecture on golang

Edit page

原文链接

As we know the constraint before designing the Clean Architecture are :

  1. Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.
  2. Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
  3. Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.
  4. Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.
  5. Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.

service部分的单测写法

package usecase_test

import (
	"errors"
	"strconv"
	"testing"

	"github.com/bxcodec/faker"
	models "github.com/bxcodec/go-clean-arch/article"
	"github.com/bxcodec/go-clean-arch/article/repository/mocks"
	ucase "github.com/bxcodec/go-clean-arch/article/usecase"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

func TestFetch(t *testing.T) {
	mockArticleRepo := new(mocks.ArticleRepository)
	var mockArticle models.Article
	err := faker.FakeData(&mockArticle)
	assert.NoError(t, err)

	mockListArtilce := make([]*models.Article, 0)
	mockListArtilce = append(mockListArtilce, &mockArticle)
	mockArticleRepo.On("Fetch", mock.AnythingOfType("string"), mock.AnythingOfType("int64")).Return(mockListArtilce, nil)
	u := ucase.NewArticleUsecase(mockArticleRepo)
	num := int64(1)
	cursor := "12"
	list, nextCursor, err := u.Fetch(cursor, num)
	cursorExpected := strconv.Itoa(int(mockArticle.ID))
	assert.Equal(t, cursorExpected, nextCursor)
	assert.NotEmpty(t, nextCursor)
	assert.NoError(t, err)
	assert.Len(t, list, len(mockListArtilce))

	mockArticleRepo.AssertCalled(t, "Fetch", mock.AnythingOfType("string"), mock.AnythingOfType("int64"))

}

controller端的单测写法

func TestGetByID(t *testing.T) {
     var mockArticle models.Article 
     err := faker.FakeData(&mockArticle) 
     assert.NoError(t, err) 
     mockUCase := new(mocks.ArticleUsecase) 
     num := int(mockArticle.ID) 
     mockUCase.On(“GetByID”, int64(num)).Return(&mockArticle, nil) 
     e := echo.New() 
     req, err := http.NewRequest(echo.GET, “/article/+  
                 strconv.Itoa(int(num)), strings.NewReader(“”)) 
     assert.NoError(t, err) 
     rec := httptest.NewRecorder() 
     c := e.NewContext(req, rec) 
     c.SetPath(“article/:id”) 
     c.SetParamNames(“id”) 
     c.SetParamValues(strconv.Itoa(num)) 
     handler:= articleHttp.ArticleHandler{
                AUsecase: mockUCase,
                Helper: httpHelper.HttpHelper{}
     } 
     handler.GetByID(c) 
     assert.Equal(t, http.StatusOK, rec.Code) 
     mockUCase.AssertCalled(t, “GetByID”, int64(num))
}

Edit page
Share this post on:

Previous Post
data traffic control in apache airflow
Next Post
dockerise scala and akka