How mock class in Unit Test for a Action in MVC - c#

I'm implementing Unit Test on existing software.
These software is in ASP.NET MVC, and I want test the return of a Action, of a Controller.
But in this Action a I have the code:
public ActionResult EditProfile(
string accountId = null,
string partnership = null,
AccountType? subscriptionType = null,
bool forcePartnerUpdate = false)
{
var model = new EditProfileModel();
var account = _authUser.Account;
var catalogClient = new CatalogService.CatalogClient(Request.Cookies, Globals.CatalogURL);
The problem is because the last line : "new CatalogService.CatalogService()"
I'm using this class in other part of Action:
model.ListBrands = new List<Brands>();
model.ListAvailableBrands = new List<Brands>();
model.ListSubscribedBrands = new List<Brands>();
var brands = catalogClient.GetBrandsWithManufacturer();
So, How I can mock this for tests?
I have thought in abstract for a interface and send how a parameter in action, but I don't have the Request in my NInjectModule to IoC and in other codes this class has other parameters, so I think I can't do IoC.
How I can mock this with moq?
Thank you for help.

I've thought and tested a lot and now I got to do the mock for my tests.
First, I've created a "Factory" to my class:
public interface ICatalogClientFactory
{
ICatalogClient Create(HttpCookieCollection cookies, string catalogUrl);
}
public class CatalogClientFactory : ICatalogClientFactory
{
public ICatalogClient Create(HttpCookieCollection cookies, string catalogUrl)
{
return new CatalogClient(cookies, catalogUrl);
}
}
Than, this Interface I've registered in the NinjectModule and set this in the constructor of Controller. So, I'm calling in my actions with this code:
ICatalogClientFactory _catalogClientFactory = catalogClientFactory;
var clientCatalog = _catalogClientFactory.Create(Request.Cookies, Globals.CatalogURL);
Now, I can mock "ICatalogClientFactory" and "ICatalogClient" to use in unit tests.

Related

Mocking the httprequest with an manually defined accepttype or null [duplicate]

I have following web Api controller method.
When I run this code through web, HttpContext.Current is never null and give desired value.
public override void Post([FromBody]TestDTO model)
{
var request = HttpContext.Current.Request;
var testName = request.Headers.GetValues("OS Type")[0];
// more code
}
However, when I call this method from Unit Test, HttpContext.Current is always null.
How do i fix it?
During unit tests HttpContext is always null as it is usually populate by IIS. You have a few options around this.
Sure, you could mock the HttpContext, (which you shouldn't really do - Don't mock HttpContext!!!! He doesn't like to be mocked!),. You should really try to stay away from tight coupling with HttpContext all over your code. Try constraining it to one central area (SRP);
Instead figure out what is the functionality you would like to achieve and design an abstraction around that. This will allow for your code to be more testable as it is not so tightly coupled to HttpContext.
Based on your example you are looking to access header values. This is just an example of how to change your thinking when it comes to using HttpContext.
Your original example has this
var request = HttpContext.Current.Request;
var testName = request.Headers.GetValues("OS Type")[0];
When you are looking for something like this
var testName = myService.GetOsType();
Well then create a service that provides that
public interface IHeaderService {
string GetOsType();
}
which could have a concrete implementation like
public class MyHeaderService : IHeaderService {
public string GetOsType() {
var request = HttpContext.Current.Request;
var testName = request.Headers.GetValues("OS Type")[0];
return testName;
}
}
Now in your controller you can have your abstraction instead of having tight coupling to HttpContext
public class MyApiController : ApiController {
IHeaderService myservice;
public MyApiController(IHeaderService headers) {
myservice = headers;
}
public IHttpActionResult Post([FromBody]TestDTO model) {
var testName = myService.GetOsType();
// more code
}
}
You can later inject your concrete type to get the functionality you want.
For testing you then swap dependencies to run your test.
If the method under test is your Post() method you can create a fake dependency or use a mocking framework
[TestClass]
public class MyTestClass {
public class MyFakeHeaderService : IHeaderService {
string os;
public MyFakeHeaderService(string os) {
this.os = os;
}
public string GetOsType() {
return os;
}
}
[TestMethod]
public void TestPostMethod() {
//Arrange
IHeaderService headers = new MyFakeHeaderService("FAKE OS TYPE");
var sut = new MyApiController(headers);
var model = new TestDTO();
//Act
sut.Post(model);
//Assert
//.....
}
}
This is by design and it's always null. But there is a FakeHttpContext project on Nuget that simply you can use it.
To install FakeHttpContext, run the following command in the Package Manager Console (PMC)
Install-Package FakeHttpContext
And then use it like this:
using (new FakeHttpContext())
{
HttpContext.Current.Session["mySession"] = "This is a test";
}
Visit https://www.nuget.org/packages/FakeHttpContext to install the package
See examples on Github: https://github.com/vadimzozulya/FakeHttpContext#examples
Hope this will help :)
All you need is
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
From unit-testing-controllers-in-web-api

How do you moq a class for unit test

This question is posted as a follow up to How do you Mock an class for a unit test that has a return type but no input parameters
Since asking the original question I have now created a Minimal, Complete and Verifiable Example which is used as the basis for this question.
I have a controller (shown below)
public class HomeController : Controller
{
private OrganisationLogic _organisationLogic;
public HomeController(OrganisationLogic logic)
{
_organisationLogic = new OrganisationLogic();
}
public ActionResult Index()
{
var model = _organisationLogic.GetOrganisation().ToViewModel();
return View(model);
}
}
The controller retrieves data from a method in a Business Logic Layer called OrganisationLogic (shown below)
public class OrganisationLogic : LogicRepository<OrganisationModel>
{
public OrganisationLogic()
{
}
public override OrganisationModel GetOrganisation()
{
return new OrganisationModel { Id = 1, OrganisationName = "My Orgaisation", Address = "Logic" };
}
}
The business logic later inherits Logic repository (shown below)
public abstract class LogicRepository<T> : ILogicRepository<T>
{
protected LogicRepository()
{
}
public abstract T GetOrganisation();
}
The Logic Repository implements the ILogicRepository Interface (shown below)
public interface ILogicRepository<TModel>
{
TModel GetOrganisation();
}
I want to Unit Test the HomeController to verify that the data displayed in the ViewModel is returned correctly from OrganisationLogic and Transformed from OrganisationModel into an OrganisationViewModel.
I have written the following UnitTest which uses Moq to mock the method _OrganisationLogic.GetOrganisation().
[TestMethod]
public void Index()
{
var _OrganisationLogic = new Mock<OrganisationLogic>();
var testdata = new OrganisationModel { Id = 3, OrganisationName = "My Test Class Organisation" };
_OrganisationLogic.Setup(p => p.GetOrganisation()).Returns(testdata).Callback<OrganisationModel>(p=>p = testdata);
HomeController controller = new HomeController(_OrganisationLogic.Object);
ViewResult result = controller.Index() as ViewResult;
OrganisationViewModel model = (OrganisationViewModel)result.Model;
Assert.AreEqual(testdata.OrganisationName,model.OrganisationName);
}
When I run the test the test fails. The reason for this is that the Mock has not overridden the class and has instead returned the result from the actual method in the BusinessLogic Layer.
In my original question I posted that the error message being generated was:
System.ArgumentException Invalid callback. Setup on method with 0 parameter(s) cannot invoke callback with different number of parameters (1). Source=Moq StackTrace: at Moq.MethodCallReturn2.ValidateNumberOfCallbackParameters(MethodInfo
callbackMethod) at Moq.MethodCallReturn2.ValidateReturnDelegate(Delegate callback) at Moq.MethodCallReturn2.Returns[T](Func2 valueExpression)
I have now been able to replicate this error message exactly by running the following Unit Test. I suspect the unit test above is closer to what I need and that in the instance below I am setting up the Return() incorrectly. Thoughts on this are welcome?
[TestMethod]
public void Index()
{
var _OrganisationLogic = new Mock<OrganisationLogic>();
var testdata = new OrganisationModel { Id = 3, OrganisationName = "My Test Class Organisation" };
_OrganisationLogic.Setup(p => p.GetOrganisation()).Returns<OrganisationModel>(p=>p = testdata).Callback<OrganisationModel>(p=>p = testdata);
HomeController controller = new HomeController(_OrganisationLogic.Object);
ViewResult result = controller.Index() as ViewResult;
OrganisationViewModel model = (OrganisationViewModel)result.Model;
Assert.AreEqual(testdata.OrganisationName,model.OrganisationName);
}
My question is how to I setup the Mock so that it uses my test data.
In order to assist with answering the the above I have placed a version of the Code on GitHub that demonstrates this issue and shows the test failing. This can be accessed at https://github.com/stephenwestgarth/UnitTestExample
Any help would be very much appreciated.
Change the constructor of HomeController so that the parameter type is ILogicRepository<OrganisationModel>, and the field will need that type as well, and use the instance that was injected into the constructor. _organisationLogic = logic; (your code above ignores the parameter and creates its own concrete instance of the OrganisationLogic, which means it is not using your mock object.
In the test change the declaration of _OrganisationLogic to be...
var _OrganisationLogic = new Mock<ILogicRepository<OrganisationModel>>();
As I said when you previously asked, I don't think you need that Callback in there.
Edited constructor would look like this...
private ILogicRepository<OrganisationModel> _organisationLogic;
public HomeController(ILogicRepository<OrganisationModel> logic)
{
_organisationLogic = logic;
}

ASP.NET MVC - unit testing creation of action method URL's in a business assembly

I'm trying to write some unit tests for my ASP.NET MVC code, but I'm hitting a road block.
I have entity partial classes (in a business class library assembly) that need to determine a URL to call for an action method and controller. In order to do that, I found this snippet of code which works nicely - but alas, this uses the HttpContext.Current and thus prevents me from writing any unit tests:
public string NavigateUrl
{
get
{
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));
string url = urlHelper.Action("SomeAction", "MyController");
}
}
I am reading about the HttpContextBase - but how does this come into play here?? Or is there another way to determine an action URL inside an entity class (that is in a business assembly - NOT the MVC project and not inside a controller or other MVC infrastructure class)?
Update: I need to return this URL from an entity class as a string, since I need to use it in a grid as the navigation URL of a hyperlink. And in reality, there are numerous conditions being checked and the URL string returned can be one of several possibilities - so I cannot just replace it by a single controller call...
Create an abstraction to represent the desired functionality.
For example
public interface IUrlHelper {
string Action(string actionName, string controllerName);
//TODO: create other desired members to be exposed
}
You then create a factory for that abstraction. Since you are not injecting it into the entities we are using a service locator ani-pattern.
public static class UrlHelperFactory {
public static Func<IUrlHelper> Create = () => {
throw new NotImplementedException();
};
}
The helper and factory are not coupled to anything and could live anywhere in the solution.
The following test mocks the service to allow the entity to be tested in isolation.
[TestClass]
public class UrlHelperFactory_Should {
public class MyTestEntity {
public string NavigateUrl {
get {
var urlHelper = UrlHelperFactory.Create();
string url = urlHelper.Action("SomeAction", "MyController");
return url;
}
}
}
[TestMethod]
public void Generate_NavigationUrl() {
//Arrange
var mockHelper = Mock.Of<IUrlHelper>();
UrlHelperFactory.Create = () => {
return mockHelper;
};
var expected = "http://my_fake_url";
Mock.Get(mockHelper)
.Setup(_ => _.Action(It.IsAny<string>(), It.IsAny<string>()))
.Returns(expected);
var sut = new MyTestEntity();
//Act
var actual = sut.NavigateUrl;
//Assert
actual.Should().NotBeNullOrWhiteSpace()
.And.Be(expected);
}
}
In production code at the composition root you make sure that the factory knows how to build the service
UrlHelperFactory.Create = () => {
var httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));
return new DefaultUrlHelperWrapper(urlHelper);
};
Where a wrapper could look like this...
internal class DefaultUrlHelperWrapper : IUrlHelper {
private UrlHelper urlHelper;
public DefaultUrlHelperWrapper(UrlHelper urlHelper) {
this.urlHelper = urlHelper;
}
public string Action(string actionName, string controllerName) {
return urlHelper.Action(actionName, controllerName);
}
//TODO: Implement other members
}

Unit Testing with DbContext mock through Service Layer

I'm a beginner at writing unit tests and I have a test I'm trying to get working. I'll start of by explaining what I'm trying to test.
I'm trying to test a method which saves messages in a Mvc 4 project. The method is called SaveMessage and is shown below.
namespace ChatProj.Service_Layer
{
public class UserService : IUserService
{
public MessageContext messageContext = new MessageContext();
public UserService()
{
_messageRepository = new MessageRepository(messageContext);
}
private IMessageRepository _messageRepository;
-> public void SaveMessage(Message message)
{
messageContext.Messages.Add(message);
_messageRepository.Save();
}
The _messageRepository.Save in the SaveMessage method is implemented in my DAL layer MessageRepository and looks like this:
public void Save()
{
context.SaveChanges();
}
This way of saving will seem a bit overcomplicated, but I structured the project this way because I didn't want the service layer (IUserService & UserService) to handle operations that could & should (i think) be handled by the Data Access Layer (IMessageRepository & MessageRepository).
Now comes the tricky part. I've been trying to understand how I could unit test this. This is my try:
namespace ChatProj.Tests
{
[TestFixture]
class MessageRepositoryTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void SaveMessage_SaveWorking_VerifyUse()
{
//Arrange
var userServiceMock = new Mock<UserService>();
var message = new Message { MessageID = 0, Name = "Erland", MessageString = "Nunit Test", MessageDate = DateTime.Now };
var repositoryMock = new Mock<IMessageRepository>();
var contextMock = new Mock<MessageContext>();
MessageRepository messageRepository = new MessageRepository(contextMock.Object);
UserService userService = new UserService();
//Act
userService.SaveMessage(message);
//Assert
repositoryMock.Verify(m => m.Save());
userServiceMock.Verify(m => m.SaveMessage(message));
}
}
I get this error: Imgur link , and I'm not quite sure how to solve it. I've tried looking at several other SO posts but I fail to make the test work.
So I'm wondering, how do I practically get my Unit Test to work?
You should setup your MessageContext properties to return fake data and don't make real Db call with SaveChanges method.
Right now it still tries to access a real DB.
But you can setup only virtual properties or if it will be an inteface.
So the best solution is to extract an interface from your MessageContext and inject it into repository. Then you can easily mock your IMessageContext interface and force it to return appropriate in-memory data.
Take a look at these two lines:
UserService userService = new UserService();
//Act
userService.SaveMessage(message);
You're creating a userService instance, and then immediately saving your message. Now jump into the SaveMessage code.
public void SaveMessage(Message message)
{
messageContext.Messages.Add(message);
_messageRepository.Save();
}
Ok, now you're adding stuff to messageContext, and then calling _messageRepository.Save(). But where are messageContext and _messageRepository instantiated?
public MessageContext messageContext = new MessageContext();
public UserService()
{
_messageRepository = new MessageRepository(messageContext);
}
You're creating them at instantiation. The mocks that you've created in your test aren't being used. Instead of creating instances of these objects in the constructor, you might consider passing them into the UserService constructor as arguments. Then, you can pass in mocked instances in your test.

Unit Testing method ASP. NET (NUnit)

I have a controller that need to test. It has a method with the function inside.
public ActionResult GetZZ()
{
ApplyResponseHeaders();
var result = new MediaJsonResult();
using (var str = new StreamReader(Request.InputStream))
{
string inputData = str.ReadToEnd();
MyFunction(inputData, result);
}
return Json(result);
}
I just want to test the function MyFunction. This function is private. How can I do this. Test the entire method is not necessary, because the problems in the appointment of its values ​​in Request.InputStream
Don't try to test private methods ever. These methods are not part of public API and cannot be invoked by caller. Your goal is to satisfy requirements for public API. It really doesn't matter if private method works as expected or not. From caller's point of view this method does not exist and does not have any value.
Instead you should test functionality, which is available via public API, GetZZ() method in your case. But you should mock external dependencies in order to test your controller in isolation with any test data you want.
So, here you have two options. First one is mocking HttpRequest which your controller depends on, and providing test data for input stream (you will have to do lot of work):
var httpRequest = new Mock<HttpRequestBase>();
var stream = new MemoryStream(Encoding.Default.GetBytes("Hello world"));
httpRequest.Setup(r => r.InputStream).Returns(stream);
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(c => c.Request).Returns(httpRequest.Object);
var controller = new HomeController();
var routeData = new RouteData();
controller.ControllerContext = // set mocked context
new ControllerContext(httpContext.Object, routeData, controller);
var result = (JsonResult)controller.GetZZ();
Assert.That(result.Data, Is.EqualTo(42)); // your assertions here
Another option - hiding this environment-related stuff under some abstraction, which can be easily mocked (of course, you should use better names):
public interface IFoo
{
string Bar();
}
This is a implementation, which uses current context request to get input data:
public class Foo : IFoo
{
public string Bar()
{
using (var str = new StreamReader(HttpContext.Current.Request.InputStream))
{
string inputData = str.ReadToEnd();
return inputData;
}
}
}
Make controller depend on this abstraction:
public class HomeController : Controller
{
private readonly IFoo _foo;
public HomeController(IFoo foo) // inject dependency
{
_foo = foo;
}
public ActionResult GetZZ()
{
ApplyResponseHeaders();
var result = new JsonResult();
MyFunction(_foo.Bar(), result); // use dependency
return result;
}
}
Now you can mock it without any problems:
var foo = new Mock<IFoo>();
foo.Setup(f => f.Bar()).Returns("Hello, TDD");
var controller = new HomeController(foo.Object);
var result = (JsonResult)controller.GetZZ();
Assert.That(result.Data, Is.EqualTo(42));
One easy way is to make the method public. If you can't (or don't want to), you could make the method protected instead of private, then subclass your controller in your test assembly and test it through the derived type.
Something like so:
public class TesterController : YourController
{
public new ActionResult MyFunction(string inputData, MediaJsonResult result)
{
return base.MyFunction(inputData, result);
}
}
You can mark the methods as internal instead of private and then add a InternalsVisibleTo("Path.To.Test.Project") in AssemblyInfo.cs of your controllers.
Not 100% sure agree about NEVER EVER test a private method in your code. Like most things, sometimes it makes sense and being pragmatic is often better then being dogmatic.

Categories

Resources