{"id":88333,"date":"2023-05-09T07:00:48","date_gmt":"2023-05-09T05:00:48","guid":{"rendered":"https:\/\/code-maze.com\/?p=88333"},"modified":"2023-05-09T07:40:56","modified_gmt":"2023-05-09T05:40:56","slug":"aspnetcore-identity-testing-usermanager-rolemanager","status":"publish","type":"post","link":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/","title":{"rendered":"Unit Testing With UserManager and RoleManager in ASP.NET Core Identity"},"content":{"rendered":"<p>In this article, we will learn how to perform Unit Testing with UserManager and RoleManager in ASP.NET Core Identity. Unit testing is an essential practice in software development that helps ensure the correctness and reliability of code. In the context of ASP.NET Core Identity, unit testing becomes even more critical, as it involves sensitive user-related operations such as registration and authentication.<\/p>\n<p>We use the <code>UserManager<\/code> class to perform user-related operations, such as user registration and authentication. Moreover, we use the <code>RoleManager<\/code> class to manage and access user roles in our application. During unit testing, we need a way to mock those objects, to test the functionality of our controllers in isolation.<\/p>\n<p>Here, we will test the user registration process that makes use of both objects. The code is based on <a href=\"https:\/\/code-maze.com\/user-registration-aspnet-core-identity\/\">the article on user registration<\/a> that is part of the <a href=\"https:\/\/code-maze.com\/asp-net-core-identity-series\/\" target=\"_blank\" rel=\"noopener\">ASP.NET Core Identity series<\/a>.<\/p>\n<p>By the end of this article, you will have a comprehensive understanding of how to unit test the user registration process in ASP.NET Core Identity and improve the quality and reliability of your code.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/dotnet-testing\/TestingUserAndRoleManager\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2><a id=\"registration\"><\/a>The User Registration Process<\/h2>\n<p>During registration, the user has to enter his details: first and last name, username, and password. Additionally, the user has to select the appropriate role (Visitor or Administrator).<\/p>\n<p>The registration process is handled by the <code>AccountController<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class AccountController : Controller\r\n{\r\n    private readonly IMapper _mapper;\r\n    private readonly UserManager&lt;User&gt; _userManager;\r\n    private readonly RoleManager&lt;IdentityRole&gt; _roleManager;\r\n\r\n    public AccountController(IMapper mapper, \r\n        UserManager&lt;User&gt; userManager, RoleManager&lt;IdentityRole&gt; roleManager)\r\n    {\r\n        _mapper = mapper;\r\n        _userManager = userManager;\r\n        _roleManager = roleManager;\r\n    }\r\n    \/\/other methods omitted\r\n}<\/pre>\n<p>The <code>Register()<\/code> method in the Account controller handles the registration POST request and uses <code>UserManager<\/code> and <code>RoleManager<\/code> to create a new user and assign them a role:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPost]\r\n[ValidateAntiForgeryToken]\r\npublic async Task&lt;IActionResult&gt; Register(UserRegistrationModel userModel)\r\n{\r\n    ViewData[\"roles\"] = _roleManager.Roles.ToList();\r\n\r\n    if (!ModelState.IsValid)\r\n    {\r\n        return View(userModel);\r\n    }\r\n\r\n    var user = _mapper.Map&lt;User&gt;(userModel);\r\n\r\n    var result = await _userManager.CreateAsync(user, userModel.Password);\r\n    if(!result.Succeeded)\r\n    {\r\n        foreach (var error in result.Errors)\r\n        {\r\n            ModelState.TryAddModelError(error.Code, error.Description);\r\n        }\r\n\r\n        return View(userModel);\r\n    }\r\n\r\n    await _userManager.AddToRoleAsync(user, userModel.Role);\r\n\r\n    return RedirectToAction(nameof(HomeController.Index), \"Home\");\r\n}<\/pre>\n<p>Let&#8217;s see how to define those roles in the application.<\/p>\n<p>Initially, let&#8217;s create a new class (<code>RoleConfiguration<\/code>) that implements the <code>IEntityTypeConfiguration&lt;IdentityRole&gt;<\/code> interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class RoleConfiguration : IEntityTypeConfiguration&lt;IdentityRole&gt;\r\n{\r\n    public void Configure(EntityTypeBuilder&lt;IdentityRole&gt; builder)\r\n    {\r\n        builder.HasData(\r\n            new IdentityRole\r\n            {\r\n                Name = \"Visitor\",\r\n                NormalizedName = \"VISITOR\"\r\n            },\r\n            new IdentityRole\r\n            {\r\n                Name = \"Administrator\",\r\n                NormalizedName = \"ADMINISTRATOR\"\r\n            });\r\n    }\r\n}<\/pre>\n<p><code>RoleConfiguration<\/code> creates two new <code>IdentityRole<\/code> objects, named <code>Visitor<\/code> and <code>Administrator<\/code> respectively.<\/p>\n<p>Next, let&#8217;s override the <code>OnModelCreating()<\/code> method in the database context file (<code>ApplicationContext.cs<\/code>), to apply the new configuration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n{\r\n    base.OnModelCreating(modelBuilder);\r\n    modelBuilder.ApplyConfiguration(new RoleConfiguration());\r\n}<\/pre>\n<p>Now, the new roles are available to the Account controller and are displayed with a drop-down box in the respective view. Next, let&#8217;s proceed with unit testing of this controller.<\/p>\n<h2><a id=\"unit_testing\"><\/a>Unit Testing With UserManager and RoleManager<\/h2>\n<p>In order to unit test the registration controller in isolation, we will need to mock the <code>UserManager<\/code> and <code>RoleManager<\/code> objects. Mocking helps us simulate the external dependencies of the class under test. We will use <a href=\"https:\/\/github.com\/moq\/moq4\" target=\"_blank\" rel=\"nofollow noopener\">Moq<\/a>, a mock object framework for .NET that helps us create mock objects for unit testing.<\/p>\n<p>First of all, let&#8217;s create a new xUnit test project and let&#8217;s add support for Moq. We will begin by testing a successful registration to ensure that the controller creates a new user and assigns them a role correctly.<\/p>\n<h3>Successful Registration Scenario<\/h3>\n<p>First, let&#8217;s define the user registration model and the user object that would be created by the object mapper:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var userRegistrationModel = new UserRegistrationModel()\r\n{\r\n    FirstName = \"Test1\",\r\n    LastName = \"Test2\",\r\n    Email = \"Test3\",\r\n    Password = \"Test4\",\r\n    ConfirmPassword = \"Test4\"\r\n};\r\n\r\nvar user = new User()\r\n{\r\n    UserName = \"Test3\",\r\n    FirstName = \"Test1\",\r\n    LastName = \"Test2\",\r\n    Email = \"Test3\"\r\n};\r\n<\/pre>\n<p>Next, let&#8217;s mock the <code>UserManager<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var userManagerMock = new Mock&lt;UserManager&lt;User&gt;&gt;(\r\n    new Mock&lt;IUserStore&lt;User&gt;&gt;().Object,\r\n    new Mock&lt;IOptions&lt;IdentityOptions&gt;&gt;().Object,\r\n    new Mock&lt;IPasswordHasher&lt;User&gt;&gt;().Object,\r\n    new IUserValidator&lt;User&gt;[0],\r\n    new IPasswordValidator&lt;User&gt;[0],\r\n    new Mock&lt;ILookupNormalizer&gt;().Object,\r\n    new Mock&lt;IdentityErrorDescriber&gt;().Object,\r\n    new Mock&lt;IServiceProvider&gt;().Object,\r\n    new Mock&lt;ILogger&lt;UserManager&lt;User&gt;&gt;&gt;().Object);\r\n\r\nuserManagerMock\r\n    .Setup(userManager =&gt; userManager.CreateAsync(It.IsAny&lt;User&gt;(), It.IsAny&lt;string&gt;()))\r\n    .Returns(Task.FromResult(IdentityResult.Success));\r\nuserManagerMock\r\n    .Setup(userManager =&gt; userManager.AddToRoleAsync(It.IsAny&lt;User&gt;(), It.IsAny&lt;string&gt;()));\r\n<\/pre>\n<p>For the <code>UserManager<\/code> mock object, we are mocking the two methods (<code>CreateAsync()<\/code> and <code>AddToRoleAsync()<\/code>).<\/p>\n<p>Now, let&#8217;s mock the <code>RoleManager<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var list = new List&lt;IdentityRole&gt;()\r\n{\r\n    new IdentityRole(\"Administrator\"),\r\n    new IdentityRole(\"Visitor\")\r\n}\r\n.AsQueryable();\r\n\r\nvar roleManagerMock = new Mock&lt;RoleManager&lt;IdentityRole&gt;&gt;(\r\n    new Mock&lt;IRoleStore&lt;IdentityRole&gt;&gt;().Object,\r\n    new IRoleValidator&lt;IdentityRole&gt;[0],\r\n    new Mock&lt;ILookupNormalizer&gt;().Object,\r\n    new Mock&lt;IdentityErrorDescriber&gt;().Object,\r\n    new Mock&lt;ILogger&lt;RoleManager&lt;IdentityRole&gt;&gt;&gt;().Object);\r\n\r\nroleManagerMock\r\n    .Setup(r =&gt; r.Roles).Returns(list);<\/pre>\n<p>The <code>RoleManager<\/code> mock object will return a list of <code>IdentityRole<\/code> objects when we access the <code>Roles<\/code> property.<\/p>\n<p>Finally, let&#8217;s also mock the <code>Mapper<\/code> object and use it, along with the other two mock objects, to instantiate the <code>AccountController<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var mapperMock = new Mock&lt;IMapper&gt;();\r\nmapperMock.Setup(m =&gt; m.Map&lt;User&gt;(userRegistrationModel)).Returns(user);\r\n\r\nvar controller = new AccountController(mapperMock.Object, userManagerMock.Object, roleManagerMock.Object);\r\nvar result = (RedirectToActionResult) await controller.Register(userRegistrationModel);\r\n\r\nAssert.Equal(\"Index\", result.ActionName);\r\n<\/pre>\n<h3>Failed Registration Scenario<\/h3>\n<p>We will also test a scenario where the registration fails due to an existing username:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var userManagerMock = new Mock&lt;UserManager&lt;User&gt;&gt;(\r\n    new Mock&lt;IUserStore&lt;User&gt;&gt;().Object,\r\n    new Mock&lt;IOptions&lt;IdentityOptions&gt;&gt;().Object,\r\n    new Mock&lt;IPasswordHasher&lt;User&gt;&gt;().Object,\r\n    new IUserValidator&lt;User&gt;[0],\r\n    new IPasswordValidator&lt;User&gt;[0],\r\n    new Mock&lt;ILookupNormalizer&gt;().Object,\r\n    new Mock&lt;IdentityErrorDescriber&gt;().Object,\r\n    new Mock&lt;IServiceProvider&gt;().Object,\r\n    new Mock&lt;ILogger&lt;UserManager&lt;User&gt;&gt;&gt;().Object);\r\n\r\nvar identityErrors = new IdentityError[]\r\n{\r\n    new IdentityError()\r\n    {\r\n        Code = \"Username already exists\",\r\n        Description = \"Username already exists\"\r\n    }\r\n};\r\nuserManagerMock\r\n    .Setup(userManager =&gt; userManager.CreateAsync(It.IsAny&lt;User&gt;(), It.IsAny&lt;string&gt;()))\r\n    .Returns(Task.FromResult(IdentityResult.Failed(identityErrors)));\r\nuserManagerMock\r\n    .Setup(userManager =&gt; userManager.AddToRoleAsync(It.IsAny&lt;User&gt;(), It.IsAny&lt;string&gt;()));\r\n<\/pre>\n<p>Here, we set up the <code>CreateAsync()<\/code> method to return an <code>IdentityError<\/code> object, indicating that the provided username already exists in the system. As a result, the original registration page loads again and displays the error message:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var controller = new AccountController(mapperMock.Object, userManagerMock.Object, roleManagerMock.Object);\r\nvar result = (ViewResult)await controller.Register(userRegistrationModel);\r\n\r\nAssert.Null(result.ViewName);<\/pre>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we have learned how to perform Unit Testing with UserManager and RoleManager in ASP.NET Core Identity. By using mock objects, we were able to isolate the behavior of the controller and test it in a controlled environment. As a result, we can be more confident in the correctness of our code and catch errors early on in the development process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn how to perform Unit Testing with UserManager and RoleManager in ASP.NET Core Identity. Unit testing is an essential practice in software development that helps ensure the correctness and reliability of code. In the context of ASP.NET Core Identity, unit testing becomes even more critical, as it involves sensitive user-related [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1112],"tags":[629,1765,173,1766],"class_list":["post-88333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-testing","tag-asp-net-core-identity","tag-rolemanager","tag-unit-testing","tag-usermanager","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Testing UserManager and RoleManager in ASP.NET Core Identity<\/title>\n<meta name=\"description\" content=\"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing UserManager and RoleManager in ASP.NET Core Identity\" \/>\n<meta property=\"og:description\" content=\"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-09T05:00:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-09T05:40:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Unit Testing With UserManager and RoleManager in ASP.NET Core Identity\",\"datePublished\":\"2023-05-09T05:00:48+00:00\",\"dateModified\":\"2023-05-09T05:40:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\"},\"wordCount\":642,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"keywords\":[\"Asp.NET Core Identity\",\"RoleManager\",\"Unit Testing\",\"UserManager\"],\"articleSection\":[\"Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\",\"url\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\",\"name\":\"Testing UserManager and RoleManager in ASP.NET Core Identity\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"datePublished\":\"2023-05-09T05:00:48+00:00\",\"dateModified\":\"2023-05-09T05:40:56+00:00\",\"description\":\"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"width\":1100,\"height\":620,\"caption\":\"ASP.NET Core\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unit Testing With UserManager and RoleManager in ASP.NET Core Identity\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Testing UserManager and RoleManager in ASP.NET Core Identity","description":"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/","og_locale":"en_US","og_type":"article","og_title":"Testing UserManager and RoleManager in ASP.NET Core Identity","og_description":"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.","og_url":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/","og_site_name":"Code Maze","article_published_time":"2023-05-09T05:00:48+00:00","article_modified_time":"2023-05-09T05:40:56+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Unit Testing With UserManager and RoleManager in ASP.NET Core Identity","datePublished":"2023-05-09T05:00:48+00:00","dateModified":"2023-05-09T05:40:56+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/"},"wordCount":642,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","keywords":["Asp.NET Core Identity","RoleManager","Unit Testing","UserManager"],"articleSection":["Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/","url":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/","name":"Testing UserManager and RoleManager in ASP.NET Core Identity","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","datePublished":"2023-05-09T05:00:48+00:00","dateModified":"2023-05-09T05:40:56+00:00","description":"In this article, we will learn how to perform unit testing when using the UserManager and RoleManager objects in our code.","breadcrumb":{"@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","width":1100,"height":620,"caption":"ASP.NET Core"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/aspnetcore-identity-testing-usermanager-rolemanager\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Unit Testing With UserManager and RoleManager in ASP.NET Core Identity"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/88333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=88333"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/88333\/revisions"}],"predecessor-version":[{"id":88335,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/88333\/revisions\/88335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62187"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=88333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=88333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=88333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}