This file contains hidden or 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
public class MyObject | |
{ | |
// Read Only -- must initialize in constructor! | |
public string Id { get; } | |
public double Count { get; } | |
public MyObject(string id, double count) | |
{ | |
Id = id; | |
Count = count; |
This file contains hidden or 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
[HttpPost("upload/{id}")] | |
public async Task<IActionResult> Upload(string id, [FromBody]MyObject myObject) | |
{ | |
if (await _myService.Upload(myObject)) | |
{ | |
return NoContent(); | |
} | |
return BadRequest(); | |
} |
This file contains hidden or 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
[Fact(DisplayName = "Sequencer can do callbacks in sequence")] | |
public void Sequencer_CallbacksInSequence() | |
{ | |
int expectedFirst = 5; | |
int expectedSecond = 10; | |
bool wasCallbackIssued = false; | |
var myMock = new Mock<IMyInterface>(); | |
myMock.Setup(m => m.CoolNumber()).Sequence(Sequencer.New, | |
() => { return expectedFirst; }, |
This file contains hidden or 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
public class Sequencer | |
{ | |
public int Sequence { get; private set; } = 0; | |
public int NextSequence => Sequence++; | |
public static Sequencer New => new Sequencer(); | |
} | |
public static class MoqExtensions | |
{ |
This file contains hidden or 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
[Fact(DisplayName = "Setup can issue a callback")] | |
public void Setup_IssuesCallback() | |
{ | |
bool wasCallbackIssued = false; | |
var myMock = new Mock<IMyInterface>(); | |
myMock.Setup(m => m.CoolNumber()).Callback(() => | |
{ | |
wasCallbackIssued = true; | |
}); |
This file contains hidden or 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
[Fact(DisplayName = "Setup can return a Func<T>")] | |
public void Setup_ReturnsFunc() | |
{ | |
int expected = 42; | |
var myMock = new Mock<IMyInterface>(); | |
myMock.Setup(m => m.CoolNumber()).Returns(() => | |
{ | |
// Do some fancy calculation... | |
return expected; |
This file contains hidden or 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
[Fact(DisplayName = "Setup can return an object directly")] | |
public void Setup_ReturnsObject() | |
{ | |
int expected = 42; | |
var myMock = new Mock<IMyInterface>(); | |
myMock.Setup(m => m.CoolNumber()).Returns(expected); | |
int actual = myMock.Object.CoolNumber(); |
This file contains hidden or 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
using FluentAssertions; | |
using Moq; | |
using System; | |
using System.Threading.Tasks; | |
using Xunit; | |
namespace MyUnitTestProject | |
{ | |
public interface IMyInterface | |
{ |
This file contains hidden or 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
using Flayed.Communication.Clients.Impl; | |
using Flayed.Communication.Clients; | |
namespace Flayed.Communication.Services | |
{ | |
public class ClientService : IClientService | |
{ | |
/// <summary> | |
/// Creates an <see cref="ITcpClient"/> client | |
/// </summary> |
This file contains hidden or 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
public class TcpClient : ITcpClient | |
{ | |
// The real implementation | |
private readonly System.Net.Sockets.TcpClient _client; | |
public EndPoint RemoteEndPoint => _client.Client.RemoteEndPoint; | |
public Stream GetStream() => _client.GetStream(); | |
public TcpClient() | |
{ | |
_client = new System.Net.Sockets.TcpClient(); |