Mock ImongoCollection driver 2.0.1 C# - c#

I'm trying to write unit test for my repository layer that is using MongoDb 2.0.1 driver.
I have a dependency of ImongoCollection.
Someone knows how to mock that avoiding to use real db?
Thank you
Following is the code:
public class Repository<T> : IRepository<T>
where T : Content
{
private readonly IMongoCollection<T> _mongoCollection;
public Repository(IMongoCollection<T> mongoCollection)
{
_mongoCollection = mongoCollection;
}
public void Insert(T entity)
{
_mongoCollection.InsertOneAsync(entity);
}
public async void Update(T entity)
{
await _mongoCollection.InsertOneAsync(entity);
}
public async void Delete(string id)
{
await _mongoCollection.DeleteOneAsync(Builders<T>.Filter.Where(x => x.Id == id));
}
public async Task<T> GetById(string id)
{
return await _mongoCollection.Find(Builders<T>.Filter.Where(x => x.Id == id)).FirstOrDefaultAsync();
}
/// <summary>
/// This method will retrieve a list of items
/// by passing a dynamic mongo query
/// Eg. AND - var filter = builder.Eq("cuisine", "Italian") & builder.Eq("address.zipcode", "10075");
/// Eg. OR - var filter = builder.Eq("cuisine", "Italian") | builder.Eq("address.zipcode", "10075");
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<List<T>> GetByFilter(FilterDefinition<T> filter)
{
filter = filter ?? new BsonDocument();
return await _mongoCollection.Find(filter).ToListAsync();
}
public async Task<List<T>> GetAll()
{
return await _mongoCollection.Find(new BsonDocument()).ToListAsync();
}
}
I'm trying someting like
private Mock<IMongoCollection<Content>> _mockContentCollection;
private IRepository<Content> _contentRepository;
[SetUp]
public void TestMethod1()
{
_mockContentCollection = new Mock<IMongoCollection<Content>>();
_mockContentCollection.Setup(x => x.)....
_contentRepository = new Repository<Content>(_mockContentCollection.Object);
}
[Test]
public void GetAll_With_Results()
{
var a = _contentRepository.GetAll();
}

Related

.Net Core not routing to POST Method but GET Method works

I'm training with the sample web application.
For unknown reason .Net Core does not route POST requests to corresponding method.
Get methods are working but POST not even being fired.
POST requests return 404.
I already tried to add annotation like that but it didn't help.
Could someone pleas
[HttpPost("envelope")]
Here's the code of my controller.
namespace Receiver.Controllers
{
[Route("[controller]")]
[ApiController]
public class EnvelopesController : ControllerBase
{
private readonly ReceiverDbContext _dbContext;
private readonly ILogger _logger;
public EnvelopesController(ReceiverDbContext dbContext, ILogger logger)
{
_dbContext = dbContext;
_logger = logger;
}
/// <summary>
/// GET (Read all) /envelopes
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<IEnumerator<EnvelopeDTO>>> GetAll()
{
_logger.LogInformation("Hello from the Get() method!");
var envelopes = await _dbContext.Envelope.Include(e => e.Type).ToArrayAsync();
return Ok(envelopes.Select(e => e.ToDTO()));
}
/// <summary>
/// GET (Read) /envelope/{id}
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<EnvelopeDTO>> Get(int id)
{
var envelope = await _dbContext.Envelope.Include(e => e.Type).FirstOrDefaultAsync(s => s.Id == id);
if (envelope == null)
return NotFound();
return Ok(envelope.ToDTO());
}
/// <summary>
/// POST (Create) /envelope
/// </summary>
/// <param name="envelopeDto"></param>
/// <returns></returns>
[HttpPost("envelope")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<EnvelopeDTO>> Create([FromBody] EnvelopeDTO envelopeDto)
{
_logger.LogInformation("Hello from the Post() method!");
if (string.IsNullOrEmpty(envelopeDto.СontentX))
return BadRequest();
var #type = await _dbContext.EnvelopeType.FirstOrDefaultAsync(t => t.Id == envelopeDto.TypeId);
if (#type == null)
return NotFound();
var #user = await _dbContext.User.FirstOrDefaultAsync(u => u.UserName == envelopeDto.User);
if (#user == null)
return NotFound();
var existingEnvelope = await _dbContext.Envelope.FindAsync(envelopeDto.СontentX);
if (existingEnvelope != null)
return Conflict();
var envelopeToAdd = envelopeDto.ToModel(#type, #user);
_dbContext.Envelope.Add(envelopeToAdd);
await _dbContext.SaveChangesAsync();
var updatedEnvelopeDto = envelopeToAdd.ToDTO();
return CreatedAtAction(nameof(Get), new { Content = envelopeDto.СontentX }, updatedEnvelopeDto);
}
/// <summary>
/// DELETE /envelope/{id}
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<EnvelopeDTO>> Delete(int id)
{
var envelope = await _dbContext.Envelope.FindAsync(id);
if (envelope == null)
return NotFound();
_dbContext.Envelope.Remove(envelope);
await _dbContext.SaveChangesAsync();
return Ok(envelope.ToDTO());
}
}
public static class EnvelopeExtensions
{
public static Envelope ToModel(this EnvelopeDTO envelopeDto, EnvelopeType #type, User #user)
{
//if (#class.Id != studentDto.ClassId) throw new NotSupportedException();
return new Envelope
{
//DateTimeReceivied = DateTime.Now,
Type = #type,
СontentX = envelopeDto.СontentX,
User = #user
};
}
public static EnvelopeDTO ToDTO(this Envelope envelope)
{
return new EnvelopeDTO
{
//DateTimeReceivied = DateTime.Now,
TypeId = envelope.Type.Id,
СontentX = envelope.СontentX,
User = envelope.User.UserName
};
}
}
}
Here are the samples of POST and GET requests.
Change
[HttpPost("envelope")]
to
[HttpPost]
The way you have it coded the POST body would be posted to the POST /envelopes/envelope endpoint, which appears to be what you don't want.
Also your comments in the other methods are misleading. For example
// DELETE /envelope/{id}
should really be
// DELETE /envelopes/{id}.
The [controller] portion of [Route("[controller]")] will be substituted with the lowercase portion of the controller name without Controller (i.e. /envelopes (plural) forms the basis for your endpoints from EnvelopesController.
In your Postman screenshots you can see that you were trying to GET from plural /envelopes (good) but post to singular /envelope, which is not defined.

Cannot setup a Moq callback for MediatR

I'm writing a test that verifies if my controller calls my query with expected query arguments. This is my query class:
public class GetProducts : IRequest<IEnumerable<Product>>
{
public int CategoryId { get; set; }
}
This implements IRequest<T> MediatR interface.
This is the test case that does not work:
[Theory]
[InlineData(1)]
public async Task GetProductsAsync_GivenValidRequestParameters_ReturnsListGetProductsResponseAsync(int categoryId)
{
var expectedQuery = new GetProducts
{
CategoryId = categoryId
};
_mediatorMock
.Setup(s => s.Send(It.IsAny<GetProducts>(), It.IsAny<CancellationToken>()))
.Callback<GetProducts, CancellationToken>((query, ct) => query.Should().BeEquivalentTo(expectedQuery))
.ReturnsAsync(Enumerable.Empty<Product>());
var response = await _productsController.GetProductsAsync(categoryId);
response.Result.Should().BeOfType<OkObjectResult>();
_mediatorMock.Verify(s => s.Send(It.IsAny<GetProducts>(), It.IsAny<CancellationToken>()), Times.Once);
}
This is the controller I'm testing:
[ApiController]
[Route("categories/{categoryId:int}/products")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[TrackUsage]
public class ProductsController : ControllerBase
{
private readonly IMediator _mediator;
public ProductsController(IMediator mediator)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
}
[HttpGet]
public async Task<ActionResult<IEnumerable<GetProductsResponse>>> GetProductsAsync([FromRoute]int categoryId)
{
var query = new GetProducts
{
CategoryId = categoryId
};
var products = await _mediator.Send(query);
return Ok(products.ToResponse());
}
}
It complains because it cannot find an callback with <GetProducts, CancellationToken> as parameters even though it seems right.
I know I could use It.Is<...>(callback => true) to check each and every property, but there could be queries with multiple properties and I'd prefer to test that using FluentAssertion.
Generic Send definition
public interface IMediator
{
/// <summary>
/// Asynchronously send a request to a single handler
/// </summary>
/// <typeparam name="TResponse">Response type</typeparam>
/// <param name="request">Request object</param>
/// <param name="cancellationToken">Optional cancellation token</param>
/// <returns>A task that represents the send operation. The task result contains the handler response</returns>
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
//...
Source
The Callback needs to match the provided definition
//...
_mediatorMock
.Setup(s => s.Send<IEnumerable<Product>>(It.IsAny<IRequest<IEnumerable<Product>>>(), It.IsAny<CancellationToken>()))
.Callback<IRequest<IEnumerable<Product>>, CancellationToken>((query, ct) =>
((GetProducts)query).Should().BeEquivalentTo(expectedQuery)
)
.ReturnsAsync(Enumerable.Empty<Product>());
//...

Nunit Testing with Moq

I recently started learning Unit Test in MVC and used NUnit Framework for Test Cases. My problem is, i cannot understand for what should i write Test case. Imagine i have CRUD operation and i want to Test them, so what should be my Test case condition.
Here is my Interface class:
public interface IUserRepository
{
//Creating Single User Records into database using EF.
bool CreateUser(tbl_Users objUser);
//Updating Single User Records into database using EF.
void UpdateUser(tbl_Users objUser);
//Deleting Single User Records from database using EF.
bool DeleteUser(long IdUser);
}
Here is my Repository Class:
public class UserRepository : IUserRepository
{
DBContext objDBContext = new DBContext();
/// <summary>
/// Creating new User Record into Database
/// </summary>
/// <param name="objUser"></param>
public bool CreateUser(tbl_Users objUser)
{
bool blnResult = false;
objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim();
objUser.Photo = string.Empty;
objUser.Approved = false;
objUser.CreatedDate = DateTime.Now;
objUser.DeleteFlag = false;
objUser.UpdBy = 0;
objUser.UpdDate = DateTime.Now;
objDBContext.tbl_Users.Add(objUser);
blnResult = Convert.ToBoolean(objDBContext.SaveChanges());
return blnResult;
}
/// <summary>
/// Updating existing User Record into Database
/// </summary>
/// <param name="objUser"></param>
public void UpdateUser(tbl_Users objUser)
{
objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim();
objUser.Approved = true;
objUser.UpdBy = objUser.IdUser;
objUser.UpdDate = DateTime.Now;
objDBContext.Entry(objUser).State = EntityState.Modified;
objDBContext.SaveChanges();
}
/// <summary>
/// Deleting existing User Record from Database
/// </summary>
/// <param name="IdUser"></param>
public bool DeleteUser(long IdUser)
{
bool blnResult = false;
tbl_Users objUser = objDBContext.tbl_Users.Where(x => x.IdUser == IdUser).Single();
objUser.ConfirmPassword = objUser.Password;
objUser.UpdDate = DateTime.Now;
objUser.DeleteFlag = true;
blnResult = Convert.ToBoolean(objDBContext.SaveChanges());
return blnResult;
}
}
And Here is My Controller class
public class UserController : Controller
{
tbl_Users objUser = new tbl_Users();
UserRepository Repository = new UserRepository();
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Create(tbl_Users objUser)
{
if (ModelState.IsValid)
{
try
{
Repository.CreateUser(objUser);
return RedirectToAction("Update", "User");
}
catch
{
return View();
}
}
return View();
}
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Update(tbl_Users objUser)
{
Repository.UpdateUser(objUser);
return View();
}
public ActionResult Delete(long IdUser = 0)
{
bool blnResult = Repository.DeleteUser(IdUser);
if (blnResult)
{
return View("Delete");
}
else
{
return View();
}
}
}
Here are Test cases which i tried to Execute using Moq
[TestFixture]
public class UserControllerTest
{
UserController Controller;
[SetUp]
public void Initialise()
{
Controller = new UserController();
}
[Test]
public void DeleteTest()
{
var ObjUser = new Mock<IUserRepository>();
ObjUser.Setup(X => X.DeleteUser(It.IsAny<long>())).Returns(true);
var Result = ObjUser.Object.DeleteUser(1);
Assert.That(Result, Is.True);
}
[Test]
public void CreateTest()
{
tbl_Users User = new tbl_Users();
Mock<IUserRepository> MockIUserRepository = new Mock<IUserRepository>();
MockIUserRepository.Setup(X => X.CreateUser(It.IsAny<tbl_Users>())).Returns(true);
var Result = MockIUserRepository.Object.CreateUser(User);
Assert.That(Result, Is.True);
}
[TearDown]
public void DeInitialise()
{
Controller = null;
}
}
Can anyone tell me, how to Write test cases for above Controller Action Method with brief description about test cases using Moq.
you have a couple of problems. the first is that you have not tested your controller, you have tested your mock. The second is that your controller creates it's own user repository. this means that you can't provide a mock user repository in order to test, even if you were testing it.
The solution to the first on is to test the controller, by calling its methods and asserting the results, however you'll have solve the second one before you can do that in your tests.
To solve the second one you'll need to apply the dependency inversion principal and pass your IUserRepository implementation into your controller (via the constructor ideally).
you could change your controller to have a constructor like this:
public class UserController : Controller
{
tbl_Users objUser = new tbl_Users();
IUserRepository Repository;
public UserController(IUserRepository userRepository)
{
Repository = userRepository;
}
...etc
}
then you can change your tests to be more like this:
[TestFixture]
public class UserControllerTest
{
[Test]
public void DeleteTest()
{
var ObjUser = new Mock<IUserRepository>();
ObjUser.Setup(X => X.DeleteUser(It.IsAny<long>())).Returns(true);
var Result = new UserController(ObjUser.Object).Delete(1);
Assert.That(Result, //is expected view result with expected model);
Assert.That(ObjUser.Verify(), Is.True);
}
[Test]
public void CreateTest()
{
tbl_Users User = new tbl_Users();
Mock<IUserRepository> MockIUserRepository = new Mock<IUserRepository>();
MockIUserRepository.Setup(X => X.CreateUser(It.IsAny<tbl_Users>())).Returns(true);
var Result = var Result = new UserController(ObjUser.Object).Create(User);;
Assert.That(Result, //is a view result with expected view model);
Assert.That(ObjUser.Verify(), Is.True);
}
}
now you are testing the actual controller and checking it returns the right view and that it interacts with the mock repository in the expected way

DatabaseContext seed never completing

This is a long shot because there is so much custom code, but hopefully it is something simple.
I have this DatabaseInitializer class:
/// <summary>
/// Our Database initialsizer, which inherits CreateDatabaseIfNotExists. We could use DropCreateDatabaseWhenModelChanges or DropCreateDatabaseAlways as well.
/// </summary>
public class DatabaseInitializer : CreateDatabaseIfNotExists<DatabaseContext>
{
/// <summary>
/// Method to insert our data upon initialization
/// </summary>
/// <param name="context">Our DbConext</param>
protected override void Seed(DatabaseContext context)
{
// Create our User
CreateUser();
// Seed
base.Seed(context);
}
/// <summary>
/// Private method which creates the user
/// </summary>
private void CreateUser()
{
// Create our unit of work
var unitOfWork = new UnitOfWork<DatabaseContext>();
// Create our user service
var service = new UserService<User>(unitOfWork, true, true);
// Get our current date
var now = DateTime.UtcNow;
// Map the user model out
var user = new User()
{
UserName = "j***#*****.co.uk",
Email = "j***#*****.co.uk",
DateCreated = now,
DateModified = now,
LastLoginDate = now
};
// Run our task
var task = Task.Run(async () => {
// Create our user
await service.CreateAsync(user, "********");
// Save the changes to our DbSet
await unitOfWork.SaveChangesAsync();
});
// Wait for the async task to complete
task.Wait();
}
}
Now the problem is, that it never seems to complete.
I am thinking it might be because of the async tasks, but I can't be sure.
I know there is no a lot to go on, but like I said I hope that it is something simple because showing you how my UnitOfWork class works along with my UserService would be a lot of code. Rest assured that they have worked fine in other projects.
UnitOfWork
This is what makes up my UnitOfWork class:
public class UnitOfWork<TContext> : IUnitOfWork where TContext : DbContext, new()
{
private readonly DbContext context;
private Dictionary<Type, object> repositories;
public DbContext Context { get { return this.context; } }
public UnitOfWork()
{
this.context = new TContext();
repositories = new Dictionary<Type, object>();
}
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
if (repositories.Keys.Contains(typeof(TEntity)))
return repositories[typeof(TEntity)] as IRepository<TEntity>;
var repository = new Repository<TEntity>(context);
repositories.Add(typeof(TEntity), repository);
return repository;
}
public async Task SaveChangesAsync()
{
try
{
await this.context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
ex.Entries.First().Reload();
}
}
public void Dispose()
{
this.context.Dispose();
}
}
As stated in the comments, you need to use the same datacontext.
Add a constructor to your UnitOfWork<TContext> class that can take an existing context as parameter:
public UnitOfWork(TContext context)
{
this.context = context;
repositories = new Dictionary<Type, object>();
}
Then in you CreateUser method:
private void Createuser(DatabaseContext context)
{
var unitOfWork = new UnitOfWork<DatabaseContext>(context);
...
}

Automocking Web Api 2 controller

I am trying to auto mock ApiController class in my test cases. It worked perfectly when I was using WebApi1. I started to use WebApi2 on the new project and I am getting this exception thrown after I try to run my new tests:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.Cryptography.CryptographicException: pCertContext is an invalid handle.
at System.Security.Cryptography.CAPI.CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, UInt32 dwPropId, UInt32 dwFlags, SafeLocalAllocHandle safeLocalAllocHandle)
at System.Security.Cryptography.X509Certificates.X509Certificate2.set_Archived(Boolean value)
My test code:
[Theory, AutoMoqData]
public void approparte_status_code_is_returned(
string privateKey,
UsersController sut)
{
var response = sut.GetUser(privateKey);
var result = response;
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}
Test case does work if I create sut manually:
[Theory, AutoMoqData]
public void approparte_status_code_is_returned(
string privateKey,
[Frozen]Mock<IUserModel> stubModel)
{
var sut = new UsersController(stubModel.Object);
var response = sut.GetUser(privateKey);
var result = response;
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}
It's seems that something goes wrong when trying to mock the ControllerContext.RequestContext.ClientCertificate I've tried to create a fixture without it (using AutoFixture .Without() method) but then even the old tests started to fail.
My AutoMoqDataAttribute:
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute()
: base(new Fixture()
.Customize(new WebApiCustomization()))
{
}
}
WebApi customization:
public class WebApiCustomization : CompositeCustomization
{
public WebApiCustomization()
: base(
new HttpRequestMessageCustomization(),
new AutoMoqCustomization())
{
}
}
HttpRequestMessage customization:
public class HttpRequestMessageCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<HttpRequestMessage>(c => c
.Without(x => x.Content)
.Do(x =>
{
x.Properties[HttpPropertyKeys.HttpConfigurationKey] = new HttpConfiguration();
})
);
}
}
UsersController:
/// <summary>
/// Handles user's account.
/// </summary>
[RoutePrefix("api/v1/users/{privateKey:length(64)}")]
public class UsersController : ApiController
{
private readonly IUserModel _model;
public UsersController(IUserModel model)
{
_model = model;
}
/// <summary>
/// Returns a user.
/// </summary>
/// <param name="privateKey">The private key of the user.</param>
/// <returns>
/// 200 (OK) with user data is returned when user is found.
/// 404 (Not found) is returned when user is not found.
/// </returns>
[HttpGet]
[Route("")]
public HttpResponseMessage GetUser(string privateKey)
{
UserProjection projection;
try
{
projection = new UserProjection(_model.Get(privateKey));
}
catch (UserNotFoundException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, projection);
}
}
Note:
The original answer requires the same customization to be copied for each new ApiController.
Generalized approach
An alternative way is to automatically fill the Request property on all ApiControllers (thus saving you from cut, copy, and paste):
internal class ApiControllerCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new FilteringSpecimenBuilder(
new Postprocessor(
new MethodInvoker(
new ModestConstructorQuery()),
new ApiControllerFiller()),
new ApiControllerSpecification()));
}
private class ApiControllerFiller : ISpecimenCommand
{
public void Execute(object specimen, ISpecimenContext context)
{
if (specimen == null)
throw new ArgumentNullException("specimen");
if (context == null)
throw new ArgumentNullException("context");
var target = specimen as ApiController;
if (target == null)
throw new ArgumentException(
"The specimen must be an instance of ApiController.",
"specimen");
target.Request =
(HttpRequestMessage)context.Resolve(
typeof(HttpRequestMessage));
}
}
private class ApiControllerSpecification : IRequestSpecification
{
public bool IsSatisfiedBy(object request)
{
var requestType = request as Type;
if (requestType == null)
return false;
return typeof(ApiController).IsAssignableFrom(requestType);
}
}
}
The value of type HttpRequestMessage, for the Request property, is built using the following customization:
internal class HttpRequestMessageCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<HttpRequestMessage>(c => c
.Without(x => x.Content)
.Do(x => x.Properties[HttpPropertyKeys.HttpConfigurationKey] =
new HttpConfiguration()));
}
}
Packing everything into a composite Customization
Create a Customization composite as below - note that the order of AutoFixture Customizations matter:
internal class ApiControllerConventions : CompositeCustomization
{
internal ApiControllerConventions()
: base(
new HttpRequestMessageCustomization(),
new ApiControllerCustomization(),
new AutoMoqCustomization())
{
}
}
Hope that helps.
Note:
Assuming that the UserController class takes an IUserModel through its constructor.
As it looks like, the default constructor of ApiController performs some work (probably more than simple assignments).
If the UserController class takes an IUserModel through its constructor, you can pick that constructor (the greediest) instead.
Update:
Replace the HttpRequestMessageCustomization customization with:
internal class ApiControllerCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<HttpRequestMessage>(c => c
.Without(x => x.Content)
.Do(x => x.Properties[HttpPropertyKeys.HttpConfigurationKey] =
new HttpConfiguration()));
fixture.Customize<UsersController>(c => c
.OmitAutoProperties()
.With(x => x.Request, fixture.Create<HttpRequestMessage>()));
}
}
And the original test will execute fine.
Based on Nikos' answer:
This is a more generic way of using this customization where the controller type can be supplied and the Customization can be used for any controller
internal class WebApiCustomization<TControllerType> : ICustomization
where TControllerType : ApiController
{
public void Customize(IFixture fixture)
{
fixture.Customize<HttpRequestMessage>(c => c
.Without(x => x.Content)
.Do(x => x.Properties[HttpPropertyKeys.HttpConfigurationKey] =
new HttpConfiguration()));
fixture.Customize<TControllerType>(c => c
.OmitAutoProperties()
.With(x => x.Request, fixture.Create<HttpRequestMessage>()));
}
}
Then use as follows:
var fixture = new Fixture().Customize(
new WebApiCustomization<UsersController>());
var sut = fixture.Create<UsersController>();

Categories