FakeXRMEasy: Using AddFakeMessageExecutor to override update request behaviour - c#

I am trying to create a test for a situation where an Update request throws an exception. Is this possible to do using FakeXRMEasy? I have tried using AddFakeMessageExecutor, but at the moment it is not working:
My fake message executor class:
public class UpdateExecutor : IFakeMessageExecutor
{
public bool CanExecute(OrganizationRequest request)
{
return request is UpdateRequest;
}
public OrganizationResponse Execute(
OrganizationRequest request,
XrmFakedContext ctx)
{
throw new Exception();
}
public Type GetResponsibleRequestType()
{
return typeof(UpdateRequest);
}
}
Use in test:
fakeContext.Initialize(new Entity[] { agreement });
fakeContext.AddFakeMessageExecutor<UpdateRequest>(new UpdateExecutor());
fakeContext.ExecuteCodeActivity<AgreementConfirmationWorkflow>(fakeContext.GetDefaultWorkflowContext());
And in the workflow the update request is called:
var workflowContext = executionContext.GetExtension<IWorkflowContext>();
var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
/// some code to retrieve entity and change attributes ///
service.Update(entity);
I wanted this to throw an exception, but at the moment the update request is completing successfully. How can I make this work?

IFakeMessageExecutor only works when you call IOrganizationService.Execute method. So, if you change your service.Update(entity); line of code for service.Execute(new UpdateRequest { Target = entity}); it should work.
Here is a full working example for reference:
CodeActivity
public class RandomCodeActivity : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
var workflowContext = context.GetExtension<IWorkflowContext>();
var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
var service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
var accountToUpdate = new Account() { Id = new Guid("e7efd527-fd12-48d2-9eae-875a61316639"), Name = "A new faked name!" };
service.Execute(new UpdateRequest { Target = accountToUpdate });
}
}
FakeMessageExecutor instance
public class FakeUpdateRequestExecutor : IFakeMessageExecutor
{
public bool CanExecute(OrganizationRequest request)
{
return request is UpdateRequest;
}
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
{
throw new InvalidPluginExecutionException("Throwing an Invalid Plugin Execution Exception for test purposes");
}
public Type GetResponsibleRequestType()
{
return typeof(UpdateRequest);
}
}
Test (uses xUnit test lib)
[Fact]
public void UpdateAccount_WithUpdateExecutorThrowingAnException_ExceptionThrown()
{
//Assign
var context = new XrmFakedContext
{
ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account))
};
var account = new Account() { Id = new Guid("e7efd527-fd12-48d2-9eae-875a61316639"), Name = "Faked Name" };
context.Initialize(new List<Entity>() { account });
context.AddFakeMessageExecutor<UpdateRequest>(new FakeUpdateRequestExecutor());
var service = context.GetOrganizationService();
//Act
//Assert
Assert.Throws<InvalidPluginExecutionException>(() => context.ExecuteCodeActivity<RandomCodeActivity>(account));
}

Related

C# Repositories and Http responses not returning expected data using same dbContext XUnit

I've been trying to implement integration tests with a database per test strategy, but I haven't been able to make it work as needed.
This is the factory class that uses WebApplicationFactory:
public class TestFactory<TProgram, TDbContext> : WebApplicationFactory<TProgram>
where TProgram : class where TDbContext : DbContext
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.RemoveDbContext<TDbContext>();
services.AddDbContext<TDbContext>(options =>
{
options.UseInMemoryDatabase(Guid.NewGuid().ToString());
});
services.EnsureDbCreated<TDbContext>();
});
}
}
This is the TestClass:
public class RolesControllerTest : IDisposable
{
private readonly TestFactory<Program, ADbContext> _factory;
private IServiceScope _scope;
private ADbContext_dbContext;
private readonly HttpClient _client;
private IRoleRepository _rolesRepository;
public RolesControllerTest()
{
_factory = new TestFactory<Program, ADbContext>();
_client = _factory.CreateClient();
_scope = _factory.Services.CreateScope();
var scopedServices = _scope.ServiceProvider;
_dbContext = scopedServices.GetRequiredService<ADbContext>();
_dbContext.Database.EnsureCreated();
}
public void Dispose()
{
_factory.Dispose();
_scope.Dispose();
}
// Tests ...
}
This is the test:
[Fact(DisplayName = "GetAsync returns a list of role models")]
public async Task GetAsync_ReturnsTaskOfRoleModelList()
{
var roleModelInDb = new RoleModel
{
Id = Guid.NewGuid(),
Name = "Role A",
Description = "Role A Description"
};
_rolesRepository = new RoleRepository(_dbContext, TestMapperHelper.GenerateTestMapper());
var roleModel = await _rolesRepository.CreateAsync(roleModelInDb);
var responseData = await _client.GetFromJsonAsync<List<RoleModel>>("/api/roles");
responseData.ShouldNotBeNull();
responseData.ShouldBeOfType<List<RoleModel>>();
responseData.Count.ShouldBe(1);
responseData[0].Id.ShouldBe(roleModel.Id);
responseData[0].Name.ShouldBe(roleModelInDb.Name);
}
The repository returns the expected data: the new roleModel that's been added to the db.
The responseData is a list as expected, but it's empty, so the test fails.
If I try to use a client instead of the repository to create the initial roleModel:
var createdResponse = await _client.PostAsJsonAsync("/api/roles", roleModelInDb);
var createdByClient = await TestResponseHelper.GetResponseContent<RoleModel>(createdResponse);
The createdResponse is a 200 OK Http response, and the role model createdByClient is a valid RoleModel, but the test fails, the list is still empty.
If I use a roleRepository to find the previously created roleModel by Id, the result is null.
If I'm using the same database context for the web factory and repositories, why is this happening?

Mock a controller for unit test without any service reference C#

I am having a simple controller which needs to be unit tested not integration tested. I just need a way to mock so that I can verify if receive method is called. We already have test against Receive(), so no need to verify what is going inside that method.
My code looks like
public class MessageController : Controller
{
private readonly ConnectionDetail connectionDetail;
private readonly QueueDetail queueDetail;
public MessageController(IOptions<ConnectionDetail> connectionDetail, IOptions<QueueDetail> queueDetail)
{
this.connectionDetail = connectionDetail.Value;
this.queueDetail = queueDetail.Value;
}
[HttpGet()]
public IActionResult Get()
{
try
{
var channel = CreateConnectionAndChannel(queueDetail);
var message = channel.Receive();
var hbaseKey = new HbaseKey { Key = new Guid(message) };
return Ok(hbaseKey);
}
catch
{
return StatusCode(500, "Exception occured while processing. Try again.");
}
}
private IChannel CreateConnectionAndChannel(QueueDetail queueDetail)
{
var factory = new Factory();
var adapter = factory.Connect(MessagingType.MQ, connectionDetail);
return adapter.BindQueue(queueDetail);
}
}
Refactor the CreateConnectionAndChannel function out into its own service
public interface IChannelProvider {
IChannel CreateConnectionAndChannel();
}
and have controller explicitly depend on that service
public class MessageController : Controller {
private readonly IChannelProvider channelProvider;
public MessageController(IChannelProvider channelProvider) {
this.channelProvider = channelProvider;
}
[HttpGet()]
public IActionResult Get() {
try {
var channel = channelProvider.CreateConnectionAndChannel();
var message = channel.Receive();
var hbaseKey = new HbaseKey { Key = new Guid(message) };
return Ok(hbaseKey);
} catch {
return StatusCode(500, "Exception occured while processing. Try again.");
}
}
}
So now only the IChannelProvider needs to be mocked to test the controller in isolation.
I just need a way to mock so that I can verify if receive method is called.
public void Verify_Received_Called() {
//Arrange
var channel = new Mock<IChannel>();
channel
.Setup(_ => _.Receive())
.Returns("My mock value here");
var mockProvider = new Mock<IChannelProvider>();
mockProvider.Setup(_ => _.CreateConnectionAndChannel())
.Returns(channel.Object);
var controller = new MessageController(mockProvider.Object);
//Act
var result = controller.Get();
//Assert
channel.Verify(_ => _.Receive(), Times.AtLeastOnce);
}
The provider implementation could look like...
public class ChannelProvider : IChannelProvider {
private readonly ConnectionDetail connectionDetail;
private readonly QueueDetail queueDetail;
public ChannelProvider(IOptions<ConnectionDetail> connectionDetail, IOptions<QueueDetail> queueDetail) {
this.connectionDetail = connectionDetail.Value;
this.queueDetail = queueDetail.Value;
}
public IChannel CreateConnectionAndChannel() {
var factory = new Factory();
var adapter = factory.Connect(MessagingType.MQ, connectionDetail);
return adapter.BindQueue(queueDetail);
}
}
In order to do this, you need to move your CreateConnectionAndChannel method to a separate dependency, for instance, ChannelFactory which implements IChannelFactory interface.
public interface IChannelFactory {
IChannel CreateConnectionAndChannel(QueueDetail queueDetail);
}
public class ChannelFactory : IChannelFactory {
public IChannel CreateConnectionAndChannel(QueueDetail queueDetail)
{
var factory = new Factory();
var adapter = factory.Connect(MessagingType.MQ, connectionDetail);
return adapter.BindQueue(queueDetail);
}
}
public class MessageController : Controller
{
private readonly ConnectionDetail connectionDetail;
private readonly QueueDetail queueDetail;
private readonly IChannelFactory channelFactory;
public MessageController(IOptions<ConnectionDetail> connectionDetail, IOptions<QueueDetail> queueDetail, IChannelFactory channelFactory)
{
this.connectionDetail = connectionDetail.Value;
this.queueDetail = queueDetail.Value;
this.channelFactory = channelFactory;
}
[HttpGet()]
public IActionResult Get()
{
try
{
var channel = channelFactory.CreateConnectionAndChannel(queueDetail);
var message = channel.Receive();
var hbaseKey = new HbaseKey { Key = new Guid(message) };
return Ok(hbaseKey);
}
catch
{
return StatusCode(500, "Exception occured while processing. Try again.");
}
}
}
After that you can mock your controller in test (using Moq for example):
[TestFixture]
public class TestMessageController
{
[Test]
public void TestGet()
{
var channelMock = new Mock<IChannel>(MockBehavior.Strict);
channelMock
.Setup(c => c.Receive())
.Returns(null);
var channelFactoryMock = new Mock<IChannelFactory>(MockBehavior.Strict);
channelFactory
.Setup(cf => cf.CreateConnectionAndChannel(It.IsAny<IOptions<QueueDetail>>()))
.Returns();
var controller = new MessageController(null, null, channelFactoryMock.Object);
controller.Get();
}
}

Mock Async method on Service using Moq

I am working in a .Net Core API. I wish to unit test the GetArtists method on the ArtistsController.
CODE
Here is my controller code:
[Route("artists")]
public class ArtistsController : Controller
{
private readonly IPermissionsService _permissionsService;
private readonly IArtistsService _artistsService;
private readonly ILogger<ArtistsController> _logger;
public ArtistsController(IPermissionsService permissionsService, IArtistsService artistsService, ILogger<ArtistsController> logger)
{
_permissionsService = permissionsService ?? throw new ArgumentNullException(nameof(permissionsService));
_artistsService = artistsService ?? throw new ArgumentNullException(nameof(artistsService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
[HttpGet]
public async Task<IActionResult> GetArtists()
{
var permissions = await _permissionsService.GetPermissionsAsync(HttpContext);
var artists = _artistsService.GetAllArtists(permissions.UserId, permissions.IsAdministrator);
return Ok( new { artists });
}
}
And here is the test method I am writing:
[TestClass]
public class ArtistsControllerTests
{
private readonly Mock<IPermissionsService> _mockPermissionsService = new Mock<IPermissionsService>();
private readonly Mock<IArtistsService> _mockArtistsService = new Mock<IArtistsService>();
private readonly Mock<ILogger<ArtistsController>> _mockLogger = new Mock<ILogger<ArtistsController>>();
public void Setup()
{
_mockArtistsService.Reset();
_mockPermissionsService
.Setup(service => service.GetPermissionsAsync(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(new Permissions { UserId = "112233", IsAdministrator = false }));
_mockArtistsService.Setup(service => service.GetAllArtists(It.IsAny<string>(), false)).Returns(new ArtistCardDtoCollection());
}
[TestMethod]
public async Task GetArtists_ReturnsOKStatusCode()
{
// arrange
var artistsController = new ArtistsController(_mockPermissionsService.Object, _mockArtistsService.Object, _mockLogger.Object);
// act
var getArtistsResult = await artistsController.GetArtists();
var okResult = getArtistsResult as OkObjectResult;
// assert
Assert.IsInstanceOfType(okResult, typeof(OkObjectResult));
}
}
Here is the IPermissionsService and the Permissions class.
public interface IPermissionsService
{
Task<Permissions> GetPermissionsAsync(HttpContext httpContext);
}
public class Permissions
{
public string UserId { get; set; }
public bool IsAdministrator { get; set; }
}
When I run that, I get the following error:
Project.ArtistsControllerTests.GetArtists_ReturnsOKStatusCode threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
When debugging, I found out that var permissions = await _permissionsService.GetPermissionsAsync(HttpContext); returns null.
I must have an issue with the way I am mocking that:
_mockPermissionsService
.Setup(service => service.GetPermissionsAsync(It.IsAny<HttpContext>()))
Why wouldn't the above work?
ArtistsControllerTests.Setup() is not being invoked so the mocks are not being setup before the test is exercised.
Therefore when the test is exercised they will return null.
Your setup code is correct, it just is not getting called.
either change that Setup method to a constructor
public ArtistsControllerTests() {
_mockArtistsService.Reset();
_mockPermissionsService
.Setup(service => service.GetPermissionsAsync(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(new Permissions { UserId = "112233", IsAdministrator = false }));
_mockArtistsService.Setup(service => service.GetAllArtists(It.IsAny<string>(), false)).Returns(new ArtistCardDtoCollection());
}
or adorn the method with [TestInitilize] attribute
[TestInitialize]
public void Setup() {
_mockArtistsService.Reset();
_mockPermissionsService
.Setup(service => service.GetPermissionsAsync(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(new Permissions { UserId = "112233", IsAdministrator = false }));
_mockArtistsService.Setup(service => service.GetAllArtists(It.IsAny<string>(), false)).Returns(new ArtistCardDtoCollection());
}
or just move the arrange into the test itself
[TestMethod]
public async Task GetArtists_ReturnsOKStatusCode() {
// arrange
_mockArtistsService.Reset();
_mockPermissionsService
.Setup(service => service.GetPermissionsAsync(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(new Permissions { UserId = "112233", IsAdministrator = false }));
_mockArtistsService.Setup(service => service.GetAllArtists(It.IsAny<string>(), false)).Returns(new ArtistCardDtoCollection());
var artistsController = new ArtistsController(_mockPermissionsService.Object, _mockArtistsService.Object, _mockLogger.Object);
// act
var getArtistsResult = await artistsController.GetArtists();
var okResult = getArtistsResult as OkObjectResult;
// assert
Assert.IsInstanceOfType(okResult, typeof(OkObjectResult));
}

Mock HttpActionContext for Unit Test

I have a custom authorization attribute seen below and I am trying to write a unit test to test its functionality.
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization != null)
{
// get the Authorization header value from the request and base64 decode it
string userInfo = Encoding.Default.GetString(Convert.FromBase64String(actionContext.Request.Headers.Authorization.Parameter));
// custom authentication logic
if (string.Equals(userInfo, string.Format("{0}:{1}", "user", "pass")))
{
IsAuthorized(actionContext);
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized)
{
ReasonPhrase = "Unauthorized"
};
}
My problem is that when I try to test this I get "System.NullReferenceException: Object reference not set to an instance of an object." I have tried to set the actionContext's request.headers.authorization value but it has no setter. When I try mocking the HttpActionContext it says it cannot convert from a mock HttpActionContext to a real one. Below is my test code
public class HttpBasicAuthorizeAttributeTest
{
private HttpBasicAuthorizeAttribute ClassUnderTest { get; set; }
private HttpActionContext actionContext { get; set; }
[TestMethod]
public void HttpBasicAuthorizeAttribute_OnAuthorize_WithAuthorizedUser_ReturnsAuthorization()
{
var context = new Mock<HttpActionContext>();
context.Setup(x => x.Request.Headers.Authorization.Parameter).Returns("bzUwkDal=");
ClassUnderTest.OnAuthorization(context);
}
[TestInitialize]
public void Initialize()
{
ClassUnderTest = new HttpBasicAuthorizeAttribute();
actionContext = new HttpActionContext();
}
}
*Left out the assert until I can even get the HttpActionContext to work
You can use the actual objects and provide it to the mock in order to exercise the method under test as Moq in unable to mock the non-virtual members.
[TestMethod]
public void HttpBasicAuthorizeAttribute_OnAuthorize_WithAuthorizedUser_ReturnsAuthorization() {
//Arrange
var context = new HttpActionContext();
var headerValue = new AuthenticationHeaderValue("Basic", "bzUwkDal=");
var request = new HttpRequestMessage();
request.Headers.Authorization = headerValue;
var controllerContext = new HttpControllerContext();
controllerContext.Request = request;
context.ControllerContext = controllerContext;
//Act
ClassUnderTest.OnAuthorization(context);
//Assert
//...
}

Unit Test HTTPRequest Headers with ServiceStack

I have this Service:
public class PlayerService : Service
{
public IPlayerAppService PlayerAppService { get; set; }
public PlayerService (IPlayerAppService service)
{
if (service == null)
throw new ArgumentException ("Service null");
PlayerAppService = service;
}
public object Post (PlayerDTO request)
{
var newPlayer = new PlayerResponse ()
{
Player = PlayerAppService.SendPlayerLocation(request.Position.Latitude, request.Position.Longitude)
};
return new HttpResult (newPlayer)
{
StatusCode = System.Net.HttpStatusCode.Created,
Headers =
{
{ HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newPlayer.Player.Id.ToString()) }
}
};
}
}
I've manually verified that the Location and the Response looks correct from my deployments of this service. I would like to figure out how to unit test this though. I wrote a test like this:
[TestFixture]
public class PlayerServiceTests
{
AppHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp ()
{
appHost = new AppHost ();
appHost.Init ();
appHost.Start ("http://localhost:1337/");
}
[TestFixtureTearDown]
public void TestFixtureTearDown ()
{
appHost.Dispose ();
appHost = null;
}
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
// Arrange
PlayerService service = new PlayerService (appHost.TryResolve<IPlayerAppService>());
// Act
HttpResult response = (HttpResult)service.Post (It.IsAny<PlayerDTO>());
// Assert
Assert.NotNull (response);
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
Assert.AreEqual(response.Response.ToDto<PlayerResponse>().Player.Id.ToString(), response.Headers.Where(x=> x.Key == HttpHeaders.Location).SingleOrDefault().Value);
}
}
The base.Request when my unit test runs though. Do you have any suggestions on how I can populate this from my unit test?
You're using an self-hosting HttpListener as you would for an integration test, but you're not doing in integration test.
An integration test would look like:
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
var client = new JsonServiceClient("http://localhost:1337/") {
ResponseFilter = httpRes => {
//Test response headers...
};
}
PlayerResponse response = client.Post(new Player { ... });
}
Otherwise if you want to do an unit test you don't need an AppHost and can just test the PlayerService class just like any other C# class, injecting all the dependencies and the mock Request context it needs.
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
var mockCtx = new Mock<IRequestContext>();
mockCtx.SetupGet (f => f.AbsoluteUri).Returns("localhost:1337/player");
PlayerService service = new PlayerService {
MyOtherDependencies = new Mock<IMyOtherDeps>().Object,
RequestContext = mockCtx.Object,
};
HttpResult response = (HttpResult)service.Post(new Player { ... });
//Assert stuff..
}

Categories