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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.fake
.vscode
# Visual Studio
.vs/
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/

# NuGet Packages
*.nupkg
**/packages/*
!**/packages/build/
Binary file modified .vs/MyFirstBlog/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/MyFirstBlog/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/MyFirstBlog/v17/.suo
Binary file not shown.
194 changes: 119 additions & 75 deletions .vs/MyFirstBlog/v17/DocumentLayout.json

Large diffs are not rendered by default.

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.
1 change: 1 addition & 0 deletions MyFirstBlog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
});
});


services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
Expand Down
4 changes: 3 additions & 1 deletion MyFirstBlog/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",

//"AllowedHosts": "*"
}
57 changes: 57 additions & 0 deletions MyFirstBlogTests/Controllers/PostsControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Mvc;
using Moq;
using MyFirstBlog.Controllers;
using MyFirstBlog.Dtos;
using MyFirstBlog.Services;
using System;
using Xunit;

namespace MyFirstBlogTests.Controllers
{
public class PostsControllerTests
{
[Fact]
public void GetPost_ReturnsPost_WhenSlugExists()
{
// Step 1: Create a fake post service
var fakePostService = new Mock<IPostService>();

// Step 2: Create a fake post to return
var fakePost = new PostDto
{
Title = "My Blog Post Test",
Slug = "my-blog-post-test"
};

// Step 3: Tell the fake service what to return when called
fakePostService.Setup(service => service.GetPost("my-blog-post"))
.Returns(fakePost);

// Step 4: Create the controller with our fake service
var controller = new PostsController(fakePostService.Object);

// Step 5: Call the method we want to test
var result = controller.GetPost("my-blog-post");

// Step 6: Check if we got the right answer
Assert.Equal("My Blog Post", result.Value.Title);
}

[Fact]
public void GetPost_ReturnsNotFound_WhenSlugDoesNotExist()
{
// Create a fake service that returns nothing
var mockPostService = new Mock<IPostService>();
mockPostService.Setup(service => service.GetPost("fake-slug"))
.Returns((PostDto)null);

// Create controller
var controller = new PostsController(mockPostService.Object);

// Call the method
var result = controller.GetPost("fake-slug");

Assert.IsType<NotFoundResult>(result.Result);
}
}
}
17 changes: 11 additions & 6 deletions MyFirstBlogTests/MyFirstBlogTests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4"/>
<PackageReference Include="NUnit" Version="3.13.1"/>
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/>
<PackageReference Include="coverlet.collector" Version="3.0.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Moq" Version="4.18.4" />
</ItemGroup>

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

</Project>
52 changes: 46 additions & 6 deletions MyFirstBlogTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
using Microsoft.AspNetCore.Mvc;
using Moq;
using MyFirstBlog.Controllers;
using MyFirstBlog.Dtos;
using MyFirstBlog.Services;
using System;
using NUnit.Framework;

namespace MyFirstBlogTests
namespace MyFirstBlogTests.Controllers
{
public class Tests
public class PostsControllerTests
{
[SetUp]
public void Setup()
[Test]
public void IfSlugExists()
{
// Step 1: Create a fake post service
var fakePostService = new Mock<IPostService>();

// Step 2: Create a fake post to return
var fakePost = new PostDto
{
Title = "Testing My Blog Post ",
Slug = "my-blog-post"
};

// Step 3: Tell the fake service what to return when called
fakePostService.Setup(service => service.GetPost("my-blog-post"))
.Returns(fakePost);

// Step 4: Create the controller with our fake service
var controller = new PostsController(fakePostService.Object);

// Step 5: Call the method we want to test
var result = controller.GetPost("my-blog-post");

// Step 6: Check if we got the right answer
Assert.Equals("Testing My Blog Post ", result.Value.Title);
}

[Test]
public void Test1()
public void IfSlugDoesNotExist()
{
Assert.Pass();
// Create a fake service that returns nothing
var mockPostService = new Mock<IPostService>();
mockPostService.Setup(service => service.GetPost("fake-slug"))
.Returns((PostDto)null);

// Create controller
var controller = new PostsController(mockPostService.Object);

// Call the method
var result = controller.GetPost("fake-slug");

// Check that we got "Not Found"
Assert.That(result.Result, Is.InstanceOf<NotFoundResult>());
}
}
}