Skip to content

Instantly share code, notes, and snippets.

@lee-dohm
Last active December 17, 2015 18:29
Show Gist options
  • Save lee-dohm/5653431 to your computer and use it in GitHub Desktop.
Save lee-dohm/5653431 to your computer and use it in GitHub Desktop.
Code examples for "Dynamic Typing Makes for Testability" article
class A
{
private B b;
public A(B b)
{
this.b = b;
}
public void store()
{
// Stores important things into the database
this.b.store();
}
}
class A
def initialize(b)
@b = b
end
def store
# Stores important things into the database
@b.store
end
end
class MockB extends B
{
public boolean storeCalled = false;
public void store()
{
this.storeCalled = true;
}
}
class ATest
{
public void storeTest()
{
MockB b = new MockB();
A a = new A(b);
a.store();
assert(b.storeCalled);
}
}
class B
{
public final void store()
{
// Writes to the database
}
}
class MockB
{
private boolean mock;
private B b;
public boolean storeCalled = false;
public MockB(boolean mock = false)
{
this.mock = mock;
if(!this.mock)
{
this.b = new B();
}
}
public void store()
{
if(!this.mock)
{
this.b.store();
}
else
{
this.storeCalled = true;
}
}
}
class MockB implements B
{
public boolean storeCalled = false;
public void store()
{
this.storeCalled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment