Last active
August 6, 2020 18:09
-
-
Save iknowcodesoup/ae4326f7c805049b5a8d8613b68e7663 to your computer and use it in GitHub Desktop.
This is a non-tested sample of how to use TheoryData to create a unit testing setup using stubs / mocks. Have been using it for years with much success. Not sure of the original article but this one is the first hit: https://andrewlock.net/creating-strongly-typed-xunit-theory-test-data-with-theorydata/
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 System; | |
using Moq; | |
using Refit; | |
using Xunit; | |
namespace Some.Kind.Of.Space | |
{ | |
[Collection("Feature Name")] | |
[Trait("Feature Aspect", "Classification")] | |
public class FeatureClassNameTests | |
{ | |
[Theory] | |
[ClassData(typeof(TestData.ValidUseCases))] | |
public async Task Sends_Metadata_To_Service(string dataPoint1, string dataPoint2, Guid identifier, int count) | |
{ | |
// Arrange | |
var mockDataServiceGateway = new MockDataServiceGateway() | |
.MockAddMetadata($"{dataId}"); | |
var dataServiceAdapter = new DataServiceAdapter(null, null, mockDataServiceGateway.Object, null); | |
// Act | |
var response = await dataServiceAdapter.SetDataMetadata(dataId, identifier, datdataPoint1, dataPoint2); | |
// Assert | |
mockDataServiceGateway.Verify(x => x.AddMetadata(dataId, It.IsAny<DataMetadataDto>())); | |
} | |
[Theory] | |
[ClassData(typeof(TestData.ValidUseCases))] | |
public async Task Should_Return_A_Value_Correctly(string dataPoint1, string dataPoint2, Guid identifier, int count) | |
{ | |
// Arrange | |
var command = new DataModel { DataPoint1 = dataPoint1, DataPoint2 = dataPoint2, Identifier = identifier }; | |
var mockDataServiceGateway = new MockDataServiceGateway() | |
.MockAddMetadata($"{identifier}") | |
.MockUploadData($"{identifier}"); | |
var mockDataConverter = new MockDataCapture() | |
.MockGetData(new byte[] { }); | |
var mockDataSettings = new MockDataSettings(); | |
var dataServiceAdapter = new DataServiceAdapter(mockDataSettings.Object, null, mockDataServiceGateway.Object, mockDataConverter.Object); | |
// Act | |
var response = await dataServiceAdapter.UploadToDataService(command); | |
// Assert | |
Assert.Equal(response.DataId, dataId); | |
} | |
} | |
} |
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 System; | |
using System.Threading.Tasks; | |
using Moq; | |
using Refit; | |
namespace Some.Kind.Of.Space | |
{ | |
internal class MockDataServiceGateway : Mock<IDataServiceGateway> | |
{ | |
public MockDataServiceGateway MockAddMetadata(string dataId) | |
{ | |
Setup(x => x.AddMetadata( | |
It.IsAny<Guid>(), | |
It.IsAny<DataMetadataDto>() | |
)) | |
.Returns(Task.FromResult(dataId)); | |
return this; | |
} | |
public MockDataServiceGateway MockUploadData(string dataId) | |
{ | |
Setup(x => x.UploadData( | |
It.IsAny<StreamPart>() | |
)) | |
.Returns(Task.FromResult(dataId)); | |
return this; | |
} | |
} | |
} |
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 System; | |
using Xunit; | |
namespace Some.Kind.Of.Space | |
{ | |
public class TestData | |
{ | |
public class ValidUseCases : TheoryData<string, string, Guid, int> | |
{ | |
public ValidUseCases() | |
{ | |
Add("23434", "onyx", Guid.NewGuid(), 4); | |
Add("523532", "teal", Guid.NewGuid(), 6); | |
Add("23412", "fuschia", Guid.NewGuid(), 1); | |
Add("23452", "obligatory", Guid.NewGuid(), 8); | |
} | |
} | |
} | |
} |
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 System.Collections; | |
using System.Collections.Generic; | |
namespace Some.Kind.Of.Space | |
{ | |
internal abstract class TheoryData : IEnumerable<object[]> | |
{ | |
readonly List<object[]> data = new List<object[]>(); | |
protected void AddRow(params object[] values) | |
{ | |
data.Add(values); | |
} | |
public IEnumerator<object[]> GetEnumerator() | |
{ | |
return data.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment