Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.fake
.vscode
.vs/
Binary file removed .vs/MyFirstBlog/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,016 changes: 0 additions & 1,016 deletions .vs/MyFirstBlog/config/applicationhost.config

This file was deleted.

Binary file removed .vs/MyFirstBlog/v17/.futdcache.v2
Binary file not shown.
Binary file removed .vs/MyFirstBlog/v17/.suo
Binary file not shown.
149 changes: 0 additions & 149 deletions .vs/MyFirstBlog/v17/DocumentLayout.json

This file was deleted.

Binary file removed .vs/ProjectEvaluation/myfirstblog.metadata.v7.bin
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/myfirstblog.projects.v7.bin
Binary file not shown.
26 changes: 26 additions & 0 deletions MyFirstBlog.Tests/MyFirstBlog.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyFirstBlog\MyFirstBlog.csproj" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions MyFirstBlog.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Xunit;
using Moq;
using Microsoft.AspNetCore.Mvc;
using MyFirstBlog.Controllers;
using MyFirstBlog.Services;
using MyFirstBlog.Dtos;
using System;
using System.Collections.Generic;

public class PostsControllerTests
{
private readonly Mock<IPostService> _mockService;
private readonly PostsController _controller;

public PostsControllerTests()
{
_mockService = new Mock<IPostService>();
_controller = new PostsController(_mockService.Object);
}

[Fact]
public void CreatePost_ValidInput_ReturnsCreatedResponse()
{
var input = new PostDto { Title = "some title", Body = "some content" };
var expected = new PostDto { Title = "some title", Body = "some content", Slug = "some-title", CreatedDate = DateTime.UtcNow };

_mockService.Setup(s => s.CreatePost(input)).Returns(expected);

var result = _controller.PostCreateDto(input);

var createdResult = Assert.IsType<CreatedAtActionResult>(result.Result);
var post = Assert.IsType<PostDto>(createdResult.Value);

Assert.Equal(201, createdResult.StatusCode);
Assert.Equal(expected.Title, post.Title);
Assert.Equal(expected.Body, post.Body);
}

[Fact]
public void CreatePost_BlankTitle_ReturnsBadRequest()
{
var input = new PostDto { Title = "", Body = "some content" };

var result = _controller.PostCreateDto(input);

var badRequest = Assert.IsType<BadRequestObjectResult>(result.Result);
var errorObj = badRequest.Value;
var errorsProperty = errorObj.GetType().GetProperty("errors");
var errors = errorsProperty.GetValue(errorObj) as string[];

Assert.Contains("Title cannot be blank", errors);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added MyFirstBlog.Tests/bin/Debug/net9.0/Moq.dll
Binary file not shown.
Loading