Created
November 27, 2016 19:21
-
-
Save drunkcod/8e3b0051c9fe4f0c0f5d4e27f63e093f to your computer and use it in GitHub Desktop.
Supersimple Mock/Fake Proxy vs handrolled
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 FakeItEasy and Cone */ | |
interface ICandy { } | |
interface ICandyShop | |
{ | |
ICandy GetTopSellingCandy(); | |
void BuyCandy(ICandy candy); | |
} | |
class SweetTooth | |
{ | |
public void BuyTastiestCandy(ICandyShop shop) {} | |
} | |
//http://fakeiteasy.readthedocs.io/en/stable/quickstart/ | |
public class SweetToothTests | |
{ | |
public void BuyTastiestCandy_should_buy_top_selling_candy_from_shop() | |
{ | |
// make some fakes for the test | |
var lollipop = A.Fake<ICandy>(); | |
var shop = A.Fake<ICandyShop>(); | |
// set up a call to return a value | |
A.CallTo(() => shop.GetTopSellingCandy()).Returns(lollipop); | |
// use the fake as an actual instance of the faked type | |
var developer = new SweetTooth(); | |
developer.BuyTastiestCandy(shop); | |
// asserting uses the exact same syntax as when configuring calls— | |
// no need to learn another syntax | |
A.CallTo(() => shop.BuyCandy(lollipop)).MustHaveHappened(); | |
} | |
public void look_ma_no_mocks() { | |
var lollipop = new Lollipop(); | |
var shop = new TestableShop { HandleGetTopSellingCandy = () => lollipop } | |
var developer = new SweetTooth(); | |
developer.BuyTastiestCandy(shop); | |
var buyCandy = MethodSpy.On(ref shop.HandlyBuyCandy, candy => Check.That(() => candy == lollipop)); | |
Check.That(() => buyCandy.HasBeenCalled); | |
} | |
} | |
class Lollipop : ICandy { } | |
class TestableShop : ICandyShop | |
{ | |
public Func<ICandy> HandleGetTopSellingCandy = () => null; | |
public ICandy GetTopSellingCandy() => HandleGetTopSellingCandy(); | |
public Action<ICandyShop> HandlyBuyCandy = candy => {}; | |
public void BuyCandy(ICandy candy) => BuyCandy(candy); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do love using lambda expressions in place of library-rolled stubs for one-method interfaces, though. That kicks ass.