-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new SessionFake with samples
- Loading branch information
1 parent
523b9bc
commit c517b94
Showing
3 changed files
with
195 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace HttpContextMoq; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
public class SessionFake : ISession | ||
{ | ||
private readonly Dictionary<string, byte[]> data = new(StringComparer.OrdinalIgnoreCase); | ||
|
||
public bool IsAvailable => true; | ||
|
||
public string Id { get; } = Guid.NewGuid().ToString(); | ||
|
||
public IEnumerable<string> Keys => this.data.Keys; | ||
|
||
public void Clear() => this.data.Clear(); | ||
|
||
public Task CommitAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task LoadAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public void Remove(string key) => this.data.Remove(key); | ||
|
||
public void Set(string key, byte[] value) => this.data.Add(key, value); | ||
|
||
public bool TryGetValue(string key, out byte[] value) => this.data.TryGetValue(key, out value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Text; | ||
using FluentAssertions; | ||
using HttpContextMoq.Extensions; | ||
using Microsoft.AspNetCore.Http; | ||
using Xunit; | ||
|
||
namespace HttpContextMoq.Samples; | ||
|
||
public class SessionSamples | ||
{ | ||
[Fact] | ||
public void SetupSession_WhenIfCalledWithMock_ShouldReturnValue() | ||
{ | ||
/// | ||
/// Default setup using the SessionMock. You can mock any Method or Property from the ISession interface. | ||
/// In this sample, TryGetValue is mocked to return a byte[]. | ||
/// | ||
|
||
// Arrange | ||
var context = new HttpContextMock(); | ||
context.SetupSession(); | ||
|
||
var value = Encoding.UTF8.GetBytes("Mike"); | ||
context.SessionMock.Mock.Setup(x => x.TryGetValue("Name", out value)).Returns(true); | ||
|
||
// Act | ||
var result = context.Session.GetString("Name"); | ||
|
||
// Assert | ||
result.Should().Be("Mike"); | ||
} | ||
|
||
[Fact] | ||
public void SetupSession_IfCalledWithFake_ShouldReturnValue() | ||
{ | ||
/// | ||
/// Use the SessionFake to mock your session with a Fake implementation. Every method or extension for ISession works. | ||
/// You can also create your own ISession implementaion and use it in the SetupSession<T>(). e.g. context.SetupSession<MyOwnSessionFake>(). | ||
/// | ||
|
||
// Arrange | ||
var context = new HttpContextMock(); | ||
context.SetupSession<SessionFake>(); | ||
|
||
context.Session.SetString("Name", "Mike"); | ||
|
||
// Act | ||
var result = context.Session.GetString("Name"); | ||
|
||
// Assert | ||
result.Should().Be("Mike"); | ||
} | ||
} |