Created
June 30, 2017 08:32
-
-
Save farukterzioglu/a845d83eef505690d3c2dcea691aaa89 to your computer and use it in GitHub Desktop.
Curiously Recurring Template Pattern (CRTP), (Generic, TDD, Fixture, SUT)
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
//Curiously Recurring Template Pattern (CRTP) | |
//http://zpbappi.com/curiously-recurring-template-pattern-in-csharp/ | |
public interface IFixture<out TFixture, out TSut> | |
where TFixture : IFixture<TFixture, TSut> | |
where TSut : class | |
{ | |
TFixture Setup(); | |
TSut CreateSut(); | |
} | |
public abstract class Fixture<TFixture, TSut> : IFixture<TFixture, TSut> | |
where TFixture : Fixture<TFixture, TSut> | |
where TSut : class | |
{ | |
public virtual TFixture Init() | |
{ | |
return (TFixture) this; | |
} | |
public abstract TFixture Setup(); | |
public abstract TSut CreateSut(); | |
} | |
public class MySut{ | |
public void DoSomething() | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public class MyFixture : Fixture<MyFixture, MySut> | |
{ | |
public override MyFixture Setup() | |
{ | |
throw new NotImplementedException(); | |
} | |
public override MySut CreateSut() | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public class AnotherFixture : Fixture<MyFixture, MySut> | |
{ | |
public override MyFixture Init() | |
{ | |
//Do some thing | |
return base.Init(); | |
} | |
public override MyFixture Setup() | |
{ | |
throw new NotImplementedException(); | |
} | |
public override MySut CreateSut() | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public class Test | |
{ | |
public void TestMethod() | |
{ | |
var fixture = | |
new MyFixture() | |
.Init() | |
.Setup(); | |
var sut = fixture.CreateSut(); | |
sut.DoSomething(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment