Skip to content

Commit

Permalink
test: Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
data-miner00 committed Jan 5, 2025
1 parent 210f280 commit 1e7f085
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
17 changes: 14 additions & 3 deletions src/Linker.Mvc.UnitTests/Controllers/LinkControllerSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,34 @@
using System;
using System.Collections.Generic;
using Serilog;
using Linker.Core.V2.Clients;

internal sealed class LinkControllerSteps : BaseSteps<LinkControllerSteps>
{
private readonly Mock<ILinkRepository> mockRepository;
private readonly Mock<IMapper> mockMapper;
private readonly Mock<ILogger> mockLogger;
private readonly Mock<ILinkUpdatedEventClient> mockClient;
private readonly LinkController controller;

public LinkControllerSteps()
{
this.mockRepository = new();
this.mockMapper = new();
this.mockLogger = new();
this.mockClient = new();

this.controller = new(this.mockRepository.Object, this.mockMapper.Object, this.mockLogger.Object);
this.controller = new(this.mockRepository.Object, this.mockMapper.Object, this.mockLogger.Object, this.mockClient.Object);
this.GivenIHaveDefaultHttpContext();
}

public override LinkControllerSteps GetSteps() => this;

public LinkControllerSteps WhenIInitWith(bool isRepoNull, bool isMapperNull, bool isLoggerNull)
public LinkControllerSteps WhenIInitWith(
bool isRepoNull,
bool isMapperNull,
bool isLoggerNull,
bool isClientNull)
{
var repo = isRepoNull
? null
Expand All @@ -46,7 +53,11 @@ public LinkControllerSteps WhenIInitWith(bool isRepoNull, bool isMapperNull, boo
? null
: this.mockLogger.Object;

return this.RecordException(() => new LinkController(repo, mapper, logger));
var client = isClientNull
? null
: this.mockClient.Object;

return this.RecordException(() => new LinkController(repo, mapper, logger, client));
}

public LinkControllerSteps GivenIHaveDefaultHttpContext()
Expand Down
12 changes: 7 additions & 5 deletions src/Linker.Mvc.UnitTests/Controllers/LinkControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ public sealed class LinkControllerTests
private readonly LinkControllerSteps steps = new();

[Theory]
[InlineData(true, false, false)]
[InlineData(false, true, false)]
[InlineData(false, false, true)]
[InlineData(true, false, false, false)]
[InlineData(false, true, false, false)]
[InlineData(false, false, true, false)]
[InlineData(false, false, false, true)]
public void Constructor_NullParameters_Throws(
bool isRepoNull,
bool isMapperNull,
bool isLoggerNull)
bool isLoggerNull,
bool isClientNull)
{
this.steps
.WhenIInitWith(isRepoNull, isMapperNull, isLoggerNull)
.WhenIInitWith(isRepoNull, isMapperNull, isLoggerNull, isClientNull)
.ThenIExpectExceptionIsThrown<ArgumentNullException>();
}

Expand Down
12 changes: 7 additions & 5 deletions src/Linker.Mvc.UnitTests/Features/LinkController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ Background:

@nulls
Scenario: Unable to initialize controller with "Null" parameters
When I initialize with "Null" for repo "<IsRepoNull>" and "Null" for mapper "<IsMapperNull>" and "Null" for logger "<IsLoggerNull>"
When I initialize with "Null" for repo "<IsRepoNull>" and "Null" for mapper "<IsMapperNull>" and "Null" for logger "<IsLoggerNull>" and "Null" for client "<IsClientNull>"
Then I expect "ArgumentNullException" should be thrown.
Examples:
| IsRepoNull | IsMapperNull | IsLoggerNull |
| true | false | false |
| false | true | false |
| false | false | true |
| IsRepoNull | IsMapperNull | IsLoggerNull | IsClientNull |
| true | false | false | false |
| false | true | false | false |
| false | false | true | false |
| false | false | false | true |


@get
Scenario: Return links when "Index" is called
Expand Down
24 changes: 13 additions & 11 deletions src/Linker.Mvc.UnitTests/Features/LinkController.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions src/Linker.Mvc.UnitTests/StepDefinitions/LinkControllerSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@
using Microsoft.AspNetCore.Mvc;
using Linker.TestCore.DataBuilders;
using Linker.Core.V2.QueryParams;
using Linker.Core.V2.Clients;

[Binding]
public sealed class LinkControllerSteps : BaseSteps<LinkControllerSteps>
{
private readonly Mock<ILinkRepository> mockRepo;
private readonly Mock<IMapper> mockMapper;
private readonly Mock<ILogger> mockLogger;
private readonly Mock<ILinkUpdatedEventClient> mockClient;
private readonly LinkController controller;

public LinkControllerSteps()
{
this.mockRepo = new Mock<ILinkRepository>();
this.mockMapper = new Mock<IMapper>();
this.mockLogger = new Mock<ILogger>();
this.mockClient = new();

this.controller = new LinkController(this.mockRepo.Object, this.mockMapper.Object, this.mockLogger.Object); ;
this.controller = new LinkController(
this.mockRepo.Object,
this.mockMapper.Object,
this.mockLogger.Object,
this.mockClient.Object);
}

// For additional details on SpecFlow hooks see http://go.specflow.org/doc-hooks
Expand Down Expand Up @@ -84,8 +91,8 @@ public void GivenRepositoryReturnLinksWithAnExampleName(string urlName)
.ReturnsAsync(links);
}

[When(@"I initialize with ""Null"" for repo ""(.*?)"" and ""Null"" for mapper ""(.*?)"" and ""Null"" for logger ""(.*?)""")]
public void WhenIInitializeWith(string isRepoNull, string isMapperNull, string isLoggerNull)
[When(@"I initialize with ""Null"" for repo ""(.*?)"" and ""Null"" for mapper ""(.*?)"" and ""Null"" for logger ""(.*?)"" and ""Null"" for client ""(.*?)""")]
public void WhenIInitializeWith(string isRepoNull, string isMapperNull, string isLoggerNull, string isClientNull)
{
var repo = isRepoNull == "true"
? null
Expand All @@ -99,7 +106,11 @@ public void WhenIInitializeWith(string isRepoNull, string isMapperNull, string i
? null
: this.mockLogger.Object;

this.RecordException(() => new LinkController(repo, mapper, logger));
var client = isClientNull == "true"
? null
: this.mockClient.Object;

this.RecordException(() => new LinkController(repo, mapper, logger, client));
}

[When(@"I invoke ""Index"" method")]
Expand Down

0 comments on commit 1e7f085

Please sign in to comment.