Testing TryUpdateModel() with MStest and moq - c#

Im currently trying to test an insert method which uses TryUpdateModel(). I am faking the controllercontext which is needed and although that works it does not seem to be posting the model I have setup.
Here is the method I am testing:
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveAjaxEditing(int? id)
{
if (id == null)
{
Product product = new Product();
if (TryUpdateModel(product))
{
//The model is valid - insert the product.
productsRepository.Insert(product);// AddToProducts(product);
}
}
else
{
var recordToUpdate = productsRepository.Products.First(m => m.ProductID == id);
TryUpdateModel(recordToUpdate);
}
productsRepository.Save();
return View(new GridModel(productsRepository.Products.ToList()));
}
And here is my current test:
[TestMethod]
public void HomeControllerInsert_ValidProduct_CallsInsertForProducts()
{
//Arrange
InitaliseRepository();
var httpContext = CustomMockHelpers.FakeHttpContext();
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;
//controller.ControllerContext = new ControllerContext();
var request = Mock.Get(controller.Request);
request.Setup(r => r.Form).Returns(delegate()
{
var prod = new NameValueCollection
{
{"ProductID", "9999"},
{"Name", "Product Test"},
{"Price", "1234"},
{"SubCatID", "2"}
};
return prod;
});
// Act: ... when the user tries to delete that product
controller._SaveAjaxEditing(null);
//Assert
_mockProductsRepository.Verify(x => x.Insert(It.IsAny<Product>()));
}
The method is being called but when it gets to TryUpdateModel() it seems it cannot pickup the posted object. Any pointers on where I am going wrong would be great.

Sorted it. Seems mocking the Httpcontext completly was overkill.
controller.ControllerContext = new ControllerContext();
var prod = new FormCollection
{
{"ProductID", "1"},
{"Name", "Product Test"},
{"Price", "1234"},
{"SubCatID", "2"}
};
controller.ValueProvider = prod.ToValueProvider();
This does the trick. It is now posted.

Related

.Net Core Unit Testing

I have a web api as below, which is being called from my angularjs UI.
public class ValuesController : Controller
{
private static string dynamoDbTable = string.Empty;
private readonly IDynamoDbManager<MyModel> _dynamoDbManager;
public ValuesController(IOptions<Dictionary<string, string>> appSettings, IDynamoDbManager<MyModel> dynamoDbManager)
{
var vals = appSettings.Value;
dynamoDbTable = vals["dynamoDbTable"];
_dynamoDbManager = dynamoDbManager;
}
[HttpGet("api/data")]
public async Task<IActionResult> GetAllData(string status, string role)
{
List<ScanCondition> conditions = new List<ScanCondition>();
conditions.Add(new ScanCondition("status", ScanOperator.Contains, status));
conditions.Add(new ScanCondition("role", ScanOperator.Contains, role));
List<MyModel> model = new List<MyModel>();
model = await _dynamoDbManager.GetAsync(conditions);
return Ok(model);
}
[HttpPost("api/save")]
public async Task<IActionResult> SaveData([FromBody] List<MyModel> listData, string input, string name, string type)
{
List<MyModel> model = new List<MyModel>();
foreach (var data in listData)
{
//populating data here and saving
await _dynamoDbManager.SaveAsync(data);
}
return Ok();
}
}
}
Now I want to write test cases for my API endpoints. I have not written any unit test case before so need input in that. I have read basic documentation of xunit here.
Here is my sample test method:
public class ValuesControllerTests
{
private Mock<IDynamoDbManager<MyModel>> _dbManager;
private ValuesController _valueController;
public ValuesControllerTests()
{
var mockRepository = new MockRepository(MockBehavior.Loose);
_dbManager = mockRepository.Create<IDynamoDbManager<MyModel>>();
var options = new OptionsWrapper<Dictionary<string, string>>(new Dictionary<string, string>()
{
{"dynamoDbTable", nameof(MyModel) }
});
_valueController = new ValuesController(options, _dbManager.Object);
}
[Fact]
public async Task GetAllData_Test()
{
var searchResult = new List<MyModel>()
{
new MyModel(){ }
};
//Here I am trying to use MOQ but this is not working fine
_dbManager
.Setup(_ => _.GetAsync(It.IsAny<List<ScanCondition>>()))
.ReturnsAsync(searchResult);
var result = _valueController.GetAllData("new", "admin");
Assert.IsType<OkObjectResult>(result.Result);
}
}
}
Can I test my method without using moq, if so how can I do so? Because with moq I am not able to test.
Secondly above test case I am just checking if api method returns OK. What other test cases can I write for my get and save method.
I have also created a git project in case anyone wants to look at project: https://github.com/kj1981/xunit
Would appreciate if someone can help me in this.
--Added--
I also tried one test as below, but is this fine? May be my thinking is wrong, with the below test I thought it would fail but it does not.
[Fact]
public async Task GetAllData_Test()
{
var searchResult = new List<MyModel>()
{
new MyModel(){ Id ="1", Name = "Adam", Role = "User", Status ="old" },
new MyModel(){ Id ="2", Name = "Gary", Role = "Admin", Status ="new" }
};
_dbManager
.Setup(_ => _.GetAsync(It.IsAny<List<ScanCondition>>()))
.ReturnsAsync(searchResult);
var okResult = _valueController.GetAllData("fail", "none").Result as OkObjectResult; ;
var items = Assert.IsType<List<MyModel>>(okResult.Value);
Assert.Equal(2, items.Count);
}
Anyone with inputs ?

Fake HttpSessionStateBase in MS Fakes

I am writing a unit test to test an MVC controller method (CRUD operation). The controller method accepts the usernameIdentity from session for auditing purposes.
I am trying the following to setup the ShimHttpSessionStateBase:
[TestMethod()]
public void CategoryCreateTest()
{
using (ShimsContext.Create())
{
var controller = new WebcamsController();
var category = new DataModel.Webcams.Category() { Name = "TestCategory" };
var currentSessionWrapper = new HttpSessionStateWrapper(HttpContext.Current.Session);
var session = new System.Web.Fakes.ShimHttpSessionStateBase(currentSessionWrapper);
session.Instance.Add("usernameIdentity", "DOMAIN\\USERNAME");
controller.ControllerContext = new ControllerContext(controller);
var result = controller.CategoryCreate(category);
Assert.IsInstanceOfType(result, typeof(PartialViewResult));
}
}
I am not sure how to appropriately create the controllerContext so that the Session will be read in the controller method.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CategoryCreate([Bind(Include = "ID,Name")] Category category)
{
try
{
if (category != null
&& Validation.ValidNonNullableString(Session["usernameIdentity"].ToString(), 1, 60) && ModelState.IsValid)
{
businessLayer.SetCategories(category, "Add", AuthenticationHandler.GetHostInternetProtocolAddress, Session["usernameIdentity"].ToString(), AuthenticationHandler.GetUserInternetProtocolAddress);
return PartialView("CategoryList", businessLayer.GetCategoriesByCategory(0, AuthenticationHandler.GetHostInternetProtocolAddress));
}
return jsonResultHandler.JsonResultMessage("Failure", Properties.Resources.GlobalAttributeCreateFailed);
}
catch
{
throw;
}
}

Testing the result of HttpResponse.StatusCode

I've written an ErrorsController that as you can imagine, has pretty simple methods to continue to serve dynamic content in the case of an error, e.g. 500.
Now what I want to do is test that in that method, HttpResponseBase.StatusCode is set to a given number while executing this method, but for some reason, the StatusCode property is always 0. This includes when examining the property directly after setting it.
Controller
public ViewResult NotFound()
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
const string PageTitle = "404 Page Not Found";
var viewModel = this.GetViewModel(PageTitle);
return this.View(viewModel);
}
GetViewModel does nothing other than setting properties on a view model
Test
[SetUp]
public void Setup()
{
this.httpContext = new Mock<HttpContextBase>();
this.httpResponse = new Mock<HttpResponseBase>();
this.httpContext.SetupGet(x => x.Response).Returns(this.httpResponse.Object);
this.requestContext = new RequestContext(this.httpContext.Object, new RouteData());
this.controller = new ErrorsController(this.contentRepository.Object);
this.controllerContext = new Mock<ControllerContext>(this.requestContext, this.controller);
this.controllerContext.SetupGet(x => x.HttpContext.Response).Returns(this.httpResponse.Object);
this.controller.ControllerContext = this.controllerContext.Object;
}
[Test]
public void Should_ReturnCorrectStatusCode_ForNotFoundAction()
{
this.controller.NotFound();
this.httpResponse.VerifySet(x => x.StatusCode = (int)HttpStatusCode.NotFound);
Assert.AreEqual((int)HttpStatusCode.NotFound, this.httpResponse.StatusCode);
}
Where am I going wrong here?
Just add this in your setup phase:
httpResponse.SetupAllProperties();
This being said, you probably don't need those 2 assertions:
this.httpResponse.VerifySet(x => x.StatusCode = (int)HttpStatusCode.NotFound);
Assert.AreEqual((int)HttpStatusCode.NotFound, this.httpResponse.StatusCode);
The first should be more than enough for your unit test.
I end up with an extension method for mocking HttpContext
public static class HttpContextExtensions
{
public static void MockHttpContext(this Controller controller)
{
var httpContextMock = new Mock<HttpContextBase>();
var requestMock = new Mock<HttpRequestBase>();
var responseMock = new Mock<HttpResponseBase>();
responseMock.SetupAllProperties();
requestMock.SetupAllProperties();
httpContextMock.Setup(x => x.Response).Returns(responseMock.Object);
httpContextMock.Setup(x => x.Request).Returns(requestMock.Object);
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = httpContextMock.Object;
}
}
Usage:
someController.MockHttpContext()

ASP.NET MVC unit testing method that returns session

I am doing unit tests using NUnit and Moq. I tried this example that checks whether a session object called Role exists. If not, it creates it, and returns an object of type Role.
protected Role GetRole()
{
if (Session["Role"] == null)
{
Session["Role"] = new Role();
}
return Session["Role"] as Role;
}
Then I use it in the Index action:
public ActionResult Index()
{
var roles = GetRole();
roles.RoleName = "Test";
return View();
}
And this is my test:
[Test]
public void TestMethod1()
{
var contextMock = new Mock<ControllerContext>();
var mockHttpContext = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
mockHttpContext.Setup(ctx => ctx.Session).Returns(session.Object);
contextMock.Setup(ctx => ctx.HttpContext).Returns(mockHttpContext.Object);
contextMock.Setup(p => p.HttpContext.Session["Role"]).Returns(new Role
{
RoleId = 1,
RoleName = "Test"
});
var homeController = new HomeController();
homeController.ControllerContext = contextMock.Object;
var indexView = homeController.Index();
Assert.IsNotNull(indexView);
}
It runs successfully. But when I check the code coverage, it gives me that the Session["Role"] = new Role(); part is not covered by the test code. So I made another test. There I don't setup the session variable Role:
[Test]
public void TestMethod2()
{
var contextMock = new Mock<ControllerContext>();
var mockHttpContext = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
mockHttpContext.Setup(ctx => ctx.Session).Returns(session.Object);
contextMock.Setup(ctx => ctx.HttpContext).Returns(mockHttpContext.Object);
var homeController = new HomeController();
homeController.ControllerContext = contextMock.Object;
var indexView = homeController.Index();
Assert.IsNotNull(indexView);
Assert.IsNull(homeController.ControllerContext.HttpContext.Session["Role"]);
}
But it fails - it gives System.NullReferenceException : Object reference not set to an instance of an object because of the roles.RoleName = "Test"; row. How to make it run?
Thank you in advance!
The problem you have is that in the second test Session["Role"] will never return anything as it's a mocked object set up to always return null. One possible workaround is to change your GetRole function to this and adjust your tests:
protected Role GetRole()
{
var role = Session["Role"] as Role;
if (role == null)
{
role = new Role();
Session["Role"] = role;
}
return role;
}

Mocking Session not working in MVC 5

I'm storing values in the Session in my controller Action being tested. I've read several articles on how to mock a session and I'm trying to implement Milox's answer to Setting the httpcontext current session in unit test. But when I drill into Locals | this | base | HttpContext Sessions is still null and the test fails with a Null Reference exception when setting the Session variable HttpContext.Session["BsAcId"] = vM.BusAcnt.Id;
This is working production code. vM.BusAcnt.Id returns a valid int and if I substitute it with an int value the test still fails because the Session is null and therefore no value can be stored in it.
I'm using MVC5, EF6, and the latest versions of xUnit, Moq and the Resharper test runner.
Action:
public ActionResult Details(int id)
{
var vM = new BusAcntVm();
vM.BusAcnt = _db.BusAcnts.FirstOrDefault(bA => bA.Id == id);
if ((User.IsInRole("Admin"))) return RedirectToAction("Action");
HttpContext.Session["BsAcId"] = vM.BusAcnt.Id;
return View(vM);
}
MockHelpers:
public static class MockHelpers
{
public static HttpContext FakeHttpContext()
{
var httpRequest = new HttpRequest("", "http://localhost/", "");
var stringWriter = new StringWriter();
var httpResponce = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponce);
var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer });
return httpContext;
}
}
Test:
[Fact]
public void AdminGetBusAcntById()
{
HttpContext.Current = MockHelpers.FakeHttpContext();
var mockMyDb = MockDbSetup.MockMyDb();
var controller = new BusAcntController(mockMy.Object);
var controllerContextMock = new Mock<ControllerContext>();
controllerContextMock.Setup( x => x.HttpContext.User.
IsInRole(It.Is<string>(s => s.Equals("Admin")))).Returns(true);
controller.ControllerContext = controllerContextMock.Object;
var viewResult = controller.Details(1) as ViewResult;
var model = viewResult.Model as BusAcntVm;
Assert.NotNull(model);
Assert.Equal("Company 1", model.CmpnyName);
}
Milox's code seems to make sense but I can't get it to work.
Have I missed something? Is there a change in MVC5 that breaks this code?
SOLUTION:
Implementation of Darin's answer. I now have a Session to write the values against (though the values don't actually get written into it, but that's not needed for the purpose of testing) and the test passes.
Test:
[Fact]
public void AdminGetBusAcntById()
{
var mockMyDb = MockDbSetup.MockMyDb();
var controller = new BusAcntController(mockMy.Object);
var context = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
var user = new GenericPrincipal(new GenericIdentity("fakeUser"), new[] { "Admin" });
context.Setup(x => x.User).Returns(user);
context.Setup(x => x.Session).Returns(session.Object);
var requestContext = new RequestContext(context.Object, new RouteData());
controller.ControllerContext = new ControllerContext(requestContext, controller);
var viewResult = controller.Details(1) as ViewResult;
var model = viewResult.Model as BusAcntVm;
Assert.NotNull(model);
Assert.Equal("Company 1", model.CmpnyName);
}
In your unit test you have set HttpContext.Current = MockHelpers.FakeHttpContext(); but ASP.NET MVC doesn't use this static property at all. Forget about HttpContext.Current in ASP.NET MVC. It's legacy and unit testing unfriendly (yes, in your case you are using it only inside your unit test, but ASP.NET MVC doesn't use it and is the reason why your code doesn't work).
The whole point is that ASP.NET MVC is working with abstractions such as HttpContextBase, HttpRequestBase, HttpResponseBase, HttpSessionStateBase, ... that you could easily mock in your unit test.
Let's take an example controller:
public class HomeController : Controller
{
public ActionResult Index()
{
if ((this.User.IsInRole("Admin")))
{
return RedirectToAction("Action");
}
this.HttpContext.Session["foo"] = "bar";
return View();
}
}
and how a corresponding unit test might look like by mocking the required abstractions using Moq:
// arrange
var controller = new HomeController();
var context = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
var user = new GenericPrincipal(new GenericIdentity("john"), new[] { "Contributor" });
context.Setup(x => x.User).Returns(user);
context.Setup(x => x.Session).Returns(session.Object);
var requestContext = new RequestContext(context.Object, new RouteData());
controller.ControllerContext = new ControllerContext(requestContext, controller);
// act
var actual = controller.Index();
// assert
session.VerifySet(x => x["foo"] = "bar");
...
And if you wanted to enter the User.IsInRole("Admin") condition, all you have to do is provide the proper role to the mocked identity.
The way, I would apply Mocking of Sessions using MOQ is as follows.
I would create a base class in UnitTests Project. Structure would be
[TestFixture]
public class BaseClass
{
public Mock<ControllerContext> controllerContext;
public Mock<HttpContextBase> contextBase;
public BaseClass()
{
controllerContext = new Mock<ControllerContext>();
contextBase = new Mock<HttpContextBase>();
controllerContext.Setup(x => x.HttpContext).Returns(contextBase.Object);
controllerContext.Setup(cc => cc.HttpContext.Session["UserId"]).Returns(1);
}
}
Please see : I am returning 1 as session value for UserId in the last line. You can change it as per the requirement.
For easy reference, I would name my TestClass as "ControllerClassTest". So I would inherit ControllerClassTest with BaseClass like this
[TestFixture]
class ControllerClassTest : BaseClass
{
}
Then, In my Test Class, I would initialize ControllerContext within Setup method like this
[SetUp]
public void Setup()
{
controller.ControllerContext = controllerContext.Object;
}
Not to forget, that we have to declare and initialize controller first.
I hope, it helps you

Categories

Resources