Mock object injected via AutoFixture with AutoMoq, unexpected behavior - moq

I've just created my first test using AutoFixture. The SUT has the following constructor.
public LoggingService(
IClientDataProvider clientDataProvider, ... other dependencies...)
The test setup has the following code.
var fixture = new Fixture().Customize(new AutoMoqCustomization());
string ipAddress = "whatever";// fixture.CreateAnonymous<string>();
var clientDataProviderMock = fixture.Freeze<Mock<IClientDataProvider>>();
clientDataProviderMock.Setup(cdp => cdp.IpAddress).Returns(ipAddress);
LoggingService sut = fixture.CreateAnonymous<LoggingService>();
Now, when I examine the contents of sut, I see that the property IpAddress of the injected instance of IClientDataProvider returns null instead of "whatever".
What did I do wrong?
I copied the service and the necessary interfaces to an empty project and then the mocks worked as expected.
Interfaces that are types of constructor arguments of the service in the real project are defined in 3 separate assemblies that have further dependencies. I had several unexpected "Cannot load assembly" errors on the test start because several further assemblies were needed for those directly referenced assemblies. So it seems to be an assembly loading issue.
However, I tried a variation of the test with manual creation of the SUT instance with mock objects created manually using Moq and the test worked as expected

The solution was pretty surprising. When I was creating my unit test project I added the reference to Moq 4.0 first. AutoFixture was added later and since it seems to require Moq 3.1, I copied that dll directly to bin\Debug. However, the corresponding HintPath element in the project file still pointed to the 4.0 dll. As soon as I changed HintPath to point to the place where Moq 3.1 sits the test started to work properly. So Mark was right with his suggestion but the symptoms were quite different.

Related

Specflow with Autofac - Unable to load TechTak.Specflow.Infrastructre.IBindingInstanceResolver Version=2.2.0.0

I have a project that uses Autofac for constructor level injection. For every service instance, repositories are injected to constructor.
Now to start using SpecFlow for the project I am not able to instantiate the service for method calls. I have already added Specflow.Autofac plugin but now I am getting following error. I have checked all referenced assemblies versions and those are correct.
I think you are using SpecFlow.Autofac. Is this correct?
If so, it doesn't work with SpecFlow 2.2 at the moment.
For details have a look at this issue: https://github.com/techtalk/SpecFlow/issues/959

How should I add environment properties to my JUnit5 Spring MVC integration test?

I'm writing integration tests using MockMvcBuilders.webAppContextSetup() in JUnit 5.
I'm extending with Sam Brannen's SpringExtension and a MockitoExtension. (Really, I'm using the composed SpringJUnitJupiterWebConfig)
I get this output when running the tests (edited):
java.lang.IllegalStateException: Failed to load ApplicationContext
BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.myapp.config.SomeConfig];
Could not resolve placeholder 'someEnvVar' in string value "classpath:/com/example/myapp/config/${someEnvVar}/custom.properties"
(This is in an internal company library on which my application depends.)
Seems clear that I need to set the environment property before the container starts up. But I'm struggling to discover how to hook into that with JUnit5.
I want to add PropertySources to the environment, I assume, but to get the environment, I have to get the application context, and in so doing, it instantiates, erroring out before I can do anything with it.
I tried creating my own extension and getting a handle to the environment during the BeforeAllCallback.
I'm getting the feeling that I'm going about it all wrong and I'm missing something fundamental.
I just needed to use the #TestPropertySource annotation on my test class.

Error when using DependencyResolver for Controller instantiation with MVC 3

I'm using MVC 3 and using the following code when the application starts...
UnityContainer container = new UnityContainer();
new UnityMappings(container);
DependencyResolver.SetResolver(new UnityServiceLocator(container));
Now when the app runs I'm getting the following error (but only sometimes)...
Activation error occured while trying to get instance of type
IControllerFactory, key ""
Interestingly, if I continue with the web request, the website works normally.
Any ideas? I can't see what I'm doing differently from before when this worked fine.
Cheers, Ian.
MVC3 requests a lot more than just controllers from the DependencyResolver. For most of them MVC3 falls back to the default implementation in case the DependencyResolver does not return an instance.
In your case it requests the IControllerFactory which is unknown to your IoC container and it throws an exception which is caught by the UnityServiceLocator implementation and null is returned. MVC then falls back to the default controller factory.
Unlike other IoC containers Unity does not provide an optional TryResolve and therefore does not support a proper exceptionless implementation of the DependencyResolver.
I would suggest first looking through the config and make sure everything is correct there, then I would make sure I had all the assemblies needed for Unity referenced in the project. That error message may (in my experience) point to an error in config or a missing DLL, perhaps an assembly you want to load in the container or another DLL needed by a DLL loaded by Unity?
Hope this helps.

Null reference to DataContext when testing an ASP.NET MVC app with NUnit

I have an ASP.NET MVC application with a separate project added for tests. I know the plusses and minuses of using the connection to the database when running unit tests, and I still want to use it. Yet, every time when I run the tests with the NUnit tool, they all fail due to my Data Context being null. I heard something about having a separate config file for the tests assembly, but i am not sure whether I did it properly, or whether that works at all.
i think you should check this discussion here, it should be related as i was having the same problem.
and how i solve my problem was just to copy my web config content to the app config inside he test project and voila, database connection restore and all is fine in the land of mvc again.
How are you creating your data context? How is it used in your action? Typically, it will use the database referred to when you set up the classes in the designer so you'd get a context connected to what you used for the designer which is, arguably, not what you want for unit tests, thus you add an app.config file to your unit test project and change the connection string to your test database. It doesn't usually result in a null data context.
I suspect that your unit test is simply not touching the code that creates the data context before you invoke the action method. Without code though, it's really impossible to tell.

VS2008 Update Web Reference creates naming conflicts in Reference.cs

I have a Web service which, when updated on one computer with VS2008 works perfectly fine, but on another computer does not. The critical difference is the contents of the auto-generated Reference.cs.
In the correctly functioning environment, the methods have fully qualified class names, refering to classes in a class library. However, in the problem environment, the class names are unqualified and partial classes are declared near the end of the file. E.g.
public string MyMethod(MyClass pr_Class)
...
public partial class MyClass {
}
versus
public string MyMethod(Class.Library.Namespace.MyClass pr_Class)
This causes errors such as:
'<ClassType>' is an ambiguous reference between <ClassLibraryNamespace>.ClassType and <WebReferenceNamespace>.ClassType
The code is under source control. Checking out to the working environment works straight away, checking out to the problem environment will only work if the reference.cs is manually edited to be like the working environments. Transferring the project manually from problem environment to the working one only requires an "Update Web Reference" to be performed to allow compilation. Both Reference.cs files claim to be auto-generated by the same tool version. The settings from the good environment have been exported to the other. No success so far...
Any ideas would be greatly appreciated!
Matt
Ok, so I have the answer. The working environment has a long forgotten extension library for importing web references. Customizing generated Web Service proxies explains the procedure. I'm new to the project and so had no idea about this!

Resources