Created
January 15, 2015 16:50
-
-
Save wolftatsu/ee94ad15323ba13daea4 to your computer and use it in GitHub Desktop.
mocha(mocking lib for Ruby) sample
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
require "rubygems" | |
require "test/unit" | |
require "mocha/test_unit" | |
require "pry" | |
class Person | |
attr_accessor :name | |
def initialize(name) | |
self.name = name | |
end | |
def say | |
return "Hello #{name}" | |
end | |
def like(obj) | |
return "I like #{obj}" | |
end | |
end | |
class PersonTest < Test::Unit::TestCase | |
attr_accessor :me | |
def setup | |
@me = Person.new("me") | |
end | |
def test_say_nomal | |
me = Person.new("me") | |
you = Person.new("you") | |
assert_equal "Hello me", me.say | |
assert_equal "Hello you", you.say | |
end | |
def test_mocking_for_instance_method | |
assert @me | |
@me.expects(:say).returns("Hello hoge") | |
# binding.pry | |
assert_equal "Hello hoge", @me.say | |
end | |
def test_mocking_with_param | |
assert_equal "I like you", @me.like('you') | |
@me.expects(:like).with('him').returns("I don't like him") | |
assert_equal "I don't like him", @me.like('him') | |
end | |
def test_create_mock_object | |
obj = mock('object') | |
obj.expects(:hoge).with(:p1).returns('got p1') | |
assert_equal 'got p1', obj.hoge(:p1) | |
obj.expects(:hoge).with(:p2).returns('got p2') | |
assert_equal 'got p2', obj.hoge(:p2) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment