Skip to content

Commit

Permalink
Fix Unit test on filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Feb 24, 2022
1 parent 62f446e commit 68c80cc
Showing 1 changed file with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace AzureIoTHub.Portal.Server.Tests.Filters
public class LoRaFeatureActiveFilterAttributeTest
{
private MockRepository mockRepository;
// private Mock<ActionExecutingContext> mockActionExecutingContext;
private Mock<ConfigHandler> mockConfigHandler;

[SetUp]
Expand All @@ -39,7 +38,7 @@ private LoRaFeatureActiveFilterAttribute CreateAttributeFilter()
}

[Test]
public void OnActionExecuting_With_LoRa_feature_disable_Should_return_400()
public void When_LoRa_Is_Disabled_Should_Return_Http_400()
{
// Arrange
this.mockConfigHandler
Expand All @@ -65,19 +64,51 @@ public void OnActionExecuting_With_LoRa_feature_disable_Should_return_400()
new Dictionary<string, object>(),
Mock.Of<Controller>());

//var mockActionExecutingContext = new Mock<ActionExecutingContext>();
//mockActionExecutingContext
// .Setup(x => x.HttpContext.RequestServices.GetService(typeof(ConfigHandler)))
// .Returns(this.mockConfigHandler.Object);
var actionFilter = this.CreateAttributeFilter();

// Act
actionFilter.OnActionExecuting(actionExecutingContext);

// Assert
Assert.IsAssignableFrom<BadRequestObjectResult>(actionExecutingContext.Result);
this.mockRepository.VerifyAll();
}

[Test]
public void When_LoRa_Is_Enabled_Should_Return_Null()
{
// Arrange
this.mockConfigHandler
.SetupGet(c => c.IsLoRaEnabled)
.Returns(true);

var serviceProviderMock = new Mock<IServiceProvider>();
serviceProviderMock.Setup(provider => provider.GetService(typeof(ConfigHandler)))
.Returns(this.mockConfigHandler.Object);

var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupGet(context => context.RequestServices)
.Returns(serviceProviderMock.Object);

var actionContext = new ActionContext(
httpContextMock.Object,
Mock.Of<RouteData>(),
Mock.Of<ActionDescriptor>());

var actionExecutingContext = new ActionExecutingContext(
actionContext,
new List<IFilterMetadata>(),
new Dictionary<string, object>(),
Mock.Of<Controller>());

var actionFilter = this.CreateAttributeFilter();

// Act
actionFilter.OnActionExecuting(actionExecutingContext);

// Assert
var contentResult = actionExecutingContext.Result as ContentResult;
Assert.AreEqual((int)System.Net.HttpStatusCode.BadRequest, contentResult.StatusCode);
Assert.IsNull(actionExecutingContext.Result);
this.mockRepository.VerifyAll();
}
}
}

0 comments on commit 68c80cc

Please sign in to comment.