示例#1
0
func TestExtractByTypeNoMatch(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		b1 *beta
		g1 *gamma
	)

	graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var a *alpha

	defer ExpectPanic("no defined pointer matches the specified type")
	inject.ExtractByType(graph, &a)
}
示例#2
0
func TestExtractByType(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		a1 *alpha
		b1 *beta
		g1 *gamma
	)

	graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} }))
	graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var a *alpha
	inject.ExtractByType(graph, &a)

	Expect(a).To(Equal(&alpha{name: "a1"}))
}