Skip to content

Commit

Permalink
mock文件转移到统一文件
Browse files Browse the repository at this point in the history
  • Loading branch information
fuming committed Jul 7, 2020
1 parent 25da55b commit bdf614d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
*coverprofile
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ install:
- go get github.com/mattn/goveralls

script: # 集成脚本
- overalls -project=$GOPATH/src/github.com/nanjingfm/bdd_demo -covermode=count -ignore='.git,_vendor'
- overalls -project=$GOPATH/src/github.com/nanjingfm/bdd_demo -covermode=count -ignore='mock,.git,_vendor'
- goveralls -coverprofile=overalls.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN
- go test -race -coverprofile=coverage.txt -covermode=atomic # 注意要添加这一行,否则不会出现 codecov bot
- go test ./... -coverpkg=./...
11 changes: 6 additions & 5 deletions handlers/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers_test
import (
"errors"
"order/handlers"
"order/mock"
"order/models"
"order/services"
"testing"
Expand Down Expand Up @@ -36,7 +37,7 @@ var _ = Describe("FindOrdersForUser", func() {
})
Describe("with an invalid ID", func() {
BeforeEach(func() {
mockContext := handlers.NewMockContext(ctrl)
mockContext := mock.NewMockContext(ctrl)
mockContext.EXPECT().Param(gomock.Eq("id")).Return("invalid_id")
mockContext.EXPECT().Status(gomock.Eq(400))
c = mockContext
Expand All @@ -50,12 +51,12 @@ var _ = Describe("FindOrdersForUser", func() {
Describe("with a valid ID", func() {
Describe("when an error is returned from the OrderService", func() {
BeforeEach(func() {
mockContext := handlers.NewMockContext(ctrl)
mockContext := mock.NewMockContext(ctrl)
mockContext.EXPECT().Param(gomock.Eq("id")).Return("5")
mockContext.EXPECT().Status(gomock.Eq(500))
c = mockContext

mockOrderService := services.NewMockOrderService(ctrl)
mockOrderService := mock.NewMockOrderService(ctrl)
mockOrderService.EXPECT().FindAllOrderByUserID(gomock.Eq(5)).Return(nil, errors.New("some error"))
orderService = mockOrderService
})
Expand All @@ -76,12 +77,12 @@ var _ = Describe("FindOrdersForUser", func() {
},
})

mockContext := handlers.NewMockContext(ctrl)
mockContext := mock.NewMockContext(ctrl)
mockContext.EXPECT().Param(gomock.Eq("id")).Return("5")
mockContext.EXPECT().JSON(gomock.Eq(200), gomock.Eq(orders))
c = mockContext

mockOrderService := services.NewMockOrderService(ctrl)
mockOrderService := mock.NewMockOrderService(ctrl)
mockOrderService.EXPECT().FindAllOrderByUserID(gomock.Eq(5)).Return(orders, error(nil))
orderService = mockOrderService
})
Expand Down
2 changes: 1 addition & 1 deletion clients/restaurant/mock_client.go → mock/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion handlers/mock_context.go → mock/mock_context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services_test

import (
"order/clients/restaurant"
"order/mock"
"order/models"
"order/repositories"
"order/services"
Expand Down Expand Up @@ -43,7 +44,7 @@ var _ = Describe("Services", func() {
Describe("FindAllOrdersByUserID", func() {
Describe("with no records in the database", func() {
BeforeEach(func() {
orderRepoMock := repositories.NewMockOrderRepository(ctrl)
orderRepoMock := mock.NewMockOrderRepository(ctrl)
orderRepoMock.EXPECT().FindAllOrdersByUserID(gomock.Eq(userID))
orderRepo = orderRepoMock
})
Expand Down Expand Up @@ -72,15 +73,15 @@ var _ = Describe("Services", func() {
RestaurantID: 9,
PlacedAt: time.Now().Add(-36 * time.Hour),
}
orderRepoMock := repositories.NewMockOrderRepository(ctrl)
orderRepoMock := mock.NewMockOrderRepository(ctrl)
orderRepoMock.EXPECT().FindAllOrdersByUserID(gomock.Eq(userID)).
Return(models.Orders{order1, order2}, error(nil))
orderRepo = orderRepoMock
})

Describe("when not all Restaurants can be found", func() {
BeforeEach(func() {
restaurantClientMock := restaurant.NewMockClient(ctrl)
restaurantClientMock := mock.NewMockClient(ctrl)
restaurantClientMock.EXPECT().
GetRestaurantsByIDs([]int{8, 9}).
Return(models.Restaurants{}, error(nil))
Expand All @@ -104,7 +105,7 @@ var _ = Describe("Services", func() {
ID: 8,
Name: "KFC",
}
restaurantClientMock := restaurant.NewMockClient(ctrl)
restaurantClientMock := mock.NewMockClient(ctrl)
restaurantClientMock.EXPECT().
GetRestaurantsByIDs([]int{8, 9}).
Return(models.Restaurants{restaurant1, restaurant2}, nil)
Expand Down

0 comments on commit bdf614d

Please sign in to comment.