कुछ घंटों के शोध और प्रयास के बाद, मुझे अंत में पता चला कि अपने वेब एपीआई 2 विधियों का पूरी तरह से परीक्षण कैसे IHttpActionResult
करें जो OWIN मिडलवेयर और ASP.NET पहचान के डिफ़ॉल्ट कार्यान्वयन का उपयोग करते हैं।
मैं Get()
निम्नलिखित विधि का परीक्षण करूंगा ApiController
:
public class AccountController : ApiController
{
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager => _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
[Route("api/account"), HttpGet]
public async Task<IHttpActionResult> Get()
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user == null)
{
ModelState.AddModelError(ModelStateConstants.Errors, "Account not found! Try logging out and in again.");
return BadRequest(ModelState);
}
var roles = await UserManager.GetRolesAsync(user.Id);
var accountModel = new AccountViewModel
{
FullName = user.FullName,
Email = user.Email,
Phone = user.PhoneNumber,
Organization = user.Organization.Name,
Role = string.Join(", ", roles)
};
return Ok(accountModel);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_userManager != null)
{
_userManager.Dispose();
_userManager = null;
}
}
base.Dispose(disposing);
}
}
एक बेस क्लास से शुरू करें जो सभी टेस्ट क्लास से इनहेरिट करेगा:
public class BaseTest
{
protected static User CurrentUser;
protected static IList<string> Roles;
public BaseTest()
{
var email = "unit@test.com";
CurrentUser = new User
{
FullName = "Unit Tester",
Email = email,
UserName = email,
PhoneNumber = "123456",
Organization = new Organization
{
Name = "Test Organization"
}
};
Roles = new List<string>
{
"Administrator"
};
}
protected void InitializeApiController(ApiController apiController)
{
//Init fake controller Http and Identity data
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
var routeData = new HttpRouteData(new HttpRoute(""));
apiController.ControllerContext = new HttpControllerContext(config, routeData, request)
{
Configuration = config
};
apiController.User = new GenericPrincipal(new GenericIdentity(""), new[] { "" });
//Initialize Mocks
var appUserMgrMock = GetMockedApplicationUserManager();
var appSignInMgr = GetMockedApplicationSignInManager(appUserMgrMock);
var appDbContext = GetMockedApplicationDbContext();
//Configure HttpContext.Current.GetOwinContext to return mocks
var owin = new OwinContext();
owin.Set(appUserMgrMock.Object);
owin.Set(appSignInMgr.Object);
owin.Set(appDbContext.Object);
HttpContext.Current = new HttpContext(new HttpRequest(null, "http://test.com", null), new HttpResponse(null));
HttpContext.Current.Items["owin.Environment"] = owin.Environment;
}
private static Mock<ApplicationSignInManager> GetMockedApplicationSignInManager(Mock<ApplicationUserManager> appUserMgrMock)
{
var authMgr = new Mock<Microsoft.Owin.Security.IAuthenticationManager>();
var appSignInMgr = new Mock<ApplicationSignInManager>(appUserMgrMock.Object, authMgr.Object);
return appSignInMgr;
}
private Mock<ApplicationUserManager> GetMockedApplicationUserManager()
{
var userStore = new Mock<IUserStore<User>>();
var appUserMgr = new Mock<ApplicationUserManager>(userStore.Object);
appUserMgr.Setup(aum => aum.FindByIdAsync(It.IsAny<string>())).ReturnsAsync(CurrentUser);
appUserMgr.Setup(aum => aum.GetRolesAsync(It.IsAny<string>())).ReturnsAsync(Roles);
return appUserMgr;
}
private static Mock<ApplicationDbContext> GetMockedApplicationDbContext()
{
var dbContext = new Mock<ApplicationDbContext>();
dbContext.Setup(dbc => dbc.Users).Returns(MockedUsersDbSet);
return dbContext;
}
private static IDbSet<User> MockedUsersDbSet()
{
var users = new List<User>
{
CurrentUser,
new User
{
FullName = "Testguy #1",
Email = "test@guy1.com",
UserName = "test@guy1.com",
PhoneNumber = "123456",
Organization = new Organization
{
Name = "Test Organization"
}
}
}.AsQueryable();
var usersMock = new Mock<DbSet<User>>();
usersMock.As<IQueryable<User>>().Setup(m => m.Provider).Returns(users.Provider);
usersMock.As<IQueryable<User>>().Setup(m => m.Expression).Returns(users.Expression);
usersMock.As<IQueryable<User>>().Setup(m => m.ElementType).Returns(users.ElementType);
usersMock.As<IQueryable<User>>().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator);
return usersMock.Object;
}
}
InitializeApiController
विधि मांस और आलू में शामिल है।
अब हम इसके लिए अपने परीक्षण लिख सकते हैं AccountController
:
public class AccountControllerTests : BaseTest
{
private readonly AccountController _accountController;
public AccountControllerTests()
{
_accountController = new AccountController();
InitializeApiController(_accountController);
}
[Test]
public async Task GetShouldReturnOk()
{
var result = await _accountController.Get();
var response = await result.ExecuteAsync(CancellationToken.None);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
सब कुछ काम करने के लिए, आपको Microsoft.OWIN.*
और Microsoft.AspNet.*
पैकेजों का एक समूह स्थापित करना होगा, मैं packages.config
यहाँ अपना पेस्ट करूँगा :
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.3.1" targetFramework="net472" />
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.Owin" version="4.0.1" targetFramework="net472" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.0.1" targetFramework="net472" />
<package id="Microsoft.Owin.Security" version="4.0.1" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Cookies" version="4.0.1" targetFramework="net472" />
<package id="Microsoft.Owin.Security.OAuth" version="4.0.1" targetFramework="net472" />
<package id="Moq" version="4.10.1" targetFramework="net472" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net472" />
<package id="NUnit" version="3.11.0" targetFramework="net472" />
<package id="Owin" version="1.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
</packages>
परीक्षण बहुत सरल है, लेकिन यह दर्शाता है कि सब कुछ काम करता है :-)
खुश परीक्षण!