Skip to content

Instantly share code, notes, and snippets.

@drunkcod
Created November 27, 2016 19:21
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 drunkcod/8e3b0051c9fe4f0c0f5d4e27f63e093f to your computer and use it in GitHub Desktop.
Save drunkcod/8e3b0051c9fe4f0c0f5d4e27f63e093f to your computer and use it in GitHub Desktop.
Supersimple Mock/Fake Proxy vs handrolled
/* 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);
}
@jbrains
Copy link

jbrains commented Nov 27, 2016

I do love using lambda expressions in place of library-rolled stubs for one-method interfaces, though. That kicks ass.

    @Test
    public void productFound() throws Exception {
        final Price matchingPrice = Price.cents(795);

        final SellOneItemController controller
                = new SellOneItemController(
                        barcode -> Optional.of(matchingPrice));

        Assert.assertEquals(
                new ProductFoundMessage(matchingPrice),
                controller.onBarcode("12345")
        );
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment