Created
December 3, 2010 17:31
-
-
Save kyanny/727254 to your computer and use it in GitHub Desktop.
mocha mock example
This file contains 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
#!/usr/bin/env ruby | |
require 'test/unit' | |
require 'mocha' | |
class User | |
attr_accessor :user_id, :name, :payment_status | |
def initialize(name) | |
@user_id = self.object_id.abs.to_i | |
@name = name | |
end | |
end | |
class Mailer | |
def deliver_all_payment_notify(users) | |
users.each do |user| | |
if user.payment_status == true | |
deliver_payment_success_notify(user) | |
else | |
deliver_payment_fail_notify(user) | |
end | |
end | |
end | |
private | |
def deliver_payment_success_notify(user) | |
end | |
def deliver_payment_fail_notify(user) | |
end | |
end | |
if $0 == __FILE__ | |
eval(DATA.read) | |
end | |
__END__ | |
class MailerTest < Test::Unit::TestCase | |
def test_deliver_all_payment_notice | |
melody = User.new('Melody') | |
melody.payment_status = true | |
nelson = User.new('Nelson') | |
nelson.payment_status = false | |
mailer = Mailer.new | |
mailer.expects(:deliver_payment_success_notify).once.with(melody) | |
mailer.expects(:deliver_payment_fail_notify).once.with(nelson) | |
mailer.deliver_all_payment_notify([melody, nelson]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment