Unit testing fileupload with Moq .net Core - c#

I have a method in WebApi controller that I want to write unit tests for. This is how my controller method looks:
Controller.cs
public async Task<FileUploadDto> UploadGoalDocument(Guid id)
{
var file = this.Request?.Form?.Files.FirstOrDefault();
FileUploadDto result = null;
if (file == null)
{
return this.CreateResponse(result);
}
//logic to store file in db
return this.CreateResponse(new FileUploadDto() { Id = document.Id, Name = document.Name, Uri = document.Uri});
}
How can I mock the request object in unit testing? I tried following but ran into problems with IFormFileCollection. The following line throws error:
system.argumentexception interface not found
cc.Setup(x => x.HttpContext.Request.Form.Files).Returns(col.Object);
ControllerTest.cs
public async Task Upload_document_should_upload_document_and_return_dto()
{
var fileDto = new FileUploadDto { Id = Guid.NewGuid(), Name = "dummy.txt" };
var fileMock = new Mock<IFormFile>();
//Setup mock file using a memory stream
using (var ms = new MemoryStream())
{
using (var writer = new StreamWriter("dummy.txt"))
{
writer.WriteLine("Hello World from a Fake File");
writer.Flush();
ms.Position = 0;
fileMock.Setup(m => m.OpenReadStream()).Returns(ms);
var file = fileMock.Object;
this.goalService.Setup(m => m.UploadDocument(Guid.NewGuid(), file, ""))
.ReturnsAsync(new Services.DTO.FileUploadDto { Id = fileDto.Id, Name = fileDto.Name });
var cc = new Mock<ControllerContext>();
var col = new Mock<IFormFileCollection>();
col.Setup(x=> x.GetFile("dummy.txt")).Returns(file);
cc.Setup(x => x.HttpContext.Request.Form.Files).Returns(col.Object);
this.controller.ControllerContext = cc.Object;
var result = await this.controller.UploadGoalDocument(Guid.NewGuid());
//Asserts removed for simplicity
}
}
}
Detailed stack trace:
System.RuntimeTypeHandle.VerifyInterfaceIsImplemented(RuntimeTypeHandle handle, RuntimeTypeHandle interfaceHandle)
at System.RuntimeType.GetInterfaceMap(Type ifaceType)
at Moq.Extensions.IsGetObjectDataVirtual(Type typeToMock)
at Moq.Extensions.IsSerializableMockable(Type typeToMock)
at Moq.SerializableTypesValueProvider.ProvideDefault(MethodInfo member)
at Moq.Mock.GetInitialValue(IDefaultValueProvider valueProvider, Stack`1 mockedTypesStack, PropertyInfo property)
at Moq.Mock.SetupAllProperties(Mock mock, Stack`1 mockedTypesStack)
at Moq.Mock.<>c__DisplayClass72_0.<SetupAllProperties>b__0()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock.SetupAllProperties(Mock mock)
at Moq.QueryableMockExtensions.FluentMock[T,TResult](Mock`1 mock, Expression`1 setup)
at lambda_method(Closure )
at Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock)
at Moq.Mock.<>c__DisplayClass66_0`2.<SetupGet>b__0()
at Moq.PexProtector.Invoke[T](Func`1 function)
at Moq.Mock.SetupGet[T,TProperty](Mock`1 mock, Expression`1 expression, Condition condition)
at Moq.Mock.<>c__DisplayClass65_0`2.<Setup>b__0()
at Moq.PexProtector.Invoke[T](Func`1 function)
at Moq.Mock.Setup[T,TResult](Mock`1 mock, Expression`1 expression, Condition condition)
at Moq.Mock`1.Setup[TResult](Expression`1 expression)
I am thinking I have not constructed the test properly, but a keen eye can point me in the right direction.

For anyone facing similar problem, here's what I did to get it working -
ControllerTest.cs
[TestMethod]
public async Task Upload_document_should_upload_document_and_return_dto()
{
var goalId = Guid.NewGuid();
var file = new Services.DTO.FileUploadDto { Id = goalId, Name = "dummy.txt", Uri = "path/to/file" };
this.goalService.Setup(m => m.UploadDocument(It.IsAny<Guid>(), It.IsAny<IFormFile>(), It.IsAny<string>())).ReturnsAsync(file);
//**This is the interesting bit**
this.controller.ControllerContext = this.RequestWithFile();
var result = await controller.UploadGoalDocument(goalId);
Assert.IsNotNull(result);
Assert.AreEqual(file.Id, result.Data.Id);
Assert.AreEqual(file.Name, result.Data.Name);
Assert.AreEqual(file.Uri, result.Data.Uri);
}
//Add the file in the underlying request object.
private ControllerContext RequestWithFile()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers.Add("Content-Type", "multipart/form-data");
var file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
httpContext.Request.Form = new FormCollection(new Dictionary<string, StringValues>(), new FormFileCollection { file });
var actx = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());
return new ControllerContext(actx);
}

Related

How mock methods of repository in Service for Unit test

I try to have a unit test for my service, I mocked everything needed, How I can Mock repository methods that Service is calling so that has value and code is not breaking,
This is my unit test:
public async Task Updateuser_ReturnsResponse()
{
// Arrange
var request = new UpdateUserRequest()
{
Guid = new Guid("92296ac1-f8e1-489a-a312-6ea9d31d60f8"),
FirstName = "TestFirst",
LastName = "TestLast",
PhoneWork = "9495467845",
EmailWork = "test123#yahoo.com",
};
var respose = new UpdateUserResponse()
{
Success = true
};
var getGuidRequest = new GetGuidRequest()
{
Guid = request.Guid
};
var getGuidResponse = new GetGuidResponse()
{
Guid = request.Guid
};
var mockUserRepository = new Mock<IUserRepository>();
var mockAwsProxy = new Mock<IAwsProxy>();
mockUserRepository.Setup(s => s.UpdateUserAsync(request)).ReturnsAsync(respose);
mockUserRepository.Setup(i => i.GetGuidAsync(getGuidRequest)).ReturnsAsync(getGuidResponse);
var sut = new FromService.UserService(....);
// Act
var response = await sut.UpdateUserAsync(request);
// Assert
Assert.NotNull(response);
Assert.True(response.Success);
}
My problem is when calling - var response = await sut.UpdateUserAsync(request); It goese to service and this GuidResponse is empty so it break after as shows GuidResponse Null:
public async Task<UpdateUserResponse> UpdateUserAsync(UpdateUserRequest request)
{
if (request.EmailWork.HasValue() || request.Role.HasValue())
{
var GuidResponse = await userRepository.GetGuidAsync(new GetGuidRequest
{
Guid = request.Guid
});
// it breaks here because GuidResponse is Null.
if (GuidResponse.Guid != null && request.EmailWork.HasValue())
{
.......
It fails because the setup does not match what was actually given to the mock when the test was exercised.
Use It.Is<T>() to match the passed argument parameter
//...omitted for brevity
mockUserRepository
.Setup(_ => _.GetGuidAsync(It.Is<GetGuidRequest>(x => x.Guid == request.Guid)))
.ReturnsAsync(getGuidResponse);
//...omitted for brevity
assuming the mocked repository is what was injected into the SUT
Reference Moq Quickstart: Matching Arguments

Moq Returning Same Result When Supplying Different Values To Returns()

I'm setting up a Mock for various test cases:
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(4)]
public async Task Get_ReturnsAllUsers(int userCount)
{
// Arrange
var users = UserRepositoryTests.GenerateUsers(userCount);
var extensionWrapper = new Mock<IDbConnectionExtensionsWrapper>();
extensionWrapper
.Setup(ex => ex.Get<User>(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>(), It.IsAny<int>()))
.Returns(Task.FromResult(users));
var connection = new Mock<IDbConnection>();
connection
.Setup(conn => conn.BeginTransaction())
.Returns(new Mock<IDbTransaction>().Object);
var repository = new UserRepository(connection.Object, extensionsWrapper.Object);
var usersFromRepository = await repository.Get();
Assert.AreEqual(userCount, usersFromRepository.Count());
}
GenerateUsers looks like
private static IEnumerable<User> GenerateUsers(int count)
{
using var crypto = new RNGCryptoServiceProvider();
var salt = new byte[16];
crypto.GetBytes(salt);
var random = new Random();
var users = new List<User>();
for (var i = 0; i < count; i++)
{
users.Add(new User
{
Id = i,
Username = Convert.ToString(i),
Salt = salt,
Hash = salt,
});
}
return users;
}
And the relevant repository code looks like
public UserRepository(IDbConnection connection, IDbConnectionExtensionsWrapper wrapper)
{
this.Connection = connection;
this.ConnectionWrapper = wrapper;
}
public async Task<IEnumerable<User>> Get()
{
var users = await this.ConnectionWrapper
.Get<User>(this.Connection);
return users;
}
Finally IDbConnectionExtensionsWrapper.Get(...)
public async Task<IEnumerable<T>> Get<T>(
IDbConnection connection,
IDbTransaction? transaction = null,
int? timeout = null)
where T : class
=> await connection.GetAllAsync<T>(transaction, timeout);
where connection.GetAllAsync belongs to Dapper.Contrib.Extensions.
The problem I have is that only the first TestCase succeeds. Every call to IDbConnectionExtensionsWrapper.Get returns an empty enumerable, despite the fact that I set the return in each case.
Turns out it was a type mismatch that slipped my eyes. Since the signature of IDbConnectionExtensionsWrapper.Get(...) takes an int? and I was setting up the mock with It.IsAny<int>(), there was no suitable call to the method (it was never called).
The correct way to mock the method would be
extensionWrapper
.Setup(ex => ex.Get<User>(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction?>(), It.IsAny<int?>()))
.Returns(Task.FromResult(users));
which ends up making all test cases pass.

ASP.Net Web API 2 Controller Unit Test Get Request Item Count

I am trying to develop an Xunit test to establish if my Controller under test is returning the correct number of objects.
The Controller's getAreas function is as follows:
[HttpGet()]
public IActionResult GetAreas()
{
_logger.LogTrace("AreasController.GetAreas called.");
try
{
// Create an IEnumerable of Area objects by calling the repository.
var areasFromRepo = _areaRepository.GetAreas();
var areas = _mapper.Map<IEnumerable<AreaDto>>(areasFromRepo);
// Return a code 200 'OK' along with an IEnumerable of AreaDto objects mapped from the Area entities.
return Ok(areas);
}
catch (Exception ex)
{
_logger.LogError($"Failed to get all Areas: {ex}");
return StatusCode(500, "An unexpected error occurred. Please try again later.");
}
}
My test class uses Moq to mock the Logger, Repository and AutoMapper. I have created a variable to hold a list of objects to be returned by my mock repository:
private List<Area> testAreas = new List<Area>()
{
new Area
{
Id = new Guid("87d8f755-ef60-4cfa-9a4a-c94cff9f8a22"),
Description = "Buffer Store",
SortIndex = 1
},
new Area
{
Id = new Guid("19952c5a-b762-4937-a613-6151c8cd9332"),
Description = "Fuelling Machine",
SortIndex = 2
},
new Area
{
Id = new Guid("87c7e1d8-1ce7-4d8b-965d-5c44338461dd"),
Description = "Ponds",
SortIndex = 3
}
};
I created my test as follows:
[Fact]
public void ReturnAreasForGetAreas()
{
//Arrange
var _mockAreaRepository = new Mock<IAreaRepository>();
_mockAreaRepository
.Setup(x => x.GetAreas())
.Returns(testAreas);
var _mockMapper = new Mock<IMapper>();
var _mockLogger = new Mock<ILogger<AreasController>>();
var _sut = new AreasController(_mockAreaRepository.Object, _mockLogger.Object, _mockMapper.Object);
// Act
var result = _sut.GetAreas();
// Assert
Assert.NotNull(result);
var objectResult = Assert.IsType<OkObjectResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<AreaDto>>(objectResult.Value);
var modelCount = model.Count();
Assert.Equal(3, modelCount);
}
The test fails on the final Assert, it gets 0 when expecting 3.
The result is not null. The objectResult is an OkObjectResult. The model is an IEnumerable<AreaDto>, however it contains 0 items in the collection.
I can't see where I am going wrong here. Do I have to configure the mocked Automapper mapping?
Do I have to configure the mocked Automapper mapping
Yes
Setup the mapper mock to return your desired result when invoked. Right now it is not setup so will default to empty collection.
Create a collection to represent the DTOs
private List<AreaDto> testAreaDTOs = new List<AreaDto>()
{
new AreaDto
{
Id = new Guid("87d8f755-ef60-4cfa-9a4a-c94cff9f8a22"),
Description = "Buffer Store",
SortIndex = 1
},
new AreaDto
{
Id = new Guid("19952c5a-b762-4937-a613-6151c8cd9332"),
Description = "Fuelling Machine",
SortIndex = 2
},
new AreaDto
{
Id = new Guid("87c7e1d8-1ce7-4d8b-965d-5c44338461dd"),
Description = "Ponds",
SortIndex = 3
}
};
Any update the test to use that collection when the mocked mapped is invoked.
[Fact]
public void ReturnAreasForGetAreas()
{
//Arrange
var _mockAreaRepository = new Mock<IAreaRepository>();
_mockAreaRepository
.Setup(x => x.GetAreas())
.Returns(testAreas);
var _mockMapper = new Mock<IMapper>();
//Fake the mapper
_mockMapper
.Setup(_ => _.Map<IEnumerable<AreaDto>>(It.IsAny<IEnumerable<Area>>()))
.Returns(testAreaDTOs);
var _mockLogger = new Mock<ILogger<AreasController>>();
var _sut = new AreasController(_mockAreaRepository.Object, _mockLogger.Object, _mockMapper.Object);
// Act
var result = _sut.GetAreas();
// Assert
Assert.NotNull(result);
var objectResult = Assert.IsType<OkObjectResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<AreaDto>>(objectResult.Value);
var modelCount = model.Count();
Assert.Equal(3, modelCount);
}

How can I mock the Response.StatusCode with Moq?

I have the following method:
public void SetHttpStatusCode(HttpStatusCode httpStatusCode)
{
Response.StatusCode = (int)httpStatusCode;
}
And the following test:
[TestMethod]
public void SetHttpStatusCode_SetsCorrectStatusCode()
{
//Arrange
//Any url will suffice
var mockHttpContext = TestHelpers.MakeHttpContext("");
mockHttpContext.SetupSet(x => x.Response.StatusCode = It.IsAny<int>());
//creates an instance of an asp.net mvc controller
var controller = new AppController()
{
ControllerContext = new ControllerContext() {
HttpContext = mockHttpContext.Object }
};
// Act
controller.SetHttpStatusCode(HttpStatusCode.OK);
//Assert
mockHttpContext.VerifySet(x => x.Response.StatusCode = It.IsAny<int>());
}
Also, Here is MakeHttpContext
public static Mock<HttpContextBase> MakeHttpContext(string url)
{
var mockHttpContext = new Mock<HttpContextBase>();
var mockRequest = new Mock<HttpRequestBase>();
var mockResponse = new Mock<HttpResponseBase>();
var mockSession = new Mock<HttpSessionStateBase>();
//request
mockRequest.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(url);
mockHttpContext.Setup(x => x.Request).Returns(mockRequest.Object);
//response
mockResponse.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => x);
mockHttpContext.Setup(x => x.Response).Returns(mockResponse.Object);
//session
mockHttpContext.Setup(x => x.Session).Returns(mockSession.Object);
return mockHttpContext;
}
When I run the test, I get the following exception:
Test method PA.Tests.Controllers.AppControllerTest.SetHttpStatusCode_SetsCorrectStatusCode
threw exception:
Moq.MockException:
Expected invocation on the mock at least once,
but was never performed: x => x.StatusCode = It.IsAny<Int32>()
Configured setups:
x => x.StatusCode = It.IsAny<Int32>(), Times.Never
No invocations performed.
How does Moq expect/require invocations to be called? I've debugged the SetHTTPStatusCode method, the response object is indeed a mocked object, however Moq insists that there was no invocation. Am I missing something?
Thanks!
You haven't shown what your TestHelpers.MakeHttpContext method does so it's a bit difficult to understand what's going on.
Try like this:
// Arrange
var mockHttpContext = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
mockHttpContext.SetupGet(x => x.Response).Returns(response.Object);
//creates an instance of an asp.net mvc controller
var controller = new AppController()
{
ControllerContext = new ControllerContext()
{
HttpContext = mockHttpContext.Object
}
};
// Act
controller.SetHttpStatusCode(HttpStatusCode.OK);
//Assert
response.VerifySet(x => x.StatusCode = (int)HttpStatusCode.OK);

mock HttpContext.Current.Server.MapPath using Moq?

im unit testing my home controller. This test worked fine until I added a new feature which saves images.
The method that’s causing the issue is this below.
public static void SaveStarCarCAPImage(int capID)
{
byte[] capBinary = Motorpoint2011Data.RetrieveCapImageData(capID);
if (capBinary != null)
{
MemoryStream ioStream = new MemoryStream();
ioStream = new MemoryStream(capBinary);
// save the memory stream as an image
// Read in the data but do not close, before using the stream.
using (Stream originalBinaryDataStream = ioStream)
{
var path = HttpContext.Current.Server.MapPath("/StarVehiclesImages");
path = System.IO.Path.Combine(path, capID + ".jpg");
Image image = Image.FromStream(originalBinaryDataStream);
Image resize = image.GetThumbnailImage(500, 375, null, new IntPtr());
resize.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
As the call is coming from a unit test, HttpContext.Current is null and throws an exception. After reading about Moq and some of the tutorials about using Moq with sessions, im sure it can be done.
so far this the unit test code have come up with, but the issue is HTTPContext.Current is always null, and still throws the exception.
protected ControllerContext CreateStubControllerContext(Controller controller)
{
var httpContextStub = new Mock<HttpContextBase>
{
DefaultValue = DefaultValue.Mock
};
return new ControllerContext(httpContextStub.Object, new RouteData(), controller);
}
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();
controller.SetFakeControllerContext();
var context = controller.HttpContext;
Mock.Get(context).Setup(s => s.Server.MapPath("/StarVehiclesImages")).Returns("My Path");
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
HomePageModel model = (HomePageModel)result.Model;
Assert.AreEqual("Welcome to ASP.NET MVC!", model.Message);
Assert.AreEqual(typeof(List<Vehicle>), model.VehicleMakes.GetType());
Assert.IsTrue(model.VehicleMakes.Exists(x => x.Make.Trim().Equals("Ford", StringComparison.OrdinalIgnoreCase)));
}
HttpContext.Current is something that you should absolutely never use if you ever expect your code to be unit tested. It is a static method which simply returns null if there is no web context which is the case of a unit test and cannot be mocked. So one way to refactor your code would be the following:
public static void SaveStarCarCAPImage(int capID, string path)
{
byte[] capBinary = Motorpoint2011Data.RetrieveCapImageData(capID, path);
if (capBinary != null)
{
MemoryStream ioStream = new MemoryStream();
ioStream = new MemoryStream(capBinary);
// save the memory stream as an image
// Read in the data but do not close, before using the stream.
using (Stream originalBinaryDataStream = ioStream)
{
path = System.IO.Path.Combine(path, capID + ".jpg");
Image image = Image.FromStream(originalBinaryDataStream);
Image resize = image.GetThumbnailImage(500, 375, null, new IntPtr());
resize.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
You see, now this method no longer depends on any web context and can be tested in isolation. It will be the responsibility of the caller to pass the correct path.
I agree with the Darin´s answer but if you really need to moq the Server.MapPath function you could do something like this
//...
var serverMock = new Mock<HttpServerUtilityBase>(MockBehavior.Loose);
serverMock.Setup(i => i.MapPath(It.IsAny<String>()))
.Returns((String a) => a.Replace("~/", #"C:\testserverdir\").Replace("/",#"\"));
//...
Performing this, the mock will simply replace the ~/ with the c:\testserverdir\ function
Hope it helps!
It is sometimes handy to just mock the call to server.MapPath.
This solution works for me using moq.
I only mock the base path to the application.
_contextMock = new Mock<HttpContextBase>();
_contextMock.Setup(x => x.Server.MapPath("~")).Returns(#"c:\yourPath\App");
_controller = new YourController();
_controller.ControllerContext = new ControllerContext(_contextMock.Object, new RouteData(), _controller);
In your controller you can now user Server.MapPath("~").
Below works for me.
string pathToTestScripts = #"..\..\..\RelatavePathToTestScripts\";
string testScriptsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pathToTestScripts);
var server = new Mock<HttpServerUtilityBase>(); // Need to mock Server.MapPath() and give location of random.ps1
server.Setup(x => x.MapPath(PowershellScripts.RANDOM_PATH)).Returns(testScriptsFolder + "random.ps1");
var request = new Mock<HttpRequestBase>(); // To mock a query param of s=1 (which will make random.ps1 run for 1 second)
request.Setup(x => x.QueryString).Returns(new System.Collections.Specialized.NameValueCollection { { "s", "1" } });
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(x => x.Server).Returns(server.Object);
httpContext.Setup(x => x.Request).Returns(request.Object);
YourController controller = new YourController();
controller.ControllerContext = new ControllerContext(httpContext.Object, new RouteData(), controller);

Categories

Resources