How can I use Microsoft Fakes to mock User.Identity.Name when unit testing MVC 4 application with Visual Studio 2012.
I'm writing unit test for item create action method.
[HttpPost]
public ActionResult Create([Bind(Include = "Name")]Category category)
{
if (categoryService.IsNameExists(category.Name))
{
ModelState.AddModelError("Name", "Category name already exists!");
return View(category);
}
try
{
if (ModelState.IsValid)
{
UserProfile p = new UserProfile();
p.UserName = User.Identity.Name;
category.CreatedBy = p;
category.CreatedDate = DateTime.Now;
category.Active = true;
category.DeletedBy = null;
category = categoryService.SaveCategory(category);
return RedirectToAction("Index");
}
return View(category);
}
catch (DataException dex)
{
}
}
[TestMethod]
public void Create()
{
Category createdCategory = new Category();
ICategoryService service = new StubICategoryService()
{
SaveCategoryCategory = (category) => { return category; }
};
CategoryController controller = new CategoryController(service);
using (ShimsContext.Create())
{
System.Fakes.ShimDateTime.NowGet = () =>
{ return new DateTime(2000, 1, 1); };
ViewResult result = controller.Create(createdCategory) as ViewResult;
Assert.IsNotNull(result);
}
}
These are the action method and test method I have written. If there is better way to do this other than MS Fakes please tell me, (not another mocking framework).
Assuming you've added Fakes references for System.Web and System, you can do something like this inside your using (ShimsContext.Create()) block:
var context = new System.Web.Fakes.ShimHttpContext();
var user = new StubIPrincipal
{
IdentityGet = () =>
{
var identity = new StubIIdentity {NameGet = () => "foo"};
return identity;
}
};
context.UserGet = () => principal;
System.Web.Fakes.ShimHttpContext.CurrentGet = () => { return context; };
User is actually HttpContext.User. So you could use System.Fakes.ShimHttpContext to return a custom implementation of the whole IPrincipal containing the right Identity.Name...
I ended up with a similar answer to #Sven, but ended up stubbing the context instead of using a shim.
using (AccountController controller = new AccountController())
{
StubHttpContextBase stubHttpContext = new StubHttpContextBase();
controller.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), controller);
StubIPrincipal principal = new StubIPrincipal();
principal.IdentityGet = () =>
{
return new StubIIdentity
{
NameGet = () => "bob"
};
};
stubHttpContext.UserGet = () => principal;
}
Related
I'm new at asp.net and moq and xunit test, I want to create a test case for my asp.net project. I have the service file that is CRUD action,
I have been written for Create Method it's Ok but at Get, GetAll, Delete, Put method I don't know what should I setup
public void Test1()
{
var tutorRequest = new Mock<ITutorRequestService>();
//What should i set up for Get and GetAll
tutorRequest.Setup(r => r.Get(It.IsAny<int>(), It.IsAny<string?>()))
.ReturnsAsync(() => new Entity());
var logger = new Mock<ILogger<TutorRequestsController>>();
var controller = new TutorRequestsController(tutorRequest.Object, logger.Object);
var rs = controller.Get(1, "");
Assert.NotNull(rs);
}
My service class
public async Task<TutorRequest> Create(TutorRequestModel tutor)
{
var entity = _mapper.Map<TutorRequest>(tutor);
CheckDataNotNull("entity", entity);
entity = await _repository.Add(entity);
return _mapper.Map<TutorRequest>(entity);
}
public async Task<TutorRequest> Delete(int tutorId)
{
var entity = await _repository.Delete(tutorId);
return _mapper.Map<TutorRequest>(entity);
}
public async Task<Entity> Get(int id, string? fields)
{
var entity = await _repository.Get(id);
CheckDataNotNull("tutor request", entity);
var shaped = _dataShaper.ShapeData(entity, fields);
CheckDataNotNull("shaped", shaped);
return _mapper.Map<Entity>(shaped);
}
public PageList<Entity> GetAll(TutorRequestParams param)
{
var listAll = _repository.GetAll();
CheckDataNotNull("shaped", listAll);
Search(ref listAll, param);
var sortedStudent = _sortHelper.ApplySort(listAll, param.OrderBy);
var shapedOwners = _dataShaper.ShapeData(sortedStudent, param.Fields);
return PageList<Entity>.ToPageList(shapedOwners, param.PageNume, param.PageSize);
}
public async Task<TutorRequest> Update(TutorRequestModel tutor)
{
var entity = _mapper.Map<TutorRequest>(tutor);
entity = await _repository.Update(entity);
return _mapper.Map<TutorRequest>(entity);
}
If you're expecting the GetAll method to return something, you need to set up the call for the mocked service.
mockRepo.Setup(r => r.GetAll(It.IsAny<TutorRequestParams>())).Returns(
new PagedList<ExpandoObject>
{
// result you're expecting
}
);
I have write a test case for Get method that similar to your example, you can check it out
public void Test_Get_Method()
{
var questService = new Mock<IQuestService>();
questService.Setup(x => x.Get(It.IsAny<Guid>()))
.ReturnsAsync(new QuestResponseModel()
{
Id = Guid.Empty,
Title = "Test",
Description = "TestDescription",
Price = 100,
Status = "Pending",
AvailableTime = DateTime.Now,
EstimatedTime = "120",
QuestTypeId = 1
});
var mapperMock = new Mock<IMapper>();
var controller = new QuestController(questService.Object);
var result = controller.Get(Guid.Empty);
Assert.Equal("TestDescription", result.Result.Data.Description);
}
I have this method:
[HttpPut]
[AuthorizeClaim("field:write")]
[ApiConventionMethod(typeof(AttemptApiConventions), nameof(AttemptApiConventions.AttemptPut))]
public override async Task<ActionResult<Field>> UpdateAsync(Field model)
{
var getAttempt = await Mediator.Send(new GenericGet<Field>(model.Id));
if (getAttempt.Failure) return Ok(getAttempt);
var field = getAttempt.Result;
if (!field.IsSpecification && !User.IsInRole("Administrator"))
return Ok(new ForbiddenError(string.Format(Resources.PermissionError, "this field")).ToAttempt<Field>());
if (!field.IsSpecification && model.Name != field.Name)
return Ok(new ValidationError(string.Format(Resources.CannotBeChanged, nameof(Field.Name))).ToAttempt<Field>());
return await base.UpdateAsync(model);
}
As you can see, I check if the User.IsInRole to display a different error message when they are trying to change a field.
The problem is, I have this unit test:
[Test]
public async Task ReturnBadRequestIfSpecificationFieldNameChanges()
{
// Assemble
var model = new Field {Id = 1, IsSpecification = false, Name = "Test"};
var services = FieldsControllerContext.GivenServices();
var controller = services.WhenCreateController("Administrator");
services.Mediator.Send(Arg.Any<GenericGet<Field>>())
.Returns(new Field {Id = 1, IsSpecification = false, Name = "Old"});
// Act
var actionResult = await controller.UpdateAsync(model);
// Assert
actionResult.ShouldBeBadRequest(string.Format(Sxp.Web.Properties.Resources.CannotBeChanged, nameof(Field.Name)));
}
Take a look at the line services.WhenCreateController("Administrator");. It is passing the role I want the mocked user to be a member of. This is set like this:
public static class ControllerExtensions
{
public static T WithUser<T>(this T controller, string roles = null) where T: ControllerBase
{
var id = Guid.NewGuid().ToString();
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "example name"),
new Claim(ClaimTypes.NameIdentifier, id),
new Claim(JwtClaimTypes.Subject, id),
};
if (!string.IsNullOrEmpty(roles))
{
var r = roles.Split(",");
claims.AddRange(r.Select(role => new Claim(JwtClaimTypes.Role, role)));
}
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "mock"));
controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = user
}
};
return controller;
}
}
When I debug my test, I can see this:
Can anyone tell me why?
I found the answer. This line was to blame:
claims.AddRange(r.Select(role => new Claim(JwtClaimTypes.Role, role)));
It isn't JwtClaimTypes that it checks, it actually checks for:
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
Test Setup
Mock gives null object when the test hits the method inside the controller
My controller's constructor has two dependencies which I mocked. While running the test for CheckOut it is giving null object in the result. I am using AutoMapper to map between Model objects and Business
[Fact]
public void CheckOutSomethingsTest()
{
Mock<ICheckoutService> checkoutServiceMock = new Mock<ICheckoutService>();
Mock<IMapper> mapperMock = new Mock<IMapper>();
checkoutServiceMock.Setup(c => c.CheckoutSomethings(new CheckOutSomethingsInput
{
SecretKey = "SecureKey",
UserId = 100,
SomethingIds = new List<int> { 10001, 1002, 1003 }
}))
.Returns<List<CheckedOutSomething>>((o) => new List<CheckedOutSomething>
{
new CheckedOutSomething { Id = 10001, Remarks = "Success" },
new CheckedOutSomething { Id = 10002, Remarks = "Success" }
});
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddSomething(new AutoMapperBusinessToEntitiesSomething());
cfg.AddSomething(new AutoMapperModelsToBusinessSomething());
});
var mapper = new Mapper(configuration);
var controller = new SomethingsController(checkoutServiceMock.Object, mapper);
var result = controller.CheckOut(
new CheckOutSomethingInputModel
{
UserId = 100,
SomethingIds = new List<int> { 10001, 10002, 10003 }
});
Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(2, result.Value.Count);
}
Service
Constructor
public ProfilesController(ICheckoutService checkoutService, IMapper mapper)
{
this.checkoutService = checkoutService;
this.mapper = mapper;
}
Method
[HttpPost]
[Route("checkout")]
public ActionResult<List<CheckedOutSomethingModel>>
CheckOut([FromBody] CheckOutSomethingInputModel checkoutInput)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var input = mapper.Map<CheckOutSomethingsInput>(checkoutInput);
var output = mapper.Map<List<CheckedOutSomethingModel>>
(checkoutService.CheckoutSomethings(input));
return Ok(output);
}
I cannot figure out what am I doing wrong
#Fabio Many Thanks Is it like this
checkoutServiceMock.Setup(c => c.CheckoutSomethings(It.IsAny<CheckOutSomethingsInput>()))
.Returns(new List<CheckedOutSomething>
{
new CheckedOutSomething { Id = 10001, Remarks = "Success" },
new CheckedOutSomething { Id = 10002, Remarks = "Success" }
});
I actually did and It worked, Thanks Again
So, I'm starting to learn an implement UniTesting on a Web Api project I'm working on. What's happening is that when I setup a mock a call this one returns null instead of the value I'm telling to returns. I don't know why a similiar Setup works but this one just doesn't.
Here is my Test Class
namespace API.Tests.Web
{
[TestClass]
public class MaterialsControllerTest
{
private MaterialsController controller;
private Mock<IRTWRepository> repository;
private Mock<IModelFactory> factory;
private Mock<IRTWAPIIdentityService> identityService;
List<MaterialAccepted> materials;
MaterialAccepted material;
[TestInitialize]
public void Initialize()
{
repository = new Mock<IRTWRepository>();
factory = new Mock<IModelFactory>();
identityService = new Mock<IRTWAPIIdentityService>();
controller = new MaterialsController(repository.Object);
material = new MaterialAccepted()
{
business = true,
businessService = EnumRecycleCenterService.Dropoff,
residential = false,
residentialService = EnumRecycleCenterService.Pickup,
note = "this a note",
Category = new Category()
{
name = "Books"
}
};
materials = new List<MaterialAccepted>()
{
new MaterialAccepted() { business=true,businessService=EnumRecycleCenterService.Dropoff,residential=false,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
new MaterialAccepted() { business=false,businessService=EnumRecycleCenterService.Dropoff,residential=true,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
};
}
[TestMethod]
public void Post_ShouldReturnBadRequestWhenMaterialAcceptedModelValidationFails()
{
//arrange
repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());
controller.ModelState.AddModelError("error", "unit test error");
//act
var actionResult = controller.Post(2, new MaterialAcceptedModel());
Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult));
}
}
}
Here is the action in the Controller I'm trying to test
[HttpPost]
[Route("api/recyclecenters/{rcid}/materials/")]
public IHttpActionResult Post(int rcid, [FromBody]MaterialAcceptedModel model)
{
try
{
if (model != null)
{
var recycleCenter = TheRepository.RecycleCenterRepository.Get(rcid);
if (recycleCenter == null)
return NotFound();
if (!ModelState.IsValid)
return BadRequest(ModelState);
var entity = TheModelFactory.Parse(model);
if (entity == null) return BadRequest("Could not read material accepted in body");
if (TheRepository.MaterialAcceptedRepository.Get(recycleCenter.RecycleCenterId, entity.Category.name) != null)
return Conflict();
recycleCenter.Materials.Add(entity);
if (TheRepository.SaveAll())
{
string locationHeader = Url.Link("Materials", new { rcid = rcid, name = model.category.ToLower() });
return Created<MaterialAcceptedModel>(locationHeader, TheModelFactory.Create(entity));
}
return BadRequest("Could not save to the database");
}
return BadRequest();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
If I run this test it will fail because it returns an instancy type of NotFoundResult instead of a BadRequestResult, and this is happening because the test method stops in this line
if (recycleCenter == null)
return NotFound();
But this test it suppose to stop on this line
if (!ModelState.IsValid)
return BadRequest(ModelState);
Any ideas why this
repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());
is returning null when it should return a new RecycleCenter
It seems like you are setting up the repository mock for rcid = 3, and calling the repository in the controller with rcid = 2.
//arrange
repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());
controller.ModelState.AddModelError("error", "unit test error");
//act
var actionResult = controller.Post(2, new MaterialAcceptedModel());
Try calling it with rcid = 3
var actionResult = controller.Post(3, new MaterialAcceptedModel());
or change the Moq setup parameter to It.IsAny<int>()
It's been a while since I did any MVC work, so I'm hopefully missing something. I'm trying to write a test and controller action to simply Edit a DTO named "Business".
Controller Action:
[HttpPost]
public ActionResult Edit(string id, Business business)
{
try
{
var model = _businessRepository.Get(id);
if (model != null)
{
UpdateModel(model);
if (ModelState.IsValid)
{
_businessRepository.Save(model);
}
else
{
return View(business);
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Test:
[TestMethod]
public void Edit_Post_Action_Updates_Model_And_Redirects()
{
// Arrange
var mockBusinessRepository = new Mock<IBusinessRepository>();
var model = new Business { Id = "1", Name = "Test" };
var expected = new Business { Id = "1", Name = "Not Test" };
// Set up result for business repository
mockBusinessRepository.Setup(m => m.Get(model.Id)).Returns(model);
mockBusinessRepository.Setup(m => m.Save(expected)).Returns(expected);
var businessController = new BusinessController(mockBusinessRepository.Object);
// Act
var result = businessController.Edit(model.Id, expected) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.RouteValues["action"], "Index");
mockBusinessRepository.VerifyAll();
}
The line that it is giving an exception on, is the UpdateModel() in the controller. The exception details are:
"Value cannot be null. Parameter name: controllerContext"
I have some code on Gist that I typically use to set up that ControllerContext. The code is a modified version that was originally taken from Hanselman's blog.
https://gist.github.com/1578697 (MvcMockHelpers.cs)
Setup the Controller context
The following is code snippet from a project which I work on, so maybe it's to much for you
public class TestBase
{
internal Mock<HttpContextBase> Context;
internal Mock<HttpRequestBase> Request;
internal Mock<HttpResponseBase> Response;
internal Mock<HttpSessionStateBase> Session;
internal Mock<HttpServerUtilityBase> Server;
internal GenericPrincipal User;
public void SetContext(Controller controller)
{
Context = new Mock<HttpContextBase>();
Request = new Mock<HttpRequestBase>();
Response = new Mock<HttpResponseBase>();
Session = new Mock<HttpSessionStateBase>();
Server = new Mock<HttpServerUtilityBase>();
User = new GenericPrincipal(new GenericIdentity("test"), new string[0]);
Context.Setup(ctx => ctx.Request).Returns(Request.Object);
Context.Setup(ctx => ctx.Response).Returns(Response.Object);
Context.Setup(ctx => ctx.Session).Returns(Session.Object);
Context.Setup(ctx => ctx.Server).Returns(Server.Object);
Context.Setup(ctx => ctx.User).Returns(User);
Request.Setup(r => r.Cookies).Returns(new HttpCookieCollection());
Request.Setup(r => r.Form).Returns(new NameValueCollection());
Request.Setup(q => q.QueryString).Returns(new NameValueCollection());
Response.Setup(r => r.Cookies).Returns(new HttpCookieCollection());
var rctx = new RequestContext(Context.Object, new RouteData());
controller.ControllerContext = new ControllerContext(rctx, controller);
}
}
Then in your tests you can arrange:
//Arrange
SetContext(_controller);
Context.Setup(ctx => ctx.Request).Returns(Request.Object);
If you want to test your method with ModelState errors, add:
_controller.ModelState.AddModelError("Name", "ErrorMessage");
You need to mock the ControllerContext for your BusinessController.
See this question or this one.
I've managed to get what I wanted working by using Automapper instead of the UpdateModel.
I added in my automapper initialization (IPersistable is an interface for all my DTOs):
Mapper.CreateMap<IPersistable, IPersistable>().ForMember(dto => dto.Id, opt => opt.Ignore());
I then changed my controller action to:
[HttpPost]
public ActionResult Edit(string id, Business business)
{
try
{
var model = _businessRepository.Get(id);
if (model != null)
{
Mapper.Map(business, model);
if (ModelState.IsValid)
{
_businessRepository.Save(model);
}
else
{
return View(business);
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And changed my test to:
[TestMethod]
public void Edit_Post_Action_Updates_Model_And_Redirects()
{
// Arrange
var mockBusinessRepository = new Mock<IBusinessRepository>();
var fromDB = new Business { Id = "1", Name = "Test" };
var expected = new Business { Id = "1", Name = "Not Test" };
// Set up result for business repository
mockBusinessRepository.Setup(m => m.Get(fromDB.Id)).Returns(fromDB);
mockBusinessRepository.Setup(m => m.Save(It.IsAny<Business>())).Returns(expected);
var businessController = new BusinessController(mockBusinessRepository.Object) {ControllerContext = new ControllerContext()};
//Act
var result = businessController.Edit(fromDB.Id, expected) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.RouteValues["action"], "Index");
mockBusinessRepository.VerifyAll();
}
I had the same problem and used the stack trace to pin this down to the ValueProvider. Building on Andrew's answer above for mocking some of the underlying objects used by a controller, I managed to solve the null value exception by also mocking the ValueProvider like this:
var controller = new MyController();
// ... Other code to mock objects used by controller ...
var mockValueProvider = new Mock<IValueProvider>();
controller.ValueProvider = mockValueProvider.Object;
// ... rest of unit test code which relies on UpdateModel(...)