Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Created June 5, 2011 15:29
Show Gist options
  • Save rentalcustard/1009053 to your computer and use it in GitHub Desktop.
Save rentalcustard/1009053 to your computer and use it in GitHub Desktop.
Mocks suck - code for embedding into my blog post on this topic
#Arrange
my_thing = Thing.new :colour => :yellow
#Act
my_thing.bleach!
#Assert
my_thing.colour.should == :white
obj = MyAwesomeObject.new
helper = spy(:helper)
obj.helper = helper
obj.do_something_cool
helper.should have_received.bleach_available?
import static org.mockito.Mockito.*;
List mockedList = mock(List.class);
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
#Arrange
helper = mock(:helper)
my_thing = Thing.new :helper => helper
#Assert
helper.should_receive(:message)
#Act
my_thing.do_something_cool
#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
subject = Object.new
stub(subject).foo
subject.foo(1)
subject.should have_received.foo(1)
subject.should have_received.bar # this fails
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
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