Last active
December 17, 2015 18:29
-
-
Save lee-dohm/5653431 to your computer and use it in GitHub Desktop.
Code examples for "Dynamic Typing Makes for Testability" article
This file contains hidden or 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
class A | |
{ | |
private B b; | |
public A(B b) | |
{ | |
this.b = b; | |
} | |
public void store() | |
{ | |
// Stores important things into the database | |
this.b.store(); | |
} | |
} |
This file contains hidden or 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
class A | |
def initialize(b) | |
@b = b | |
end | |
def store | |
# Stores important things into the database | |
@b.store | |
end | |
end |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
class B | |
{ | |
public final void store() | |
{ | |
// Writes to the database | |
} | |
} |
This file contains hidden or 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
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; | |
} | |
} | |
} |
This file contains hidden or 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
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