-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3381c36
commit 459d1f0
Showing
24 changed files
with
707 additions
and
17 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/AzureIoTHub.Portal.Server.Tests.Unit/BackendUnitTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) CGI France. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace AzureIoTHub.Portal.Server.Tests.Unit | ||
{ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Moq; | ||
using NUnit.Framework; | ||
using RichardSzalay.MockHttp; | ||
|
||
public abstract class BackendUnitTest : IDisposable | ||
{ | ||
protected virtual ServiceCollection ServiceCollection { get; set; } | ||
|
||
protected virtual ServiceProvider Services { get; set; } | ||
|
||
protected virtual MockRepository MockRepository { get; set; } | ||
|
||
protected virtual MockHttpMessageHandler MockHttpClient { get; set; } | ||
|
||
protected virtual AutoFixture.Fixture Fixture { get; } = new(); | ||
|
||
[SetUp] | ||
public virtual void Setup() | ||
{ | ||
ServiceCollection = new ServiceCollection(); | ||
_ = ServiceCollection.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>)); | ||
|
||
// Configure Mockups | ||
MockRepository = new MockRepository(MockBehavior.Strict); | ||
|
||
// Add Mock Http Client | ||
MockHttpClient = new MockHttpMessageHandler(); | ||
var httpClient = MockHttpClient.ToHttpClient(); | ||
httpClient.BaseAddress = new Uri("http://fake.local"); | ||
_ = ServiceCollection.AddSingleton(httpClient); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
Services?.Dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/AzureIoTHub.Portal.Server.Tests.Unit/Controllers/v1.0/IdeasControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) CGI France. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace AzureIoTHub.Portal.Server.Tests.Unit.Controllers.v1._0 | ||
{ | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using AzureIoTHub.Portal.Server.Controllers.v1._0; | ||
using AzureIoTHub.Portal.Shared.Models.v1._0; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using NUnit.Framework; | ||
using Server.Services; | ||
|
||
[TestFixture] | ||
public class IdeasControllerTests : BackendUnitTest | ||
{ | ||
private Mock<IIdeaService> mockIdeaService; | ||
|
||
private IdeasController ideasController; | ||
|
||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
|
||
this.mockIdeaService = MockRepository.Create<IIdeaService>(); | ||
|
||
_ = ServiceCollection.AddSingleton(this.mockIdeaService.Object); | ||
|
||
Services = ServiceCollection.BuildServiceProvider(); | ||
|
||
this.ideasController = new IdeasController(Services.GetRequiredService<IIdeaService>()); | ||
} | ||
|
||
[Test] | ||
public async Task SubmitIdeaShouldSubmitIdea() | ||
{ | ||
// Arrange | ||
var ideaRequest = Fixture.Create<IdeaRequest>(); | ||
var expectedIdeaResponse = Fixture.Create<IdeaResponse>(); | ||
|
||
_ = this.mockIdeaService.Setup(service => service.SubmitIdea(ideaRequest)) | ||
.ReturnsAsync(expectedIdeaResponse); | ||
|
||
// Act | ||
var result = await this.ideasController.SubmitIdea(ideaRequest); | ||
|
||
// Assert | ||
_ = result.Should().BeEquivalentTo(expectedIdeaResponse); | ||
MockRepository.VerifyAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/AzureIoTHub.Portal.Server.Tests.Unit/Services/IdeaClientServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) CGI France. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace AzureIoTHub.Portal.Server.Tests.Unit.Services | ||
{ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using Client.Services; | ||
using FluentAssertions; | ||
using Helpers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using RichardSzalay.MockHttp; | ||
using Shared.Models.v1._0; | ||
|
||
[TestFixture] | ||
public class IdeaClientServiceTests : BlazorUnitTest | ||
{ | ||
private IIdeaClientService ideaClientService; | ||
|
||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
|
||
_ = Services.AddSingleton<IIdeaClientService, IdeaClientService>(); | ||
|
||
this.ideaClientService = Services.GetRequiredService<IIdeaClientService>(); | ||
} | ||
|
||
[Test] | ||
public async Task SubmitIdeaShouldSubmitIdea() | ||
{ | ||
// Arrange | ||
var ideaRequest = Fixture.Create<IdeaRequest>(); | ||
var expectedIdeaResponse = Fixture.Create<IdeaResponse>(); | ||
|
||
_ = MockHttpClient.When(HttpMethod.Post, "/api/ideas") | ||
.With(m => | ||
{ | ||
_ = m.Content.Should().BeAssignableTo<ObjectContent<IdeaRequest>>(); | ||
var body = (ObjectContent<IdeaRequest>) m.Content; | ||
_ = body?.Value.Should().BeEquivalentTo(ideaRequest); | ||
return true; | ||
}) | ||
.RespondJson(expectedIdeaResponse); | ||
|
||
// Act | ||
var result = await this.ideaClientService.SubmitIdea(ideaRequest); | ||
|
||
// Assert | ||
_ = result.Should().BeEquivalentTo(expectedIdeaResponse); | ||
MockHttpClient.VerifyNoOutstandingExpectation(); | ||
MockHttpClient.VerifyNoOutstandingRequest(); | ||
} | ||
} | ||
} |
Oops, something went wrong.