コード例 #1
0
        public void CreateInstanceGeneric_WhenTraitsTypeDoesNotMatchDescriptor_Throws()
        {
            var componentDescriptor = CreateStubComponentDescriptor <DummyService, DummyTraits>();

            var ex = Assert.Throws <ArgumentException>(() => ComponentHandle.CreateInstance <ServiceDescriptor, Traits>(componentDescriptor));

            Assert.Contains(ex.Message, "The component descriptor is not compatible with the requested component handle type because it has a different service type or traits type.");
        }
コード例 #2
0
        public void ToString_ReturnsComponentId()
        {
            var componentDescriptor = CreateStubComponentDescriptor <DummyService, DummyTraits>();

            componentDescriptor.Stub(x => x.ComponentId).Return("componentId");
            var componentHandle = ComponentHandle.CreateInstance <DummyService, DummyTraits>(componentDescriptor);

            Assert.AreEqual("componentId", componentHandle.ToString());
        }
コード例 #3
0
        private static ComponentHandle <TService, TTraits> CreateStubComponentHandle <TService, TTraits>()
            where TTraits : Traits
        {
            var serviceDescriptor = MockRepository.GenerateMock <IServiceDescriptor>();

            serviceDescriptor.Stub(x => x.ResolveServiceType()).Return(typeof(TService));
            serviceDescriptor.Stub(x => x.ResolveTraitsType()).Return(typeof(TTraits));

            var componentDescriptor = MockRepository.GenerateMock <IComponentDescriptor>();

            componentDescriptor.Stub(x => x.Service).Return(serviceDescriptor);
            return(ComponentHandle.CreateInstance <TService, TTraits>(componentDescriptor));
        }
コード例 #4
0
        public void CreateInstanceGeneric_WhenArgumentsValid_ReturnsTypedComponentHandle()
        {
            var componentDescriptor = CreateStubComponentDescriptor <DummyService, DummyTraits>();

            var componentHandle = ComponentHandle.CreateInstance <DummyService, DummyTraits>(componentDescriptor);

            Assert.Multiple(() =>
            {
                Assert.AreSame(componentDescriptor, componentHandle.Descriptor);
                Assert.AreEqual(typeof(DummyService), componentHandle.ServiceType);
                Assert.AreEqual(typeof(DummyTraits), componentHandle.TraitsType);
            });
        }
コード例 #5
0
        public void GetTraitsTyped_WhenComponentCanBeResolved_ReturnsAndMemoizesIt()
        {
            var traits = new DummyTraits();
            var componentDescriptor = CreateStubComponentDescriptor <DummyService, DummyTraits>();
            var componentHandle     = ComponentHandle.CreateInstance <DummyService, DummyTraits>(componentDescriptor);

            componentDescriptor.Expect(x => x.ResolveTraits()).Return(traits);

            // first time
            DummyTraits result = componentHandle.GetTraits();

            Assert.AreSame(traits, result);

            // second time should be same but method only called once
            result = componentHandle.GetTraits();
            Assert.AreSame(traits, result);

            componentDescriptor.VerifyAllExpectations();
        }
コード例 #6
0
        public void GetComponentUntyped_WhenComponentCanBeResolved_ReturnsItAndMemoizesIt()
        {
            var component           = MockRepository.GenerateStub <DummyService>();
            var componentDescriptor = CreateStubComponentDescriptor <DummyService, DummyTraits>();
            var componentHandle     = ComponentHandle.CreateInstance(componentDescriptor);

            componentDescriptor.Expect(x => x.ResolveComponent()).Return(component);

            // first time
            object result = componentHandle.GetComponent();

            Assert.AreSame(component, result);

            // second time should be same but method only called once
            result = componentHandle.GetComponent();
            Assert.AreSame(component, result);

            componentDescriptor.VerifyAllExpectations();
        }
コード例 #7
0
 public void CreateInstanceGeneric_WhenDescriptorIsNull_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => ComponentHandle.CreateInstance <DummyService, DummyTraits>(null));
 }
コード例 #8
0
 public void CreateInstance_WhenDescriptorIsNull_Throws()
 {
     Assert.Throws <ArgumentNullException>(() => ComponentHandle.CreateInstance(null));
 }