Mock vs Stubs CleRB Presentation
Upcoming SlideShare
Loading in...5
×
 

Mock vs Stubs CleRB Presentation

on

  • 2,350 views

"Mocks vs Stubs" Cleveland Ruby Brigade (CleRB) Presentation

"Mocks vs Stubs" Cleveland Ruby Brigade (CleRB) Presentation

Statistics

Views

Total Views
2,350
Views on SlideShare
1,503
Embed Views
847

Actions

Likes
3
Downloads
24
Comments
0

5 Embeds 847

http://www.adomokos.com 793
http://www.techgig.com 51
http://translate.googleusercontent.com 1
http://www.techgig.timesjobs.com 1
http://www.slashdocs.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Mock vs Stubs CleRB Presentation Mock vs Stubs CleRB Presentation Presentation Transcript

  • MOCKS VS STUBS - How will we test this?
  • WHO AM I?• Attila Domokos• .NET Programmer• Ruby Enthusiast• Twitter: @adomokos• Blog: http://www.adomokos.com• Email:
  • WHO...??
  • MOCKS AREN’T STUBS
  • THE WAREHOUSE EXAMPLE
  • CALL SEQUENCES
  • describe Order do before(:each) do @warehouse = Warehouse.new @warehouse.add(:Cleveland, 50) @warehouse.add(:Akron, 25) end it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) order.fill(@warehouse) order.should be_filled @warehouse.quantity_at(:Cleveland).should == 0 end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) order.fill(@warehouse) order.should_not be_filled @warehouse.quantity_at(:Cleveland).should == 50 endend
  • USING A MOCKdescribe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) order.fill(warehouse) order.should_not be_filled endend
  • USING A MOCKdescribe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) order.fill(warehouse) order.should_not be_filled endend
  • USING A STUB - VERBOSEdescribe Order do it "fills the order with enough items in warehouse" do @warehouse.stub!(:quantity_at).and_return(50) @warehouse.stub!(:set_quantity_at).and_return(true) order = Order.new(:Cleveland, 50) order.fill(@warehouse) order.should be_filled end it "does not fill the order with not enough items" do @warehouse.stub!(:quantity_at).and_return(50) @warehouse.stub!(:set_quantity_at).and_return(true) order = Order.new(:Cleveland, 51) order.fill(@warehouse) order.should_not be_filled endend
  • describe Order do before(:each) do @warehouse = stub(warehouse_stub) setup_stub end it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) order.fill(@warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) order.fill(@warehouse) order.should_not be_filled endenddef setup_stub @warehouse.stub!(:quantity_at).and_return(50) @warehouse.stub!(:set_quantity_at).and_return(true)end
  • HOW COOL IS THAT?describe Order do before(:each) do @warehouse = stub(warehouse_stub, {:quantity_at => 50, :set_quantity_at => true}) end it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) order.fill(@warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) order.fill(@warehouse) order.should_not be_filled endend
  • CAN WE DO THE SAME?describe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should_not be_filled endend
  • OH, SNAP!describe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should_not be_filled endend
  • BUT THIS WORKS!describe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_not_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should_not be_filled endend
  • BUT THIS WORKS!describe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should be_filled end it "does not fill the order with not enough items" do order = Order.new(:Cleveland, 51) warehouse = mock(:warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_not_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) order.should_not be_filled endend
  • SO WHAT’S THE DIFFERENCE?
  • NO, SERIOUSLY!• Stubs - Provide canned responses to messages• Mocks - Specify AND verify that certain messages should be received
  • MOCKS CAN DO MORE!Expecting Messages: my_mock.should_receive(:msg).once my_mock.should_not_receive(:msg).onceExpecting Arguments: my_mock.should_receive(:msg).with(1, 2) my_mock.should_receive(:msg).with(no_args()) my_mock.should_receive(:msg).with(any_args())Argument Constraints: my_mock.should_receive(:msg).with(1, an_instance_of(Fixnum), “b”) my_mock.should_receive(:msg).with(a, boolean(), b) my_mock.should_receive(:msg).with(/abc/)Receive Counts: my_mock.should_receive(:msg).once my_mock.should_receive(:msg).twice my_mock.should_receive(:msg).at_least(:once) my_mock.should_receive(:msg).at_most(:twice) my_mock.should_receive(:msg).any_number_of_timesOrdering: my_mock.should_receive(:one).ordered my_mock.should_receive(:two).ordered
  • MOCKS CAN DO MOREdescribe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse)warehouse.should_receive(:quantity_at).ordered.with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).ordered.with(:Cleveland, 0) order.fill(warehouse) order.should be_filled endend
  • MOCKS CAN DO MOREdescribe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse)warehouse.should_receive(:quantity_at).ordered.with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).ordered.with(:Cleveland, 0) order.fill(warehouse) order.should be_filled endend
  • CHANGE THE ORDERdescribe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:set_quantity_at).ordered.with(:Cleveland, 0) warehouse.should_receive(:quantity_at).ordered .with(:Cleveland).and_return(50) order.fill(warehouse) order.should be_filled endend
  • OH, SNAP!describe Order do it "fills the order with enough items in warehouse" do order = Order.new(:Cleveland, 50) warehouse = mock(:warehouse) warehouse.should_receive(:set_quantity_at).ordered.with(:Cleveland, 0) warehouse.should_receive(:quantity_at).ordered .with(:Cleveland).and_return(50) order.fill(warehouse) order.should be_filled endend
  • I USED RSPEC MOCKING... ... but there are other libraries:• Flexmock• Mocha• rr
  • FLEXMOCK - TEST/UNITclass TestOrder < Test::Unit::TestCase def test_fills_order_with_enough_items_at_location order = Order.new(:Cleveland, 50) warehouse = flexmock(warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) assert order.filled? end def test_does_not_fill_order_with_not_enough_items_at_location order = Order.new(:Cleveland, 51) warehouse = flexmock(warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) order.fill(warehouse) assert order.filled? == false endend
  • FLEXMOCK - TEST/UNITclass TestOrder < Test::Unit::TestCase def test_fills_order_with_enough_items_at_location order = Order.new(:Cleveland, 50) warehouse = flexmock(warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) warehouse.should_receive(:set_quantity_at).with(:Cleveland, 0) order.fill(warehouse) assert order.filled? end def test_does_not_fill_order_with_not_enough_items_at_location order = Order.new(:Cleveland, 51) warehouse = flexmock(warehouse) warehouse.should_receive(:quantity_at).with(:Cleveland).and_return(50) order.fill(warehouse) assert order.filled? == false endend
  • WHEN TO USE MOCKS/STUBS?• Using an external service• Verifying a protocol• Objects are complicated to create
  • WHEN TO USE MOCKS/STUBS?• Using an external service• Verifying a protocol• Objects are complicated to create
  • BUT HOWCAN I USE THIS?
  • MEET TWEETSTATS
  • TWITTER OAUTH
  • LOGGED ON
  • SEND TWEET
  • VIEW FRIENDS
  • VIEW FOLLOWERS
  • ENTER THE SERVICEModel ControllerView OauthService
  • THE USERS CONTROLLERclass UsersController < ApplicationController before_filter :oauth_login_required, :except => [ :callback, :signout, :index ] def new end...private def oauth_login_required logged_in? || login_by_oauth end... def login_by_oauth redirect_to self.oauth_service.login_by_oauth rescue Exception => e # The user might have rejected this application flash[:error] = "Twitter API failure (account login)" redirect_to root_url endend
  • STUB IT OUT!describe UsersController do TEST_URL = twitter_test describe "GET new" do it "redirects to twitter - stub example" do # see the extra message stub, I dont need “logged_in?” oauth_service_stub = stub(:login_by_oauth => TEST_URL, :logged_in? => false) controller .stub!(:oauth_service) .and_return(oauth_service_stub) get new response.should redirect_to TEST_URL end endend
  • STUB IT OUT!describe UsersController do TEST_URL = twitter_test describe "GET new" do it "redirects to twitter - stub example" do # see the extra message stub, I dont need “logged_in?” oauth_service_stub = stub(:login_by_oauth => TEST_URL, :logged_in? => false) controller .stub!(:oauth_service) .and_return(oauth_service_stub) get new response.should redirect_to TWITTER_TEST_URL end endend
  • OR MOCK ITdescribe UsersController do TEST_URL = twitter_test.com describe "GET new" do it "redirects to twitter - stub example" do ... end it "redirects to twitter - mock example" do oauth_service_mock = mock(:oauth_service) oauth_service_mock .should_receive(:login_by_oauth) .and_return(TEST_URL) controller .stub!(:oauth_service).and_return(oauth_service_mock) get new response.should redirect_to TEST_URL end endend
  • OR MOCK ITdescribe UsersController do TEST_URL = twitter_test.com describe "GET new" do it "redirects to twitter - stub example" do ... end it "redirects to twitter - mock example" do oauth_service_mock = mock(:oauth_service) oauth_service_mock .should_receive(:login_by_oauth) .and_return(TEST_URL) controller .stub!(:oauth_service).and_return(oauth_service_mock) get new response.should redirect_to TEST_URL end endend
  • BUT WHAT’S THE DIFFERENCE? Unused Message Stuboauth_service_stub = stub(:login_by_oauth => TEST_URL, :logged_in? => false) oauth_service_mock = mock(:oauth_service) oauth_service_mock .should_receive(:login_by_oauth) .and_return(TEST_URL)
  • THIS WILL FAILit "redirects to twitter - mock example" do oauth_service_mock = mock(:oauth_service) oauth_service_mock .should_receive(:login_by_oauth) .and_return(TEST_URL) oauth_service_mock .should_receive(:logged_in) .and_return(false) controller .stub!(:oauth_service).and_return(oauth_service_mock) get new response.should redirect_to TEST_URLend
  • OH, SNAP!it "redirects to twitter - mock example" do oauth_service_mock = mock(:oauth_service) oauth_service_mock .should_receive(:login_by_oauth) .and_return(TEST_URL) oauth_service_mock .should_receive(:logged_in) .and_return(false) controller .stub!(:oauth_service).and_return(oauth_service_mock) get new response.should redirect_to TEST_URLend
  • OK, I AM AUTHENTICATEDdef callback user_info = self.oauth_service.get_user_info(params[:oauth_verifier]) @user = User.save_user!({ :twitter_id => user_info[id], :screen_name => user_info[screen_name], :token => self.oauth_service.access_token, :secret => self.oauth_service.access_secret, :profile_image_url => user_info[profile_image_url] }) self.current_user = @user redirect_to @user # Redirect to the show pagerescue Exception => e # The user might have rejected this application. flash[:error] = "Twitter API failure (account login)" redirect_to root_urlend
  • A NEGATIVE TESTdescribe "Get callback" do it "redirects to the home page when error occurs in service" do # I can use a mock oauth_service = mock(:oauth_service) oauth_service.should_receive(:get_user_info) .once .with(xyz) .and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • A NEGATIVE TESTdescribe "Get callback" do it "redirects to the home page when error occurs in service" do # I can use a mock oauth_service = mock(:oauth_service) oauth_service.should_receive(:get_user_info) .once .with(xyz) .and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • OR EXPECT IT TWICEdescribe "Get callback" do it "redirects to the home page when error occurs in service" do # I can use a mock oauth_service = mock(:oauth_service) oauth_service.should_receive(:get_user_info) .twice .with(xyz) .and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • OH, SNAP!describe "Get callback" do it "redirects to the home page when error occurs in service" do # I can use a mock oauth_service = mock(:oauth_service) oauth_service.should_receive(:get_user_info) .twice .with(xyz) .and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • A STUB WILL BE FINEdescribe "Get callback" do it "redirects to the home page when error occurs in service" do # I could use a mock # oauth_service = mock(:oauth_service) # oauth_service.should_receive(:get_user_info) .once.with(xyz) .and_raise(StandardError) # but a stub will exercise the code just fine oauth_service = stub(:oauth_service) oauth_service.stub!(:get_user_info).and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • A STUB WILL BE FINEdescribe "Get callback" do it "redirects to the home page when error occurs in service" do # I could use a mock # oauth_service = mock(:oauth_service) # oauth_service.should_receive(:get_user_info) .once.with(xyz) .and_raise(StandardError) # but a stub will exercise the code just fine oauth_service = stub(:oauth_service) oauth_service.stub!(:get_user_info).and_raise(StandardError) controller.stub!(:oauth_service).and_return(oauth_service) # Exercise get callback, {:oauth_verifier => xyz} flash[:error].should == Twitter API failure (account login) response.should redirect_to root_url endend
  • A POSITIVE TESTit "redirects to the users page after a successful login" do user_info_stub = {id => 123456, screen_name => johndoe, profile_image_url => some_image_url} oauth_service_mock = mock(OauthService, :access_token => 12345, :access_secret => abcde) fake oauth_service_mock.should_receive(:get_user_info) .with(xyz) .and_return(user_info_stub) stub user = User.new dummy mock User.stub!(:save_user!).and_return(user) controller.stub!(:oauth_service).and_return(oauth_service_mock) # Exercise get callback, {:oauth_verifier => xyz} controller.current_user.should == user response.should redirect_to userend
  • A POSITIVE TESTit "redirects to the users page after a successful login" do user_info_stub = {id => 123456, screen_name => johndoe, profile_image_url => some_image_url} oauth_service_mock = mock(OauthService, :access_token => 12345, :access_secret => abcde) fake oauth_service_mock.should_receive(:get_user_info) .with(xyz) .and_return(user_info_stub) stub user = User.new(:id => 122) dummy mock User.stub!(:save_user!).and_return(user) controller.stub!(:oauth_service).and_return(oauth_service_mock) # Exercise get callback, {:oauth_verifier => xyz} controller.current_user.should == user response.should redirect_to userend
  • CHECK THE ARGUMENTit "redirects to the users page after a successful login" do user_info_stub = {id => 123456, screen_name => johndoe, profile_image_url => some_image_url} oauth_service_mock = mock(OauthService, :access_token => 12345, :access_secret => abcde) oauth_service_mock.should_receive(:get_user_info) .with(abc) .and_return(user_info_stub) user = User.new(:id => 122) User.stub!(:save_user!).and_return(user) controller.stub!(:oauth_service).and_return(oauth_service_mock) # Exercise get callback, {:oauth_verifier => xyz} controller.current_user.should == user response.should redirect_to userend
  • OH, SNAP!it "redirects to the users page after a successful login" do user_info_stub = {id => 123456, screen_name => johndoe, profile_image_url => some_image_url} oauth_service_mock = mock(OauthService, :access_token => 12345, :access_secret => abcde) oauth_service_mock.should_receive(:get_user_info) .with(abc) .and_return(user_info_stub) user = User.new(:id => 122) User.stub!(:save_user!).and_return(user) controller.stub!(:oauth_service).and_return(oauth_service_mock) # Exercise get callback, {:oauth_verifier => xyz} controller.current_user.should == user response.should redirect_to userend
  • AND THE CUCUMBER?
  • USER LOGIN FEATUREFeature: Login Users In order to access the full site As a user I want to log on to the site with my twitter account Scenario: Login via Twitter Given I am on the home page When I follow "sign_in_with_twitter" And Twitter authorizes me Then I should see "Welcome back!"
  • USER LOGIN FEATUREFeature: Login Users In order to access the full site As a user I want to log on to the site with my twitter account Scenario: Login via Twitter Given I am on the home page When I follow "sign_in_with_twitter" And Twitter authorizes me Then I should see "Welcome back!"
  • WHAT OPTIONS DO I HAVE?• Hit Twitter every time• Stub out my service object• Stub out HTTP
  • WHAT OPTIONS DO I HAVE?• Hit Twitter every time• Stub out my service object• Stub out HTTP Run test offline mode
  • USER_LOGIN_STEPS.RB Scenario: Login via Twitter Given I am on the home page When I follow "sign_in_with_twitter" And Twitter authorizes me Then I should see "Welcome back!" When /^Twitter authorizes me$/ do visit callback_users_path end
  • ENV.RBrequire fakewebFakeWeb.register_uri(:get, http://twitter.com/oauth/authorize?oauth_token=fake, :body => success)FakeWeb.register_uri(:post, http://twitter.com/oauth/access_token, :body => oauth_token=fake&oauth_token_secret=fake)FakeWeb.register_uri(:get, http://twitter.com/account/verify_credentials.json, :response => File.join(::Rails.root.to_s, features, fixtures, verify_credentials.json))
  • NEVER GO OUTSIDE!require fakewebFakeWeb.allow_net_connect = falseFakeWeb.register_uri(:get, http://twitter.com/oauth/authorize?oauth_token=fake, :body => success)FakeWeb.register_uri(:post, http://twitter.com/oauth/access_token, :body => oauth_token=fake&oauth_token_secret=fake)FakeWeb.register_uri(:get, http://twitter.com/account/verify_credentials.json, :response => File.join(::Rails.root.to_s, features, fixtures, verify_credentials.json))
  • VIEW FRIENDS
  • FRIENDS FEATUREFeature: View Friends In order to see who my friends are As a user I want to go to the friends page and see my friends Background: Given I am logged on to the site Scenario: View friends When I follow "friends" Then I should see "You are following 4 friends."
  • THE STEPSGiven /^I am logged on to the site$/ do visit callback_users_path view_friends_steps.rbend $: curl -is http://twitter.com/statuses/friends.json? screen_name=adomokos&cursor=-1 > features/fixtures/friends.jsonFakeWeb.register_uri(:get, http://twitter.com/statuses/friends.json?screen_name=adomokos&cursor=-1, :response => File.join(::Rails.root.to_s, features, fixtures, friends.json)) env.rb
  • CHUCK NORRIS IS HAPPY!
  • RECAP• Stubs are “Yugos”, Mocks are “Aston Martins”• Stubs are canned answered to messages• Mocks can verify: • If message was sent or not • Input parameters • Order of message• Never go outside of your “box” during automated tests!
  • MOCKS CAN DO MORE!Expecting Messages: my_mock.should_receive(:msg).once my_mock.should_not_receive(:msg).onceExpecting Arguments: my_mock.should_receive(:msg).with(1, 2) my_mock.should_receive(:msg).with(no_args()) my_mock.should_receive(:msg).with(any_args())Argument Constraints: my_mock.should_receive(:msg).with(1, an_instance_of(Fixnum), “b”) my_mock.should_receive(:msg).with(a, boolean(), b) my_mock.should_receive(:msg).with(/abc/)Receive Counts: my_mock.should_receive(:msg).once my_mock.should_receive(:msg).twice my_mock.should_receive(:msg).at_least(:once) my_mock.should_receive(:msg).at_most(:twice) my_mock.should_receive(:msg).any_number_of_timesOrdering: my_mock.should_receive(:one).ordered my_mock.should_receive(:two).ordered
  • THANK YOU!
  • REFERENCES• http://keremerkan.net/qr-code-and-2d-code-generator• http://martinfowler.com/articles/mocksArentStubs.html• http://blog.zerosum.org/2009/7/13/twitter-auth-integration- testing• http://dev.twitter.com/console• http://rspec.info/documentation/mocks/ message_expectations.html