Skip to content

Commit df88765

Browse files
committed
add extension tests
1 parent 1d1ac7f commit df88765

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

JsonApiDotNetCoreTests/Extensions/IServiceCollectionExtensionsTests.cs renamed to JsonApiDotNetCoreTests/Extensions/UnitTests/IServiceCollectionExtensionsTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using JsonApiDotNetCore.Extensions;
44
using JsonApiDotNetCoreTests.Helpers;
55
using Microsoft.EntityFrameworkCore;
6+
using System;
67

78
namespace JsonApiDotNetCoreTests.Extensions.UnitTests
89
{
9-
// see example explanation on xUnit.net website:
10-
// https://xunit.github.io/docs/getting-started-dotnet-core.html
1110
public class IServiceCollectionExtensionsTests
1211
{
1312
[Fact]
@@ -17,12 +16,29 @@ public void AddJsonApi_AddsRouterToServiceCollection()
1716
var serviceCollection = new ServiceCollection();
1817

1918
// act
20-
serviceCollection.AddJsonApi(config => {
21-
config.UseContext<DbContext>();
19+
serviceCollection.AddJsonApi(config =>
20+
{
21+
config.UseContext<DbContext>();
2222
});
2323

2424
// assert
2525
Assert.True(serviceCollection.ContainsType(typeof(IRouter)));
2626
}
27+
28+
[Fact]
29+
public void AddJsonApi_ThrowsException_IfContextIsNotDefined()
30+
{
31+
// arrange
32+
var serviceCollection = new ServiceCollection();
33+
34+
// act
35+
var testAction = new Action(() =>
36+
{
37+
serviceCollection.AddJsonApi(config => { });
38+
});
39+
40+
// assert
41+
Assert.Throws<NullReferenceException>(testAction);
42+
}
2743
}
2844
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Xunit;
2+
using JsonApiDotNetCore.Extensions;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace JsonApiDotNetCoreTests.Extensions.UnitTests
6+
{
7+
public class PathStringExtensionsTests
8+
{
9+
[Theory]
10+
[InlineData("/todoItems", "todoItems", "/")]
11+
[InlineData("/todoItems/1", "todoItems", "/1")]
12+
[InlineData("/1/relationships/person", "1", "/relationships/person")]
13+
[InlineData("/relationships/person", "relationships", "/person")]
14+
public void ExtractFirstSegment_Removes_And_Returns_FirstSegementInPathString(string path, string expectedFirstSegment, string expectedRemainder)
15+
{
16+
// arrange
17+
var pathString = new PathString(path);
18+
19+
// act
20+
PathString remainingPath;
21+
var firstSegment = pathString.ExtractFirstSegment(out remainingPath);
22+
23+
// assert
24+
Assert.Equal(expectedFirstSegment, firstSegment);
25+
Assert.Equal(expectedRemainder, remainingPath);
26+
}
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Xunit;
2+
using JsonApiDotNetCore.Extensions;
3+
4+
namespace JsonApiDotNetCoreTests.Extensions.UnitTests
5+
{
6+
public class StringExtensionsTests
7+
{
8+
[Theory]
9+
[InlineData("TodoItem", "todoItem")]
10+
public void ToCamelCase_ConvertsString_ToCamelCase(string input, string expectedOutput)
11+
{
12+
// arrange
13+
// act
14+
var result = input.ToCamelCase();
15+
16+
// assert
17+
Assert.Equal(expectedOutput, result);
18+
}
19+
20+
[Theory]
21+
[InlineData("todoItem", "TodoItem")]
22+
public void ToProperCase_ConvertsString_ToProperCase(string input, string expectedOutput)
23+
{
24+
// arrange
25+
// act
26+
var result = input.ToProperCase();
27+
28+
// assert
29+
Assert.Equal(expectedOutput, result);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)