Created
August 25, 2010 20:57
-
-
Save baldmountain/550281 to your computer and use it in GitHub Desktop.
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
stub = stub("stub") | |
customer = double("customer") | |
# single stub | |
customer = double("customer", :name => 'Bryan') | |
# multiple stubs | |
customer = double("customer", :name => 'Bryan', :open_source_projects => ['WebRat', 'Rack::Test']) | |
# single stub | |
customer.stub(:name).and_return("Aslak") | |
customer.should_receive(:name).and_return("Aslak") | |
logger = mock('logger') | |
logger.should_receive(:log).with(/Statement generated for Aslak/) | |
account.should_receive(:withdraw).with(target_account, instance_of(Fixnum)) | |
account.should_receive(:withdraw).with(anything(), instance_of(Fixnum)) | |
account.should_receive(:withdraw).with(any_args()) | |
account.should_receive(:withdraw).with(no_args()) | |
account.should_receive(:withdraw).with(hash_including('electric' => '123', 'gas' => '432')) | |
account.should_receive(:withdraw).with(hash_not_including('electric' => '123', 'gas' => '432')) | |
widget = Widget.new | |
Widget.stub!(:find).and_return(widget) | |
widget.stub!(:update_attribues).and_return(true) | |
Widget.should_receive(:update_attributes).with("37").and_return(widget) | |
ages = double('ages').stub(:age_for) do |what| | |
if what == 'drinking' | |
21 | |
elsif what == 'voting' | |
18 | |
end | |
end | |
# ActiveRecord named scopes method chains | |
article = double() | |
Article.stub_chain(:recent, :published, authored_by).and_return(article) | |
# counts | |
account.should_receive(:withdraw).exactly(1).times | |
account.should_receive(:withdraw).at_least(5).times | |
account.should_receive(:withdraw).at_most(1).times | |
account.should_receive(:withdraw).once | |
account.should_receive(:withdraw).twice | |
account.should_receive(:withdraw).never | |
account.should_not_receive(:withdraw) | |
account.should_receive(:withdraw).and_raise | |
account.should_receive(:withdraw).and_raise(InsufficientFunds) | |
account.should_receive(:withdraw).and_throw(:insufficient_funds) | |
# enforce an order add must follow multiply | |
result.should_receive(:multiply).ordered | |
result.should_receive(:add).ordered | |
# custom argument matchers | |
calculator.should_receive(:add).with(greater_than(3)) | |
class GreaterThanMatcher | |
def initialize(expected) | |
@expected = expected | |
end | |
def ==(actual) | |
actual > @expected | |
end | |
def description | |
"a number greater than #{@expected}" | |
end | |
end | |
def greater_than(floor) | |
GreaterThanThreeMatcher.new(floor) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment