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
public IServiceProvider ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
var container = new UnityContainer(); | |
container.Populate(services); | |
container.Resolve<IScopedInstance<ActionContext>>(); | |
return container.Resolve<IServiceProvider>(); | |
} |
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
public static class UnityRegistration | |
{ | |
public static void Populate(this IUnityContainer container, | |
IServiceCollection services) | |
{ | |
container.AddExtension(new EnumerableResolutionExtension()); | |
container.RegisterInstance(services); | |
container.RegisterType<IServiceProvider, UnityServiceProvider>(); | |
container.RegisterType<IServiceScopeFactory, UnityServiceScopeFactory>(); |
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
public class UnityServiceScope : IServiceScope | |
{ | |
private readonly IUnityContainer _container; | |
public IServiceProvider ServiceProvider { get; } | |
public UnityServiceScope(IUnityContainer container) | |
{ | |
_container = container; | |
ServiceProvider = _container.Resolve<IServiceProvider>(); | |
} |
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
public IServiceProvider ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
var container = new UnityContainer(); | |
container.Populate(services); | |
return container.Resolve<IServiceProvider>(); | |
} |
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
public class Startup | |
{ | |
// This method gets called by a runtime. Use this method to add services to the container | |
public IServiceProvider ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddTransient<ITransientService, TransientService>(); | |
services.AddScoped<IScopedService, ScopedService>(); | |
services.AddSingleton<ISingletonService, SingletonService>(); | |
} |
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
using Ploeh.AutoFixture; | |
using Ploeh.AutoFixture.AutoMoq; | |
using Ploeh.AutoFixture.Xunit; | |
namespace MyTestingSuite | |
{ | |
public class AutoMoqDataAttribute : AutoDataAttribute | |
{ | |
public AutoMoqDataAttribute() | |
: base(new Fixture().Customize(new AutoMoqCustomization())) |
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
[Theory, AutoMoqData] | |
public void Should_Throw_On_Invalid_Email([Frozen] Mock<IProductRepo> productRepo, | |
Product product, Customer customer, TransactionService testTransactionService) | |
{ | |
customer.Email = "Not an email address"; | |
productRepo.setup(repo => repo.GetProduct(It.Is<int>(product.id)).Returns(product); | |
Assert.Throws(() => testTransactionService.Sell(product.Id,customer)); | |
} |
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
var fixture = new Fixture(); | |
var someString = fixture.Create<string>(); | |
Console.WriteLine(someString); | |
// 30a35da1-d681-441b-9db3-77ff51728b58 | |
var generatedPerson = fixture.Create<Person>(); | |
Console.WriteLine(generatedPerson.Name); | |
// namef5cdf6b1-a473-410f-95f3-f427f7abb0c7 |
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
[Theory] | |
[InlineData(2)] | |
[InlineData(0)] | |
[InlineData(-1)] | |
public void Should_Correctly_Detect_Negative(int value) | |
{ | |
var service = new NegativeService(); | |
if(value < 0) | |
Assert.True(service.IsNegative(value)); | |
else |
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
[Fact] | |
public void Basic_Unit_Test() | |
{ | |
var mockRepo = new Mock<IMyRepo>(); | |
mockRepo.Setup(foo => foo.GetSomething(It.IsAny<int>())).Returns("Something"); | |
var serviceUnderTest = new MyUpperService(mockRepo); | |
var result = serviceUnderTest.MakeUpper(); | |
Assert.Equal("SOMETHING",result); | |
} |
NewerOlder