Test Automapper return collection or not - unit-testing

I am testing one of my services - movieService. GetLatestMovies method should return all movies, ordered by date. I am using automapper to map the entity Movie to MovieViewModel.
* Question 1: * How am I supposed to test that, should I set the return collection in the test or what?
* Question 2: * I am filling the InMemory database with a few movies and I am expecting correct ordered result from the movieService, how am I supposed to check, If the service is returning correct result, If I set the return from the automapper?
TestUtils.FillContextWithActorsMoviesAndGenres(options) - just fills the context with a few movies.
This is the movieService method I am testing
public async Task<ICollection<MovieViewModel>> GetLatestMoviesAsync()
{
var movies = await this.context.Movies
.Include(um => um.ApplicationUserMovie)
.ThenInclude(u => u.User)
.Include(x => x.Genre)
.Include(x => x.MovieActor)
.ThenInclude(x => x.Actor)
.OrderByDescending(x => x.CreatedOn).ToListAsync();
var returnMovies = this.mappingProvider.MapTo<ICollection<MovieViewModel>>(movies);
return returnMovies;
}
[TestMethod]
public async Task Return_TwoMoviesWithHighestRating()
{
var dabataseName = nameof(Return_TwoMoviesWithHighestRating);
var options = TestUtils.GetOptions(dabataseName);
// We fill the context with data and save it.
TestUtils.FillContextWithActorsMoviesAndGenres(options);
var movieOne = new MovieViewModel()
{
Name = "BestRatedMovieTest",
Duration = 90,
Director = "TestDirector",
Storyline = "TestStoryline",
ImageUrl = "TestImageUrl",
Genre = "Comedy"
};
var movieTwo = new MovieViewModel()
{
Name = "SecondMovieTestName",
Duration = 90,
Director = "TestDirector",
Storyline = "TestStoryline",
ImageUrl = "TestImageUrl",
Genre = "Comedy"
};
var collectionMovieViewModels = new List<MovieViewModel>() { movieOne, movieTwo };
var mappingProviderMock = new Mock<IMappingProvider>();
mappingProviderMock
.Setup(x => x.MapTo<ICollection<MovieViewModel>>(It.IsAny<List<Movie>>()))
.Returns(collectionMovieViewModels);
using (var actAndAssertContext = new MovieManagementContext(options))
{
var sut = new MovieService(actAndAssertContext, mappingProviderMock.Object);
var movies = await sut.GetLatestMoviesAsync();
Assert.AreEqual(2, movies.Count());
Assert.AreEqual("BestRatedMovieTest", movies.FirstOrDefault().Name);
}
}

I created an empty collection and set the Callback of the method to fill that collection,
var collectionOfMovies = new List<Movie>();
var mappingProviderMock = new Mock<IMappingProvider>();
mappingProviderMock
.Setup(x => x.MapTo<ICollection<MovieViewModel>>(It.IsAny<List<Movie>>()))
.Callback<object>(inputargs => collectionOfMovies = inputargs as List<Movie>);

Related

how to mock a method of a class

This is my controller code and I need to mock GetTokenDetails() method to conduct XUnit test on DecodeToken function. Am I doing in right way or not?
[HttpGet]
[Authorize(Roles = "Admin")]
[Route("DecodeToken")]
public IActionResult DecodeToken()
{
if (ModelState.IsValid)
{
var tokenResult = GetTokenDetails();
var result = _employeeService.ServiceDecodeToken(tokenResult.UserName, tokenResult.Role);
if (result.Httpcode == 200)
{
return Ok(result);
}
else
{
return StatusCode(500, result);
}
}
else
return BadRequest();
}
public GetTokenDetailsDto GetTokenDetails()
{
var token = HttpContext.Request.Headers["Authorization"].ToString();
var tokenbearer = token.Split(' ');
var handler = new JwtSecurityTokenHandler();
var decodedtoken = handler.ReadJwtToken(tokenbearer[1]);
string user = decodedtoken.Claims.Where(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").FirstOrDefault().ToString();
string role = decodedtoken.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").FirstOrDefault().ToString();
var usr = user.Split(":");
var rol = role.Split(":");
string userName = usr[2].Trim();
string userRole = rol[2].Trim();
GetTokenDetailsDto getTokenDetailsDto = new GetTokenDetailsDto()
{
UserName = userName,
Role = userRole,
};
return getTokenDetailsDto;
}
First, Generally, I don't think there is a right way to do something.
There are multiple right ways to do it :). You just need to pick one or create one.
Second, I suggest you to create a new Dotnet standard Project under the same Solution and separate the logic from controllers. This way, you can create a Unit test project and import only the logic project.
Third, I see some points in your code that you are reading some value from the context( which is not avaiable in testing environment). For example, HttpContext.Request.Headers["Authorization"]. these kind of data should be in the input arguments of the function GetTokenDetails, so you can provide some sample data to test in your UnitTestProject. Something like this:
public GetTokenDetailsDto GetTokenDetails(string token)
{
var tokenbearer = token.Split(' ');
var handler = new JwtSecurityTokenHandler();
var decodedtoken = handler.ReadJwtToken(tokenbearer[1]);
string user = decodedtoken.Claims.Where(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").FirstOrDefault().ToString();
string role = decodedtoken.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").FirstOrDefault().ToString();
var usr = user.Split(":");
var rol = role.Split(":");
string userName = usr[2].Trim();
string userRole = rol[2].Trim();
GetTokenDetailsDto getTokenDetailsDto = new GetTokenDetailsDto()
{
UserName = userName,
Role = userRole,
};
return getTokenDetailsDto;
}

System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider'

I am using testing my Identity actions in my .NET Core web application but keep running into problems. I recently found a way to create a mocked usermanager without running into problems with its parameters, but then a new error came to me that I can't find any solutions too: "System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider'"
Here is my relevant code:
Setting up the mock usermanager:
var _userManager = new Mock<FakeUserManager>();
UserIdentity user1 = new UserIdentity() { Id = UserId1, UserName = "test#gmail.com", Score = 5 };
UserIdentity user2 = new UserIdentity() { Id = UserId2, UserName = "pragim#gmail.com", Score = 1 };
UserIdentity user3 = new UserIdentity() { Id = UserId3, UserName = "ajax#gmail.com", Score = 0 };
UserIdentity user4 = new UserIdentity() { Id = UserId4, UserName = "pim#gmail.com", Score = 4 };
List<UserIdentity> users = new List<UserIdentity>() { user1, user2, user3, user4 };
var mock = users.AsQueryable().BuildMock();
_userManager.Setup(x => x.Users).Returns(mock.Object);
var identityRepository = new IdentityRepository(_userManager.Object, null, null);
_identityService = new IdentityService(identityRepository);
FakeUserManager.cs:
public class FakeUserManager : UserManager<UserIdentity>
{
public FakeUserManager()
: base(new Mock<IUserStore<UserIdentity>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<UserIdentity>>().Object,
new IUserValidator<UserIdentity>[0],
new IPasswordValidator<UserIdentity>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<UserIdentity>>>().Object)
{ }
}
Test method:
[TestMethod()]
public async Task GetUserAsyncTest()
{
//Arrange
//Act
var user = await _identityService.GetUserAsync(UserId4);
//Assert
Assert.AreEqual("pim#gmail.com", user.UserName);
}
Does anyone know a bypass / solution to my problem?
UPDATE: Stack trace:
EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
EntityFrameworkQueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
IdentityRepository.GetUserAsync(Nullable`1 userId) line 55
IdentityService.GetUserAsync(Nullable`1 id) line 172
IdentityServiceTests.GetUserAsyncTest() line 102
ThreadOperations.ExecuteWithAbortSafety(Action action)
and the method GetUserAsync in the repository:
public async Task<UserIdentity> GetUserAsync(Guid? userId)
{
return await _userManager.Users.Where(x => x.Id.Equals(userId)).SingleOrDefaultAsync();
}
Though I am pretty late but after struggling with this error thought should post my bit too.
In my case, a package MockQueryable.Moq(https://www.nuget.org/packages/MockQueryable.Moq/) was being used, upgrading which solved this problem.
The _users sequence needs a provider that implements IAsyncQueryProvider. Couple of ways you can do it, the following is a mocked implementation.
Firstly, some scaffolding based on your OP:
public class UserIdentity : IdentityUser<Guid>
{
public int Score { get; set; }
}
public class IdentityRepository
{
readonly UserManager<UserIdentity> _userManager;
public IdentityRepository(UserManager<UserIdentity> userManager)
{
_userManager = userManager;
}
public async Task<UserIdentity> GetUserAsync(Guid? userId)
{
return await _userManager.Users.SingleOrDefaultAsync(x => x.Id.Equals(userId));
}
}
Create your mocked IAsyncQueryProvider and add it to your own IQueryable<UserIdentity>:
var user1 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "foo#bar.com", Score = 1 };
var user2 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "bar#baz.com", Score = 2 };
var user3 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "tony stark", Score = 3000 };
var dataSource = new List<UserIdentity> { user1, user2, user3 }.AsQueryable();
var providerMock = new Mock<IAsyncQueryProvider>();
providerMock.Setup(x => x.ExecuteAsync<Task<UserIdentity>>(It.IsAny<Expression>(), It.IsAny<CancellationToken>()))
.Returns((Expression providedExpression, CancellationToken providedCancellationToken) => Task.FromResult(dataSource.Provider.Execute<UserIdentity>(providedExpression)));
var usersMock = new Mock<IQueryable<UserIdentity>>();
usersMock.Setup(x => x.ElementType).Returns(dataSource.ElementType);
usersMock.Setup(x => x.Expression).Returns(dataSource.Expression);
usersMock.Setup(x => x.Provider).Returns(providerMock.Object);
...
userManagerMock.Setup(x => x.Users).Returns(() => usersMock.Object);
You need a way to set the IQueryable<T> provider, the above is just one way you can do it.
If we pull this all together into a working LINQPad sample:
void Main()
{
var user1 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "foo#bar.com", Score = 1 };
var user2 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "bar#baz.com", Score = 2 };
var user3 = new UserIdentity() { Id = Guid.NewGuid(), UserName = "tony stark", Score = 3000 };
var dataSource = new List<UserIdentity> { user1, user2, user3 }.AsQueryable();
var providerMock = new Mock<IAsyncQueryProvider>();
providerMock.Setup(x => x.ExecuteAsync<Task<UserIdentity>>(It.IsAny<Expression>(), It.IsAny<CancellationToken>()))
.Returns((Expression providedExpression, CancellationToken providedCancellationToken) => Task.FromResult(dataSource.Provider.Execute<UserIdentity>(providedExpression)));
var usersMock = new Mock<IQueryable<UserIdentity>>();
usersMock.Setup(x => x.ElementType).Returns(dataSource.ElementType);
usersMock.Setup(x => x.Expression).Returns(dataSource.Expression);
usersMock.Setup(x => x.Provider).Returns(providerMock.Object);
var userManagerMock = new Mock<UserManager<UserIdentity>>
(new Mock<IUserStore<UserIdentity>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<UserIdentity>>().Object,
new IUserValidator<UserIdentity>[0],
new IPasswordValidator<UserIdentity>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<UserIdentity>>>().Object);
userManagerMock.Setup(x => x.Users).Returns(() => usersMock.Object);
var identityRepository = new IdentityRepository(userManagerMock.Object);
var result = identityRepository.GetUserAsync(user2.Id).Result;
Console.WriteLine(result);
}
public class UserIdentity : IdentityUser<Guid>
{
public int Score { get; set; }
}
public class IdentityRepository
{
readonly UserManager<UserIdentity> _userManager;
public IdentityRepository(UserManager<UserIdentity> userManager)
{
_userManager = userManager;
}
public async Task<UserIdentity> GetUserAsync(Guid? userId)
{
return await _userManager.Users.SingleOrDefaultAsync(x => x.Id.Equals(userId));
}
}
we get the desired result:
I've left out your IdentityService in the above as essentially it's a detail to the answer; you just need to get a working, mocked UserManager. For my mock libraries, in particular EntityFrameworkCore.Testing, I'd use a concrete IAsyncQueryProvider rather than a mock; follow the link for an example should you want to go down that path.

ASP.NET Core unit test authorization

I try to create unit testing my authorization logic, but have problem for testing
await this.HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme)));
I have error
No authentication handler is configured to handle the scheme: Cookies
Because he need CookieAuthenticationOptions
But how configuration for unit test I don't know
private static Mock<SignInManager<User>> GetSignInManagerMock(Mock<UserManager<User>> userManagerMock)
{
var context = new Mock<HttpContext>();
var contextAccessor = new Mock<IHttpContextAccessor>();
contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
return new Mock<SignInManager<User>>(userManagerMock.Object,
contextAccessor.Object,
new Mock<IUserClaimsPrincipalFactory<User>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<ILogger<SignInManager<User>>>().Object);
}
private static Mock<UserManager<User>> GetUserManagerMock()
{
return new Mock<UserManager<User>>(new Mock<IUserStore<User>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<User>>().Object,
new IUserValidator<User>[0],
new IPasswordValidator<User>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<User>>>().Object);
}
[Fact]
public async void Login_Corect_input_login_password_should_return_ok()
{
var stamp = Guid.NewGuid().ToString();
var user = new User
{
UserName = _fakeUserModel.UserName,
Email = _fakeUserModel.Email,
FirtName = _fakeUserModel.FirstName,
LastName = _fakeUserModel.LastName,
UserPicture = _fakeUserModel.UserPicture,
ConcurrencyStamp = stamp
};
var userManagerMock = GetUserManagerMock();
userManagerMock.Setup(s => s.FindByNameAsync(FakeData.UserName)).ReturnsAsync(user);
userManagerMock.Setup(s => s.GetRolesAsync(user)).ReturnsAsync(FakeData.Roles);
var signInManagerMock = GetSignInManagerMock(userManagerMock);
signInManagerMock.Setup(
s =>
s.PasswordSignInAsync(_fakeCorrectloginModel.UserName, _fakeCorrectloginModel.Password, false,
false))
.ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Success);
var controller = ControllerFactory.CreateFakeController<UserController>(false, userManagerMock.Object,
signInManagerMock.Object);
var response = await controller.Login(_fakeCorrectloginModel);
var result = Assert.IsType<JsonResult>(response);
var getModel = Assert.IsType<UserViewModel>(result.Value);
Assert.Equal(_fakeUserModel, getModel);
}
public static class ControllerFactory
{
public static T CreateFakeController<T>(bool isLoggedIn, params object[] arg) where T : Controller
{
var fakePrincipal = GetPrincipalMock(isLoggedIn).Object;
var fakeActionContext = new ActionContext
{
HttpContext = new DefaultHttpContext
{
User = fakePrincipal
},
ActionDescriptor = new ControllerActionDescriptor(),
RouteData = new RouteData()
};
var controller = (T)Activator.CreateInstance(typeof(T), arg);
controller.ControllerContext = new ControllerContext(fakeActionContext);
return controller;
}
public static Mock<ClaimsPrincipal> GetPrincipalMock(bool isLoggedIn)
{
var principalMock = new Mock<ClaimsPrincipal>();
principalMock.Setup(sg => sg.Identity).Returns(GetIdentityMock(isLoggedIn).Object);
principalMock.Setup(s => s.IsInRole(It.IsAny<string>())).Returns(false);
principalMock.Setup(s => s.Claims).Returns(new List<Claim>
{
GetClaim(HelpClaimTypes.Language, "ua")
});
return principalMock;
}
public static Mock<ClaimsIdentity> GetIdentityMock(bool isLoggedIn)
{
var identityMock = new Mock<ClaimsIdentity>();
identityMock.Setup(sg => sg.AuthenticationType).Returns(isLoggedIn ? FakeData.AuthenticationType : null);
identityMock.Setup(sg => sg.IsAuthenticated).Returns(isLoggedIn);
identityMock.Setup(sg => sg.Name).Returns(isLoggedIn ? FakeData.UserName : null);
return identityMock;
}
public static ClaimsIdentity GetClaimsIdentity(params Claim[] claims)
{
var identityMock = new ClaimsIdentity(claims);
return identityMock;
}
public static Claim GetClaim(string type, string value)
{
return new Claim(type, value);
}
}

How to test Asp.Net Identity UserManger CreateAsync

I am trying to test for failure conditions of my Account controller. When i run the test in debug mode, i am not seeing an expected result. I am expecting to return a failed identity result when reach the line of code to create a user async. however, in debug mode, it does not contain the error i provide it, and the success property is true. according to this site: https://www.symbolsource.org/MyGet/Metadata/aspnetwebstacknightly/Project/Microsoft.AspNet.Identity.Core/2.0.0-rtm-140226/Release/Default/Microsoft.AspNet.Identity.Core/Microsoft.AspNet.Identity.Core/IdentityResult.cs?ImageName=Microsoft.AspNet.Identity.Core, the way i am going about this it "should" work.
what is the right way to setup this test so that when i hit UserManager.CreateAsync, it will return a Failed IdentityResult?
Test i am trying to run
[TestMethod]
public async Task AccountController_Post_register_valid_model_account_creation_fails_returns_exception_result()
{
// arrange
RegisterApiModel model = new RegisterApiModel
{
BusinessType = BusinessType.Architect,
City = "asdf",
CompanyName = "asdf",
Email = "asdf#asdf.com",
FirstName = "asdf",
JobTitle = "asdf",
LastName = "asdf",
OperatingDistance = 123,
Phone = "1231231234",
Password = "12345678",
PostalCode = "asdf",
PrimaryContactName = "asdf",
PrimaryContactPhone = "1231231234",
PrimaryContactTitle = "asdf",
StateId = 2
};
// create http request
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost.com/api/Account/Register");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "Companies" } });
// mock userstore
Mock<IUserStore<ApplicationUser>> userStore = new Mock<IUserStore<ApplicationUser>>();
userStore.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>())).Returns(Task.FromResult(IdentityResult.Failed("Name " + model.Email + " already exists")));
var passwordManager = userStore.As<IUserPasswordStore<ApplicationUser>>();
ApplicationUserManager um = new ApplicationUserManager(userStore.Object);
um.PasswordValidator = pwValidator;
AccountController controller = new AccountController(um);
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
// act
var result = await controller.Register(model);
// assert
result.ShouldBeType(typeof(ExceptionResult));
}
web api method i am trying to test
public async Task<IHttpActionResult> Register([FromBody]RegisterApiModel model)
{
try
{
var company = new Company
{
Name = model.CompanyName,
CreateDate = DateTime.Now,
SubscriptionStatus = SubscriptionStatus.Free,
Address1 = model.Address1 ?? string.Empty,
Address2 = model.Address2 ?? string.Empty,
City = model.City,
StateId = model.StateId,
PostalCode = model.PostalCode,
BusinessType = model.BusinessType.Value,
OperatingDistance = model.OperatingDistance.Value,
Phone = PhoneNumber.ToStorage(model.Phone),
Fax = model.Fax == null ? string.Empty : PhoneNumber.ToStorage(model.Fax),
PrimaryContactName = model.PrimaryContactName,
PrimaryContactPhone = PhoneNumber.ToStorage(model.PrimaryContactPhone),
PrimaryContactTitle = model.PrimaryContactTitle
};
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Company = company, JobTitle = model.JobTitle };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// make user a company admin
user.Claims.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim { ClaimValue = "Admin", ClaimType = "http://bidchuck.com/company/role", UserId = user.Id });
result = await UserManager.UpdateAsync(user);
if (result.Succeeded)
{
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Link("Default", new { controller = "Account", action = "ConfirmEmail", userId = user.Id, code = code });
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: link");
return Ok();
}
}
return BadRequest(result.Errors.First());
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
First of all, you are looking on source of Identity 2.2-alpha1 - it is not released yet. Better get decompiler (I use DotPeek from Jetbrains) and decompile assemblies you use in your project.
Then you are trying to test on too high level. Extract your method into class that is independent from your controllers:
UserService
{
public IdentityResult CreateUser(RegisterApiModel model, String urlCallback)
{
// don't forget to add generated code and userId as parameters into url
// do your user creation.
}
}
In your controller call this service:
public async Task<IHttpActionResult> Register([FromBody]RegisterApiModel model)
{
var urlCallbac = Url.Link("Default", new { controller = "Account", action = "ConfirmEmail" });
var result = await userService.CreateUserAsync(model, urlCallback);
if (result.Succeeded)
{
return Ok();
}
return BadRequest(result.Errors.First());
}
And test user Service separately from controllers. Your tests will become much more simple.
And at the moment it is very difficult to say why you are getting this result. Probably mocks are not completely set up to do what's needed to be done.

How can I mock ServiceStack IHttpRequest

I'm trying to get a unit test working for a service that is injecting items into the IHttpRequest.Items, using a request filter:
this.RequestFilters.Add((req, res, dto) =>
{
// simplified for readability...
var repo = container.Resolve<IClientRepository>();
var apiKey = req.Headers["ApiKey"];
// lookup account code from api key
var accountcode = repo.GetByApiKey(apiKey);
req.Items.Add("AccountCode", accountCode);
});
My service uses that dictionary item:
public class UserService : AppServiceBase
{
public IUserServiceGateway UserServiceGateway { get; set; }
public object Any(UserRequest request)
{
var accountCode = base.Request.Items["AccountCode"].ToString();
var user = UserServiceGateway.GetUserByUsername(request.Name);
return new UserResponse { User = user };
}
}
My test needs be somehow to mock the request, and insert that account code item:
[Test]
public void ValidUsernameReturnUser()
{
// arrange
var gateway = new Mock<IUserServiceGateway>();
gateway.Setup(s => s.GetUserByUsername(It.IsAny<string>()))
.Returns(new UserAccountDTO { Forename = "John", Surname = "Doe" });
var service = new UserService {
UserServiceGateway = gateway.Object,
RequestContext = new MockRequestContext(),
//Request = has no setter
};
// request is this case is null
base.Request.Items.Add("AccountCode", "DEF456");
// act
var response = (UserResponse)service.Any(new UserRequest { Name = "test" });
// assert
Assert.That(response.Result, Is.Not.Null);
}
The service itself accepts a mocked RequestContext, but not a Request. The test therefore fails. Is there a better way to do this?
I think this should do it.
[Test]
public void ValidUsernameReturnUser()
{
// arrange
var mockRequestContext = new MockRequestContext();
//add items to Request
mockRequestContext.Get<IHttpRequest>().Items.Add("AccountCode", "DEF456");
var gateway = new Mock<IUserServiceGateway>();
gateway.Setup(s => s.GetUserByUsername(It.IsAny<string>()))
.Returns(new UserAccountDTO { Forename = "John", Surname = "Doe" });
var service = new UserService {
UserServiceGateway = gateway.Object,
RequestContext = new MockRequestContext(),
};
// act
var response = (UserResponse)service.Any(new UserRequest { Name = "test" });
// assert
Assert.That(response.Result, Is.Not.Null);
}