Created
February 10, 2010 20:00
-
-
Save kyanny/300779 to your computer and use it in GitHub Desktop.
Ruby mocha test sample code
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
# -*- coding: utf-8 -*- | |
require 'test/unit' | |
require 'rubygems' | |
require 'mocha' | |
class Rubyist | |
attr_accessor :boss | |
def say(message = "love Ruby") | |
"I #{message}" | |
end | |
end | |
class Boss | |
attr_accessor :name | |
end | |
class TestRubyist < Test::Unit::TestCase | |
def setup | |
@rubyist = Rubyist.new | |
boss = Boss.new | |
boss.name = "Larry Wall" | |
@rubyist.boss = boss | |
end | |
def test_love_perl? # Larry の前ではおべっかを使う | |
if @rubyist.boss.name == "Larry Wall" | |
@rubyist.expects(:say).with("love Perl").returns("I love Perl") | |
assert_equal @rubyist.say("love Perl"), "I love Perl" # 引数が "love Perl" じゃなかったら fail | |
end | |
end | |
end |
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
# -*- coding: utf-8 -*- | |
require 'test/unit' | |
require 'rubygems' | |
require 'mocha' | |
class Rubyist | |
attr_accessor :boss | |
def say | |
"I love Ruby" | |
end | |
end | |
class Boss | |
attr_accessor :name | |
end | |
class TestRubyist < Test::Unit::TestCase | |
def setup | |
@rubyist = Rubyist.new | |
boss = Boss.new | |
boss.name = "Larry Wall" | |
@rubyist.boss = boss | |
end | |
def test_love_perl? # Larry の前ではおべっかをつかう | |
if @rubyist.boss.name == "Larry Wall" | |
@rubyist.stubs(:say).returns("I love Perl") | |
assert_equal @rubyist.say, "I love Perl" | |
end | |
end | |
end |
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
# -*- coding: utf-8 -*- | |
require 'test/unit' | |
require 'rubygems' | |
require 'mocha' | |
require 'xmlrpc/client' | |
module Sasimi | |
class Blog | |
def self.api | |
'http://b.hatena.ne.jp/xmlrpc' | |
end | |
def self.request(method, url) | |
client = XMLRPC::Client.new2(self.api) | |
ok, result = client.call2(method, url) | |
"やたー#{result}ブクマいったよー" | |
end | |
end | |
end | |
class TestSasimiBlog < Test::Unit::TestCase | |
def test_request_get_total_count | |
o = mock() | |
o.expects(:call2).with('bookmark.getTotalCount', 'http://d.hatena.ne.jp/a666666/').returns([true, 100*100*100]) | |
XMLRPC::Client.expects(:new2).with('http://b.hatena.ne.jp/xmlrpc').returns(o) | |
assert_equal Sasimi::Blog.request('bookmark.getTotalCount', 'http://d.hatena.ne.jp/a666666/'), "やたー1000000ブクマいったよー" | |
end | |
end | |
# handle response content_type | |
module XMLRPC::ParseContentType | |
def parse_content_type(str) | |
a, *b = str.split(";") | |
a = "text/xml" if a == "application/xml" | |
return a.strip.downcase, *b | |
end | |
end | |
#p Sasimi::Blog.request('bookmark.getTotalCount', 'http://d.hatena.ne.jp/a666666/') | |
#=> [true, 2826] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment