Struggling with Moq: The following setups were not matched - c#

I'm using Moq for the first time, and I'm struggling with getting the tests to run properly.
I'm trying to moq the Save() method of my service layer.
public void Save(UserViewModel viewModel)
{
// todo: this still doesn't address updating a password. The UserViewModel doesn't contain any Password data.
if (viewModel.Id != Guid.Empty)
{
// The UserId is not empty, we're either updating an existing user
// or we're inserting a new user via sync
var user = _userRepository.GetById(viewModel.Id);
if (user != null)
{
// Looks like we're updating a user because they're already in the database.
_userRepository.Update(_userViewModelToModelMapper.BuildFrom(viewModel));
return;
}
}
// The user is being created, either via a Sync (Guid Exists), or via an Insert (Guid doesn't Exist)
_userRepository.Create(_userViewModelToModelMapper.BuildFrom(viewModel));
}
I've got Three tests, of which, I'm not sure any of them are right. The first two are passing, but the third one fails with
Moq.MockVerificationException : The following setups were not matched:
IUserRepository r => r.Update(It.Is(um => um.Equals()))
Here are the tests.
// PASSES but could be suspect
[Test]
public void ShouldSaveANewUserFromExistingId()
{
// emulating a "sync"
// Setup
var userId = new Guid("81C7FE19-2DB5-4083-BD6A-5433687561F7");
var userModel = new UserModel();
var userViewModel = new UserViewModel {Id = userId};
var userRepository = new Mock<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Create(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
// PASSES but could be suspect.
[Test]
public void ShouldSaveANewUser()
{
// emulating a standard create
// Setup
var userId = Guid.Empty;
var userModel = new UserModel();
var userViewModel = new UserViewModel { Id = userId };
var userRepository = new Mock<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Create(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
// FAILS MISERABLY
[Test]
public void ShouldSaveAnExistingUser()
{
// emulating an "Update"
// Setup
var userId = new Guid("0A88AEC2-9F8D-44DE-BD01-3EB9A23C78E3");
var userModel = new UserModel { Id = userId };
var userViewModel = new UserViewModel { Id = userId };
var userRepository = new Mock<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Update(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
Where am I going wrong with my Moqing?

Seems like in the last case you would need:
userRepository.Setup(r => r.GetById(userId)).Returns(userModel);
Before the userService.Save call. Without it userRepository.GetById will always be null and the update branch will never be taken.

Related

Missing type map configuration or unsupported mapping on IdentityRole

I am writing unit tests and this mapper is causing me grief again. I understood from a previous post that I cannot Mock the mapper, i have to use it straight away. So I have created maps but it says missing type map configuration.
public RoleDto GetSingle([FromRoute] string id)
{
var r = roleManagerWrapper.GetSingleRole(id);
return mapper.Map<RoleDto>(r);
}
It breaks when it tries to map the object. Is there any special mapping for Task <IdentityRole> that needs to be implemented?
public async Task<IdentityRole> GetSingleRole(string roleId)
{
var role = await this.roleManager.Roles.SingleOrDefaultAsync(r => r.Id == roleId);
return role;
}
Here is my test that only counts the number of roles that are created.
[Test]
public async Task Get_Single()
{
TestSetup();
var roleManagerWrapperMock = new Mock<IRoleManagerWrapper>();
var adminRole = new IdentityRole()
{
Name = "Admin",
Id = "4a8de423-5663-4831-ac07-7ce92465b008"
};
var managerRole = new IdentityRole()
{
Name = "Manager",
Id = "40f74162-3359-4253-9b5a-ad795b328267"
};
ApplicationDbContext.Roles.Add(managerRole);
ApplicationDbContext.Roles.Add(adminRole);
ApplicationDbContext.SaveChanges();
var sut = new RolesController(roleManagerWrapperMock.Object, ApplicationDbContext, Mapper);
var result = sut.GetSingle("4a8de423-5663-4831-ac07-7ce92465b008");
Assert.AreEqual(result.UserCount, 1);
}
protected void TestSetup(string databaseName = null)
{
if (databaseName == null)
{
databaseName = GetTestName();
}
TestCleanup();
ServiceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName)
.UseInternalServiceProvider(ServiceProvider)
.Options;
ApplicationDbContext = new ApplicationDbContext(dbContextOptions);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<RoleDto, IdentityRole>();
cfg.CreateMap<IdentityRole, RoleDto>();
cfg.CreateMap<CreateRoleDto, IdentityRole>().ReverseMap();
cfg.CreateMap<UpdateRoleDto, IdentityRole>().ReverseMap();
});
Mapper = config.CreateMapper();
}
The action needs to be refactored to use proper asyn syntax since GetSingleRole returns Task<IdentityRole>
public Task<RoleDto> GetSingle([FromRoute] string id) {
IdentityRole r = await roleManagerWrapper.GetSingleRole(id);
return mapper.Map<RoleDto>(r);
}
And the test updated accordingly
[Test]
public async Task Get_Single() {
//Arrange
TestSetup();
var roleManagerWrapperMock = new Mock<IRoleManagerWrapper>();
//...omitted for brevity
var sut = new RolesController(roleManagerWrapperMock.Object, ApplicationDbContext, Mapper);
//Act
RoleDto result = await sut.GetSingle("4a8de423-5663-4831-ac07-7ce92465b008");
//Assert
Assert.AreEqual(result.UserCount, 1);
}

Returning users from mocked ApplicationUserManager.Users

I am trying to mock ApplicationUserManager.Users so that I can test a controller method. My controller is
public class ManageController : Controller
{
private readonly ApplicationUserManager userManager;
public ManagerController(ApplicationUserManager userManager)
{
this.userManager = userManager;
}
// The method being tested
public ActionResult ListUsers()
{
// I want this to return my mocked user list
var users = this.userManager.Users.ToList();
// Do other stuff
}
}
My test method is
[TestMethod]
public void ListUsersGet_ShouldSucceed()
{
var users = new List<ApplicationUser>
{
new ApplicationUser { Id = "1", FirstName = "Test", LastName = "User" }
}.AsQueryable();
Mock<DbSet<ApplicationUser>> dbSet = new Mock<DbSet<ApplicationUser>>();
dbSet.As<IQueryable<ApplicationUser>>().Setup(e => e.ElementType).Returns(users.ElementType);
dbSet.As<IQueryable<ApplicationUser>>().Setup(e => e.Expression).Returns(users.Expression);
dbSet.As<IQueryable<ApplicationUser>>().Setup(e => e.GetEnumerator()).Returns(users.GetEnumerator());
dbSet.As<IQueryable<ApplicationUser>>().Setup(e => e.Provider).Returns(users.Provider);
Mock<MyContext> context = new Mock<MyContext>();
context.Setup(e => e.Users).Returns(dbSet.Object);
Mock<UserStore<ApplicationUser>> userStore = new Mock<UserStore<ApplicationUser>>(context.Object);
var controller = new ManageController(new ApplicationUserManager(userStore.Object));
var result = controller.ListUsers() as ViewResult;
// Assert some stuff
}
When I step into my controller method, I can see that users is null. It appears that this.userManager.Users isn't returning the users that I tried to set up in my test method.
How can I get this.userManager.Users.ToList() to return my test users?
User manager exposes public virtual IQueryable<TUser> Users { get; } property. Mock the user manager dependency and give that to the controller.
public void _ListUsersGet_ShouldSucceed() {
// Arrange.
var users = new List<ApplicationUser>
{
new ApplicationUser { Id = "1", FirstName = "Test", LastName = "User" }
}.AsQueryable();
//Only mocking this because we need it to initialize manager.
var userStore = Mock.Of<IUserStore<ApplicationUser>>();
var userManager = new Mock<ApplicationUserManager>(userStore);
userManager.Setup(_ => _.Users).Returns(users);
var controller = new ManageController(userManager.Object);
// Act.
var result = controller.ListUsers() as ViewResult;
// Assert some stuff
}
Check the Quick start documentation to get more acquainted with the mocking framework

How to Fake this get property into one of my controller tests

ignore everything
ignore everything
public class BaseController : ApiController
{
ignore everything
public UserIdentity => User.Identity as ;
public int UserId => Int32.Parse(Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value);
}
This is my class
Create a fake principal and give that to the controller before exercising the unit test
[TestMethod()]
public void BaseController_When_Authenticated_Should_Return_UserId() {
//Arrange
//Create test user
var expected = 12345;
var identity = new LtfClaimsIdentity("Ltf"); // Or how ever it is you initialize your identity
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, expected.ToString()));
var user = new ClaimsPrincipal(identity);
// Set the User on the controller directly
var controller = new BaseController () {
User = user
};
//Act
var actual = controller.UserId;
//Assert
Assert.AreEqual(expected, actual);
}

ASP MVC Unit Testing GetUserId()

I have a controller, where I'm calling a userId:
var userId = User.Identity.GetUserId();
This makes my unit test fail, since User is null.
I've tried to set a user in the test method using this method, but User is still null in the controller when the test runs.
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
Any hints as to what I'm doing wrong?
The HttpContextBase.User is of type IPrincipal and you have not mocked it. That's why it is returning null when you access it in tests. You can mock it as follows
var controllerContext = new Mock<ControllerContext>();
var principal = new Moq.Mock<IPrincipal>();
principal.Setup(p => p.IsInRole("Administrator")).Returns(true);
principal.SetupGet(x => x.Identity.Name).Returns(userName);
controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
controller.ControllerContext = controllerContext.Object;
Here is the reference of this code
Okay, so according to this post, you can do it like this:
public class MyController: Controller
{
public Func<string> GetUserId; //For testing
public MyController()
{
GetUserId = () => User.Identity.GetUserId();
}
//controller actions
}
And then instead of caling User.Identity.GetUserId() when you want the user, you just call GetUserId() and mock out the user id in your test:
controller = new MyController()
{
GetUserId = () => "IdOfYourChoosing"
};
You are mocking HttpContextBase but are not passing it to your controller.
Try this pattern:
Class:
public MyClass
{
private readonly HttpContextBase _contextBase;
public MyClass(HttpContextBase contextBase)
{
this._contextBase = contextBase;
}
public void Process()
{
var userId = _contextBase.User.Identity;
}
}
Test:
[Test]
public void MyClass_Test_SO()
{
// arrange
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
var sut = new MyClass(context.Object);
// act
sut.Process();
// assert
// ... whatever
}

How to mock HttpContext in a ShoppingCart controller/model

In our MVC4 application with Entity Framework 4.0 based on the Music Store Tutorial we are using Moq to mock the DbContext and unit test are logic. One of our methods proves difficult to test though since it makes use of HttpContext or HttpContextBase. One example method looks like this:
public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}
The only property collected from HttpContextBase is the [CartSessionKey] as can be seen here:
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
We have heard horror stories that HttpContext is a very complex class and that if you print it you have enough paper to circle the earth eight times.
Nevertheless we want to mock it. The question is how. The properties that we want to mock are the [CartSessionKey], and the property that come from the context as contest.User.Identity.Name.
We suspect we need to use something like this:
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
Mock<HttpRequestBase> mockHttpRequest = new Mock<HttpRequestBase>();
mockHttpRequest.Setup(x => x.CartSessionKey).Returns(1);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
but we cannot find how to specifically implement this so we do not get any errors on methods that use context.Session[CartSessionKey] or context.User.Identity.Name.
We hope someone can help us out.
/edit
When we do this:
var memoryUserItems = new FakeDbSet<User>()
{
new User { Email = "test#test.de",
FullName = "Test Person",
isAvailable = true,
Name = "WHat"
},
new User { Email = "test2#test.de",
FullName = "Test Person 2",
isAvailable = true,
Name = "WHat 2"
}
};
(...) Other memory...Items
And then this:
// Create mock units of work
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
mockData.Setup(m => m.Users).Returns(memoryUserItems);
var principalMock = new Mock<IPrincipal>();
var identityMock = new Mock<IIdentity>();
var userMock =
identityMock.Setup(x => x.Name).Returns("Test!");
identityMock.Setup(x => x.IsAuthenticated).Returns(true); // optional ;)
mockData.Setup(x => x.Identity).Returns(identityMock.Object);
var httpReqBase = new Mock<HttpRequestBase>(); // this is useful if you want to test Ajax request checks or cookies in the controller.
var httpContextBase = new Mock<HttpContextBase>();
httpContextBase.Setup(x => x.User).Returns(principalMock.Object);
httpContextBase.Setup(x => x.Session[It.IsAny<string>()]).Returns(1); //Here is the session indexer. You can swap 'any' string for specific string.
httpContextBase.Setup(x => x.Request).Returns(httpReqBase.Object);
We get the error that:
Error 3 'project.Models.FakeContext' does
not contain a definition for 'Identity' and no extension method
'Identity' accepting a first argument of type
'project.Models.FakeContext' could be found
(are you missing a using directive or an assembly
reference?)
/ edit2
To make it more clear. The actual method I am testing is the following:
public ActionResult Complete(int id)
{
// Make sure that user is currentuser and otherwise bring user to our Thief page
if (id != db.GetCurrentUserId())
{
return View("Thief");
}
var cart = ShoppingCart.GetCart(this.HttpContext);
var currentDate = DateTime.Today;
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal(),
ProductItems = db.Products.ToList()
};
if (viewModel.CartItems.Count() == 0)
{
return View("Empty");
}
// Try to write cart to order table
try
{
foreach (var item in viewModel.CartItems)
{
ProcessOrder(item, id, currentDate);
}
// after this we empty the shopping cart
cart.EmptyCart();
return View();
}
catch
{
// Invalid - display error page
return View("Error");
}
}
As can be seen the var cart = ShoppingCart.GetCart(this.HttpContext); uses this.HttpContext. In the test I just do controller.Complete(1). I cannot pass a new HttpContext to the controller I guess?
/ edit 3
While using the code below with the mocks I get the following message:
Test Name: TestCheckoutCompleteShouldWithEmptyCart
Test FullName: Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart
Test Source: Controllers\CheckoutControllerTest.cs : line 141
Test Outcome: Failed
Test Duration: 0:00:00.0158591
Result Message:
Test method Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Result StackTrace:
at Models\ShoppingCart.cs:line 170
at \Models\ShoppingCart.cs:line 20
at \Controllers\CheckoutController.cs:line 48
at Controllers\CheckoutControllerTest.cs:line 143
OK, here it goes. The following works in MVC5 with AD, I'm not sure if it's fully backwards compatible, you'll have to check.
var principalMock = new Mock<IPrincipal>();
var identityMock = new Mock<IIdentity>();
identityMock.Setup(x => x.Name).Returns("Test!");
identityMock.Setup(x => x.IsAuthenticated).Returns(true); // optional ;)
userMock.Setup(x => x.Identity).Returns(identityMock.Object);
var httpReqBase = new Mock<HttpRequestBase>(); // this is useful if you want to test Ajax request checks or cookies in the controller.
var httpContextBase = new Mock<HttpContextBase>();
httpContextBase.Setup(x => x.User).Returns(principalMock.Object);
httpContextBase.Setup(x => x.Session[It.IsAny<string>()]).Returns(1); //Here is the session indexer. You can swap 'any' string for specific string.
httpContextBase.Setup(x => x.Request).Returns(httpReqBase.Object);
This would help you to write a proper Unit Test using Moq.
[TestClass]
public class SutTest
{
[TestMethod]
public void GetCartId_WhenUserNameIsNotNull_SessionContainsUserName()
{
var httpContextStub = new Mock<HttpContextBase>();
var httpSessionStub = new Mock<ISessionSettings>();
httpSessionStub.Setup(x => x.Get<string>(It.IsAny<string>())).Returns(() => null);
httpSessionStub.SetupSequence(x => x.Get<string>(It.IsAny<string>()))
.Returns(null)
.Returns("FakeName");
var httpUserStub = new Mock<IPrincipal>();
var httpIdenttyStub = new Mock<IIdentity>();
httpUserStub.SetupGet(x => x.Identity).Returns(httpIdenttyStub.Object);
httpIdenttyStub.SetupGet(x => x.Name).Returns("FakeName");
httpContextStub.Setup(x => x.User).Returns(httpUserStub.Object);
var sut = new Sut(httpSessionStub.Object);
var result = sut.GetCartId(httpContextStub.Object);
Assert.AreEqual("FakeName",result );
}
}
Check the SetupSequence method which gives you find Control over different values being return on he same stubbed call.
Also important to decouple your session from HttpContext as you can always run into issues.
public class SessionSettings : ISessionSettings
{
private readonly HttpSessionStateBase _session;
public SessionSettings(HttpSessionStateBase session)
{
_session = session;
}
public T Get<T>(string key)
{
return (T)_session[key];
}
public void Set<T>(string key, T value)
{
_session[key] = value;
}
}
public interface ISessionSettings
{
T Get<T>(string key);
void Set<T>(string key, T value);
}
public class Sut
{
private ISessionSettings _sessionSettings;
public Sut(ISessionSettings sessionSettings)
{
_sessionSettings = sessionSettings;
}
public string GetCartId(HttpContextBase context)
{
if (_sessionSettings.Get<string>(CartSessionKey) == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
_sessionSettings.Set<string>(CartSessionKey, context.User.Identity.Name);
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
_sessionSettings.Set<string>(CartSessionKey, tempCartId.ToString());
}
}
return _sessionSettings.Get<string>(CartSessionKey);
}
private string CartSessionKey = "key";
}
This way the code is more readable and easier to understand.

Categories