-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlogPostTests.cs
96 lines (79 loc) · 3.39 KB
/
BlogPostTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace BlogPostTests
{
// BlogPost model and IBlogRepository interface
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; }
}
public interface IBlogRepository
{
Task<BlogPost> GetBlogPostByIdAsync(int id);
}
[TestFixture]
public class BlogPostControllerTests
{
private BlogPostController _controller;
private Mock<IMemoryCache> _memoryCacheMock;
private Mock<IBlogRepository> _blogRepositoryMock;
[SetUp]
public void Setup()
{
// Create mock objects for IMemoryCache and IBlogRepository
_memoryCacheMock = new Mock<IMemoryCache>();
_blogRepositoryMock = new Mock<IBlogRepository>();
// Instantiate BlogPostController with the mocked dependencies
_controller = new BlogPostController(_memoryCacheMock.Object, _blogRepositoryMock.Object);
}
[Test]
public async Task GetBlogPost_ReturnsCachedBlogPost_WhenBlogPostIsInCache()
{
// Arrange
var blogPostId = 1;
var cachedBlogPost = new BlogPost { Id = blogPostId, Title = "Cached Post" };
// Mock cache to return the blog post
_memoryCacheMock.Setup(mc => mc.TryGetValue($"BlogPost_{blogPostId}", out cachedBlogPost)).Returns(true);
// Act
var result = await _controller.GetBlogPost(blogPostId) as OkObjectResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(200, result.StatusCode);
Assert.AreEqual(cachedBlogPost, result.Value);
// Ensure the blog repository was never called because it was cached
_blogRepositoryMock.Verify(repo => repo.GetBlogPostByIdAsync(It.IsAny<int>()), Times.Never);
}
[Test]
public async Task GetBlogPost_RetrievesAndCachesBlogPost_WhenBlogPostIsNotInCache()
{
// Arrange
var blogPostId = 2;
BlogPost cachedBlogPost = null;
var newBlogPost = new BlogPost { Id = blogPostId, Title = "New Post" };
// Mock cache to return null (simulating a cache miss)
_memoryCacheMock.Setup(mc => mc.TryGetValue($"BlogPost_{blogPostId}", out cachedBlogPost)).Returns(false);
// Mock the blog repository to return a blog post
_blogRepositoryMock.Setup(repo => repo.GetBlogPostByIdAsync(blogPostId)).ReturnsAsync(newBlogPost);
// Act
var result = await _controller.GetBlogPost(blogPostId) as OkObjectResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(200, result.StatusCode);
Assert.AreEqual(newBlogPost, result.Value);
// Verify that the blog post was retrieved from the repository
_blogRepositoryMock.Verify(repo => repo.GetBlogPostByIdAsync(blogPostId), Times.Once);
// Verify that the blog post was added to the cache
_memoryCacheMock.Verify(mc => mc.Set(
$"BlogPost_{blogPostId}",
It.Is<BlogPost>(p => p.Id == blogPostId),
It.IsAny<MemoryCacheEntryOptions>()),
Times.Once);
}
}
}