Can I access Autofac's full power in UnitTests using Moq integration

My project (which takes place at the top of Orchard , although I don't think it is relevant) uses Autofac . I write unit tests in which I want to drown out any dependencies using Moq , and I use Autofac / Moq integration to achieve this.

This is great for any simple dependencies that are passed as constructor arguments. (The Autofac documentation contains information on how to achieve this here ).

But since I donโ€™t have a container container, I donโ€™t see how to use a lot of Autofac power - lamda registration, generics registration , marking properties are automatically connected . and etc.

  • Am I mistaken and somehow accessible?
  • Is this material simply unavailable due to some inherent scenario limitation?
  • Is this possible, just not implemented in this Autofac / Moq integration (yet)?
  • Is there any good work?
+6
source share
1 answer

Some of the features you mentioned can be used with the AutoMock class, and some of them will no longer make sense. With AutoMock we do not deal with the ContainerBuilder class - this is for simplicity, we do not want to code all these instructions for registration, but only register the ones of interest to us. Thus, from creating an AutoMock object using the GetLoose or GetStrict method, we only deal with the built-in IContainer object, ready for resolution. Fortunately for us, IContainer still allows us to expand our registration set, but Autofac will not support this with its convenient extension methods, since they are designed to work with the ContainerBuilder object. This is reasonable because for the Autofac perspective, expanding the definitions of an IContainer is something unusual. ContainerBuilder must handle the registration, and IContainer must handle the permission.

Here, as an example, I will tell you how with AutoMock we can use the functionality with auto-installation:

using (var mock = AutoMock.GetLoose()) { mock.Container.ComponentRegistry.Register( RegistrationBuilder .ForType<MyClass>() .PropertiesAutowired() .CreateRegistration<MyClass, ConcreteReflectionActivatorData, SingleRegistrationStyle>() ); } 

As you can see now, we have to deal with all the ugliness of the autofac internal code, which is usually hidden by extension methods. PropertiesAutowired functionality can be easily used with AutoMock, however registering lamda no longer makes sense. What lambda registration gives us is that the object registered in ContainerBuilder may depend on another registered object that will be allowed when creating the ContainerBuilder for IContainer, so we donโ€™t have to worry about the order of the registration instructions. Here we already have a ready-made IContainer, and it will not be built again, so such a registration division is pointless. If we want to transfer another registered object to some registered object, we can first enable it and then use it.

So, as a summary:

  • Autofac functions commonly available with AutoMock
  • some of the functions are not available due to restrictions not to register things in the ContainerBuilder, but in the IContainer object. Other things are probably not yet implemented and may be delivered in the future.
  • for almost all cases, there are good workarounds, as shown in the example above.
+2
source

Source: https://habr.com/ru/post/982907/


All Articles