Cannot use Mock interface method with MultipartFormDataStreamProvider parameter
I have this weird problem while trying to spoof an interface using MockedClass.Setup (x => x.Method ()).
This is the interface I'm mocking.
public interface IClassFactory
{
object GetValueFromFormData(string key, MultipartFormDataStreamProvider provider);
}
This is my test.
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public async Task ClassApiController_ImportClassList_ThrowsNullReferenceExceptionWhenNoClassId()
{
// Arrange
_classFactory = new Mock<IClassFactory>();
// THIS IS THE LINE THAT GIVES AN EXCEPTION
_classFactory.Setup(x => x.GetValueFromFormData("classIdNull", null)).Returns(string.Empty);
ClassApiController controller = new ClassApiController(_classRepository.Object, _surveyRepository.Object, _classFactory.Object);
// Act
string result = await controller.ImportClassList();
}
If you look at my comment "THIS IS THE LINE WHAT GIVES THE EXCEPTION" you can see that I am sending null, but it doesn't matter if I am sending MultipartFormDataStreamProvider
as an instansiated class, I still get the same exception.
Exception message: System.ArgumentException: Expression of type 'System.Net.Http.MultipartFormDataStreamProvider' cannot be used for parameter of type 'System.Net.Http.MultipartFormDataStreamProvider' of method 'System.Object GetValueFromFormData(System.String, System.Net.Http.MultipartFormDataStreamProvider)'
If you know why I can't mock a method just because it has this object as a parameter, please help me, I don't know.
Thank!
EDIT: See the solution in my answer
source to share
With the help of @ Vignesh.N, I finally solved this. The simple answer in this case was that my solution is split across multiple projects. Web, Test, Data, etc. In the web project, I referenced the api.dll: s website through Nuget, and in the test project, I directly referenced them through Add references -> Assemblies -> Framework
. Thus, the .dll: s files had identical versions, but not the files. After Nuget took care of all web api.dll projects, it worked instantly.
So, in general, a silly mistake and difficult to spot.
source to share