Files
deep_go/homework/interfaces/homework_test.go

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
// go test -v homework_test.go
type UserService struct {
// not need to implement
NotEmptyStruct bool
}
type MessageService struct {
// not need to implement
NotEmptyStruct bool
}
type Container struct {
// need to implement
}
func NewContainer() *Container {
// need to implement
return &Container{}
}
func (c *Container) RegisterType(name string, constructor interface{}) {
// need to implement
}
func (c *Container) Resolve(name string) (interface{}, error) {
// need to implement
return nil, nil
}
func TestDIContainer(t *testing.T) {
container := NewContainer()
container.RegisterType("UserService", func() interface{} {
return &UserService{}
})
container.RegisterType("MessageService", func() interface{} {
return &MessageService{}
})
userService1, err := container.Resolve("UserService")
assert.NoError(t, err)
userService2, err := container.Resolve("UserService")
assert.NoError(t, err)
u1 := userService1.(*UserService)
u2 := userService2.(*UserService)
assert.False(t, u1 == u2)
messageService, err := container.Resolve("MessageService")
assert.NoError(t, err)
assert.NotNil(t, messageService)
paymentService, err := container.Resolve("PaymentService")
assert.Error(t, err)
assert.Nil(t, paymentService)
}