Skip to content

Commit

Permalink
test: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
data-miner00 committed Dec 14, 2024
1 parent b86383e commit 3727794
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 25 deletions.
30 changes: 10 additions & 20 deletions src/Linker.Data.UnitTests.SpecFlow/Features/Calculator.feature.cs

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

2 changes: 1 addition & 1 deletion src/Linker.TestCore/DataBuilders/LinkDataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class LinkDataBuilder : ITestDataBuilder<Link>
private string? country;
private string? keyPersonName;
private Grammar grammar;
private Visibility visibility;
private Visibility visibility = Visibility.Public;
private DateTime createdAt = DateTime.Now;
private DateTime modifiedAt = DateTime.Now;

Expand Down
145 changes: 145 additions & 0 deletions src/Linker.WebApi.UnitTests/ArticleApiModelDataBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
namespace Linker.WebApi.UnitTests;

using Linker.WebApi.ApiModels;
using System;
using System.Collections.Generic;

public sealed class ArticleApiModelDataBuilder
{
private string id = Guid.NewGuid().ToString();
private string url = "https://google.com";
private string category = "Education";
private string description = "hello world";
private IEnumerable<string> tags = new List<string>() { "tag1", "tag2" };
private string language = "en";
private DateTime lastVisitAt = DateTime.Now;
private DateTime createdAt = DateTime.Now;
private DateTime modifiedAt = DateTime.Now;
private string createdBy = Guid.NewGuid().ToString();

private string title = "title";
private string author = "Author";
private int year = 2004;
private bool watchLater;
private string domain = "google.com";
private string grammar = "Normal";

public ArticleApiModelDataBuilder WithId(string value)
{
this.id = value;
return this;
}

public ArticleApiModelDataBuilder WithUrl(string value)
{
this.url = value;
return this;
}

public ArticleApiModelDataBuilder WithCategory(string value)
{
this.category = value;
return this;
}

public ArticleApiModelDataBuilder WithDescription(string value)
{
this.description = value;
return this;
}

public ArticleApiModelDataBuilder WithTags(IEnumerable<string> value)
{
this.tags = value;
return this;
}

public ArticleApiModelDataBuilder WithLanguage(string value)
{
this.language = value;
return this;
}

public ArticleApiModelDataBuilder WithLastVisitAt(DateTime value)
{
this.lastVisitAt = value;
return this;
}

public ArticleApiModelDataBuilder WithCreatedAt(DateTime value)
{
this.createdAt = value;
return this;
}

public ArticleApiModelDataBuilder WithModifiedAt(DateTime value)
{
this.modifiedAt = value;
return this;
}

public ArticleApiModelDataBuilder WithCreatedBy(string value)
{
this.createdBy = value;
return this;
}

public ArticleApiModelDataBuilder WithTitle(string value)
{
this.title = value;
return this;
}

public ArticleApiModelDataBuilder WithAuthor(string value)
{
this.author = value;
return this;
}

public ArticleApiModelDataBuilder WithYear(int value)
{
this.year = value;
return this;
}

public ArticleApiModelDataBuilder WithWatchLater(bool value)
{
this.watchLater = value;
return this;
}

public ArticleApiModelDataBuilder WithDomain(string value)
{
this.domain = value;
return this;
}

public ArticleApiModelDataBuilder WithGrammar(string value)
{
this.grammar = value;
return this;
}

public ArticleApiModel Build()
{
return new ArticleApiModel
{
Id = this.id,
Url = this.url,
Category = this.category,
Description = this.description,
Tags = this.tags,
Language = this.language,
LastVisitAt = this.lastVisitAt,
CreatedAt = this.createdAt,
ModifiedAt = this.modifiedAt,
CreatedBy = this.createdBy,
Title = this.title,
Author = this.author,
Year = this.year,
WatchLater = this.watchLater,
Domain = this.domain,
Grammar = this.grammar,
};
}
}
20 changes: 16 additions & 4 deletions src/Linker.WebApi.UnitTests/Controllers/ArticleControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Linker.Core.Models;
using Linker.TestCore.DataBuilders;
using Linker.WebApi.ApiModels;
using Linker.WebApi.UnitTests.Steps;
using Microsoft.AspNetCore.Mvc;
using Xunit;
Expand Down Expand Up @@ -101,15 +102,23 @@ public async Task GetAllAsync_HavingResults_ReturnResults()
new ArticleDataBuilder().Build(),
};

var articleApiModels = new List<ArticleApiModel>
{
new ArticleApiModelDataBuilder().Build(),
new ArticleApiModelDataBuilder().Build(),
};

this.steps
.GivenRepoGetAllAsyncReturns(articles);
.GivenRepoGetAllAsyncReturns(articles)
.GivenMapperMapToArticleApiModelReturns(articleApiModels);

await this.steps.WhenIGetAllAsync();

this.steps
.ThenIExpectNoExceptionIsThrown()
.ThenIExpectRepoGetAllAsyncToBeCalled(1)
.ThenIExpectResultToBe(new OkObjectResult(articles));
.ThenIExpectMapperToBeCalledWithArticle(2)
.ThenIExpectResultToBe(new OkObjectResult(articleApiModels));
}

[Fact]
Expand All @@ -118,16 +127,19 @@ public async Task GetByIdAsync_FoundRecord_ReturnResult()
var guidStr = "ba3e784b-5edd-432d-a6fb-5215c27d83d2";
var guid = Guid.Parse(guidStr);
var article = new ArticleDataBuilder().WithId(guidStr).Build();
var articleApiModel = new ArticleApiModelDataBuilder().WithId(guidStr).Build();

this.steps
.GivenRepoGetByIdAsyncReturns(article);
.GivenRepoGetByIdAsyncReturns(article)
.GivenMapperMapToArticleApiModelReturns([articleApiModel]);

await this.steps.WhenIGetByIdAsync(guid);

this.steps
.ThenIExpectNoExceptionIsThrown()
.ThenIExpectRepoGetByIdAsyncToBeCalledWith(guidStr, 1)
.ThenIExpectResultToBe(new OkObjectResult(article));
.ThenIExpectMapperToBeCalledWithArticle(1)
.ThenIExpectResultToBe(new OkObjectResult(articleApiModel));
}

[Fact]
Expand Down
19 changes: 19 additions & 0 deletions src/Linker.WebApi.UnitTests/Steps/ArticleControllerSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Linker.Core.Models;
using Linker.Core.Repositories;
using Linker.TestCore;
using Linker.WebApi.ApiModels;
using Linker.WebApi.Controllers;
using Microsoft.AspNetCore.Http;

Expand Down Expand Up @@ -125,6 +126,16 @@ public ArticleControllerSteps GivenRepoGetAllAsyncReturns(IEnumerable<Article> a
return this;
}

public ArticleControllerSteps GivenMapperMapToArticleApiModelReturns(IEnumerable<ArticleApiModel> apiModels)
{
this.mockMapper
.SetupSequence(x => x.Map<ArticleApiModel>(It.IsAny<Article>()))
.Returns(apiModels.First())
.Returns(apiModels.Last());

return this;
}

public ArticleControllerSteps GivenRepoGetByIdAsyncReturns(Article article)
{
this.mockRepository
Expand Down Expand Up @@ -213,6 +224,14 @@ public Task WhenIUpdateAsync(Guid id, UpdateArticleRequest request)

#region Then

public ArticleControllerSteps ThenIExpectMapperToBeCalledWithArticle(int times)
{
this.mockMapper
.Verify(x => x.Map<ArticleApiModel>(It.IsAny<Article>()), Times.Exactly(times));

return this;
}

public ArticleControllerSteps ThenIExpectMapperToBeCalledWith(CreateArticleRequest request, int times)
{
this.mockMapper
Expand Down

0 comments on commit 3727794

Please sign in to comment.