Skip to content

Instantly share code, notes, and snippets.

@farukterzioglu
Created June 30, 2017 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farukterzioglu/a845d83eef505690d3c2dcea691aaa89 to your computer and use it in GitHub Desktop.
Save farukterzioglu/a845d83eef505690d3c2dcea691aaa89 to your computer and use it in GitHub Desktop.
Curiously Recurring Template Pattern (CRTP), (Generic, TDD, Fixture, SUT)
//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