.NET Core tests with xunit

This commit is contained in:
alexandre-spieser
2017-09-09 17:52:12 +00:00
parent fb4ee42a5c
commit 54efe5e520
16 changed files with 1332 additions and 5 deletions
@@ -0,0 +1,89 @@
using MongoDbGenericRepository.Models;
using Xunit;
using System.Collections.Generic;
using System.Configuration;
using System;
namespace IntegrationTests.Infrastructure
{
public class BaseMongoDbRepositoryTests<T> : IDisposable where T : Document, new()
{
public T CreateTestDocument()
{
return new T();
}
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
{
var docs = new List<T>();
for(var i = 0; i < numberOfDocumentsToCreate; i++)
{
docs.Add(new T());
}
return docs;
}
/// <summary>
/// Constructor, init code
/// </summary>
public BaseMongoDbRepositoryTests()
{
Init();
var type = CreateTestDocument();
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
}
public string PartitionKey { get; set; }
/// <summary>
/// SUT: System Under Test
/// </summary>
protected static ITestRepository SUT { get; set; }
public void Init()
{
SUT = TestRepository.Instance;
}
public void Cleanup()
{
// We drop the collection at the end of each test session.
if (!string.IsNullOrEmpty(PartitionKey))
{
SUT.DropTestCollection<T>(PartitionKey);
}
else
{
SUT.DropTestCollection<T>();
}
}
#region IDisposable Support
private bool disposedValue = false; // Pour détecter les appels redondants
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Cleanup();
}
disposedValue = true;
}
}
// Ce code est ajouté pour implémenter correctement le modèle supprimable.
public void Dispose()
{
// Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(bool disposing) ci-dessus.
Dispose(true);
}
#endregion
}
}
@@ -0,0 +1,10 @@
using MongoDbGenericRepository;
namespace IntegrationTests
{
public interface ITestRepository : IBaseMongoRepository
{
void DropTestCollection<TDocument>();
void DropTestCollection<TDocument>(string partitionKey);
}
}
@@ -0,0 +1,43 @@
using MongoDbGenericRepository;
namespace IntegrationTests.Infrastructure
{
/// <summary>
/// A singleton implementation of the TestRepository
/// </summary>
public sealed class TestRepository : BaseMongoRepository, ITestRepository
{
const string connectionString = "mongodb://localhost:27017";
private static readonly ITestRepository instance = new TestRepository(connectionString, "MongoDbTests");
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static TestRepository()
{
}
/// <inheritdoc />
private TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
{
}
public static ITestRepository Instance
{
get
{
return instance;
}
}
public void DropTestCollection<TDocument>()
{
MongoDbContext.DropCollection<TDocument>();
}
public void DropTestCollection<TDocument>(string partitionKey)
{
MongoDbContext.DropCollection<TDocument>(partitionKey);
}
}
}