Context("when the type is not assignable to the expected type", func() {
				It("should return an error", func() {
					parameter := reflect.ValueOf("@foo")
					expectedType := reflect.TypeOf(NewBar())

					result, err := resolver.Resolve(parameter, expectedType)
					Expect(result.IsValid()).To(BeFalse())
					Expect(err).To(MatchError(`the referenced type "@foo" (type *goldi_test.Foo) is not assignable to the expected type *goldi_test.Bar`))
					Expect(err.(goldi.TypeReferenceError).TypeID).To(Equal("foo"))
				})
			})

			Context("when a func reference type is requested", func() {
				It("should generate the type and return the function", func() {
					foo := &Foo{Value: "Success!"}
					container.InjectInstance("foo", foo)

					parameter := reflect.ValueOf("@foo::ReturnString")
					expectedType := reflect.TypeOf(func(string) string { return "" })

					result, err := resolver.Resolve(parameter, expectedType)
					Expect(err).NotTo(HaveOccurred())
					Expect(result.Interface()).To(BeAssignableToTypeOf(foo.ReturnString))
					Expect(result.Interface().(func(string) string)("YEAH")).To(Equal("Success! YEAH"))
				})

				It("should return an error if the method does not exist", func() {
					container.InjectInstance("foo", new(Foo))

					parameter := reflect.ValueOf("@foo::ThisMethodDoesNotExist")
					expectedType := reflect.TypeOf(func() {})
Beispiel #2
0
					Expect(typeDef).NotTo(BeNil())

					generatedType, err := typeDef.Generate(resolver)
					Expect(err).NotTo(HaveOccurred())
					Expect(generatedType).To(BeAssignableToTypeOf(&MockType{}))

					generatedMock := generatedType.(*MockType)
					Expect(generatedMock.BoolParameter).To(BeTrue())
					Expect(generatedMock.StringParameter).To(Equal("1, two, drei"))
				})
			})

			Context("when a func reference type is given", func() {
				It("should generate the type", func() {
					foo := &MockType{StringParameter: "Success!"}
					container.InjectInstance("foo", foo)
					typeDef := goldi.NewType(NewMockTypeFromStringFunc, "YEAH", "@foo::ReturnString")

					generatedType, err := typeDef.Generate(resolver)
					Expect(err).NotTo(HaveOccurred())
					Expect(generatedType).To(BeAssignableToTypeOf(NewMockType()))
					Expect(generatedType.(*MockType).StringParameter).To(Equal("Success! YEAH"))
				})
			})

			Context("when a func reference type is given as variadic argument", func() {
				It("should generate the type", func() {
					foo := &MockType{StringParameter: "Success!"}
					container.InjectInstance("foo", foo)
					typeDef := goldi.NewType(NewVariadicMockTypeFuncs, "@foo::ReturnString", "@foo::ReturnString")
	BeforeEach(func() {
		container = goldi.NewContainer(goldi.NewTypeRegistry(), map[string]interface{}{})
	})

	Describe("Configure", func() {
		Context("when the type configurator has not been defined correctly", func() {
			It("should return an error", func() {
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Configure")
				Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator type "@configurator" has not been defined`))
			})
		})

		Context("when the type configurator is no struct or pointer to struct", func() {
			It("should return an error", func() {
				container.InjectInstance("configurator", 42)
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Configure")
				Expect(configurator.Configure(someType, container)).To(MatchError("the configurator instance is no struct or pointer to struct but a int"))
			})
		})

		Context("when the type configurator method does not exist", func() {
			It("should return an error", func() {
				someType := new(Foo)
				configurator := goldi.NewTypeConfigurator("configurator", "Fooobar")
				container.Register("configurator", goldi.NewInstanceType(&MyConfigurator{}))

				Expect(configurator.Configure(someType, container)).To(MatchError(`the configurator does not have a method "Fooobar"`))
			})
		})