Created
June 5, 2011 15:29
-
-
Save rentalcustard/1009053 to your computer and use it in GitHub Desktop.
Mocks suck - code for embedding into my blog post on this topic
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
#Arrange | |
my_thing = Thing.new :colour => :yellow | |
#Act | |
my_thing.bleach! | |
#Assert | |
my_thing.colour.should == :white |
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
obj = MyAwesomeObject.new | |
helper = spy(:helper) | |
obj.helper = helper | |
obj.do_something_cool | |
helper.should have_received.bleach_available? |
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
import static org.mockito.Mockito.*; | |
List mockedList = mock(List.class); | |
mockedList.add("one"); | |
mockedList.clear(); | |
verify(mockedList).add("one"); |
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
#Arrange | |
helper = mock(:helper) | |
my_thing = Thing.new :helper => helper | |
#Assert | |
helper.should_receive(:message) | |
#Act | |
my_thing.do_something_cool |
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
#Arrange | |
helper = mock(:helper) | |
my_thing = Thing.new :colour => :yellow, :helper => helper | |
#Assert... Arrange... both?! | |
helper.should_receive(:bleach_available?).and_return(true) | |
#Act | |
my_thing.do_something_cool | |
#Assert again! | |
my_thing.colour.should == :white |
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
subject = Object.new | |
stub(subject).foo | |
subject.foo(1) | |
subject.should have_received.foo(1) | |
subject.should have_received.bar # this fails |
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
describe Thing do | |
context "when yellow" do | |
before { my_thing = Thing.new :colour => :yellow } | |
it "is white after bleaching" do | |
my_thing.bleach! | |
my_thing.colour.should == :white | |
end | |
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
def send_to_warehouse(item) | |
warehouse << item | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment