1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/bin/env ruby
#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++
require 'test_helper'
# Sample FlexMock Usage.
class TestSamples < Minitest::Test
include FlexMock::Minitest
# This is a basic example where we setup a mock object to mimic an
# IO object. We know that the +count_lines+ method uses gets, so we
# tell the mock object to handle +gets+ by returning successive
# elements of an array (just as the real +gets+ returns successive
# elements of a file.
def test_file_io
mock_file = flexmock("file")
mock_file.should_receive(:gets).and_return("line 1", "line 2", nil)
assert_equal 2, count_lines(mock_file)
end
# Count the number of lines in a file. Used in the test_file_io
# test.
def count_lines(file)
n = 0
while file.gets
n += 1
end
n
end
end
class TestUndefined < Minitest::Test
include FlexMock::Minitest
def test_undefined_values
m = flexmock("mock")
m.should_receive(:divide_by).with(0).
and_return_undefined
assert_equal FlexMock.undefined, m.divide_by(0)
end
end
class TestSimple < Minitest::Test
include FlexMock::Minitest
def test_simple_mock
m = flexmock(:pi => 3.1416, :e => 2.71)
assert_equal 3.1416, m.pi
assert_equal 2.71, m.e
end
end
class TestDog < Minitest::Test
include FlexMock::Minitest
def test_dog_wags
tail_mock = flexmock(:wag => :happy)
assert_equal :happy, tail_mock.wag
end
end
class Woofer
end
class Dog
def initialize
@woofer = Woofer.new
end
def bark
@woofer.woof
end
def wag
:happy
end
end
class TestDogBarking < Minitest::Test
include FlexMock::Minitest
# Setup the tests by mocking the +new+ method of
# Woofer and return a mock woofer.
def setup
@dog = Dog.new
flexmock(@dog, :bark => :grrr)
end
def test_dog
assert_equal :grrr, @dog.bark # Mocked Method
assert_equal :happy, @dog.wag # Normal Method
end
end
class TestDogBarkingWithNewInstances < Minitest::Test
include FlexMock::Minitest
# Setup the tests by mocking Woofer to always
# return partial mocks.
def setup
flexmock(Woofer).new_instances.should_receive(:woof => :grrr)
end
def test_dog
assert_equal :grrr, Dog.new.bark # All dog objects
assert_equal :grrr, Dog.new.bark # are mocked.
end
end
class TestDefaults < Minitest::Test
include FlexMock::Minitest
def setup
@mock_dog = flexmock("Fido")
@mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
end
def test_something_where_bark_must_be_called_once
@mock_dog.should_receive(:bark => "bow wow").once
assert_equal "bow wow", @mock_dog.bark
assert_equal :a_tail, @mock_dog.tail
end
end
class TestDemeter < Minitest::Test
include FlexMock::Minitest
def test_manual_mocking
# Manually mocking a Law of Demeter violation
cog = flexmock("cog")
cog.should_receive(:turn).once.and_return(:ok)
joint = flexmock("gear", :cog => cog)
axle = flexmock("axle", :universal_joint => joint)
chassis = flexmock("chassis", :axle => axle)
car = flexmock("car", :chassis => chassis)
# test code
assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
end
def test_demeter
car = flexmock("car")
car.should_receive( "chassis.axle.universal_joint.cog.turn" => :ok).once
# Test code
assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
end
end
class TestDb < Minitest::Test
include FlexMock::Minitest
def test_db
db = flexmock('db')
db.should_receive(:query).and_return([1,2,3])
db.should_receive(:update).with(5).and_return(nil).once
# test code
assert_nil db.update(5)
end
end
class TestDb < Minitest::Test
include FlexMock::Minitest
def test_query_and_update
db = flexmock('db')
db.should_receive(:query).and_return([1,2,3]).ordered
db.should_receive(:update).and_return(nil).ordered
# test code here
assert_raises(check_failed_error) do
db.update
db.query
end
end
def test_ordered_queries
db = flexmock('db')
db.should_receive(:startup).once.ordered
db.should_receive(:query).with("CPWR").and_return(12.3).
once.ordered(:queries)
db.should_receive(:query).with("MSFT").and_return(10.0).
once.ordered(:queries)
db.should_receive(:query).with(/^....$/).and_return(3.3).
at_least.once.ordered(:queries)
db.should_receive(:finish).once.ordered
# test code here
db.startup
db.query("CPWR")
db.query("MSFT")
db.query("asdf")
db.finish
end
def test_ordered_queries_in_record_mode
db = flexmock('db')
db.should_expect do |rec|
rec.startup.once.ordered
rec.query("CPWR") { 12.3 }.once.ordered(:queries)
rec.query("MSFT") { 10.0 }.once.ordered(:queries)
rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
rec.finish.once.ordered
end
# test code here using +db+.
db.startup
db.query("CPWR")
db.query("MSFT")
db.query("asdf")
db.finish
end
def known_good_way_to_build_xml(builder)
builder.html
end
def new_way_to_build_xml(builder)
known_good_way_to_build_xml(builder)
end
def test_build_xml
builder = flexmock('builder')
builder.should_expect do |rec|
rec.should_be_strict
known_good_way_to_build_xml(rec) # record the messages
end
new_way_to_build_xml(builder) # compare to new way
end
end
class TestMoreSamples < Minitest::Test
include FlexMock::Minitest
def test_multiple_gets
file = flexmock('file')
file.should_receive(:gets).with_no_args.
and_return("line 1\n", "line 2\n")
# test code here
assert_equal "line 1\n", file.gets
assert_equal "line 2\n", file.gets
end
def test_an_important_message
m = flexmock('m')
m.should_receive(:an_important_message).and_return(1).once
m.should_ignore_missing
# test code here
assert_equal 1, m.an_important_message
assert_equal FlexMock.undefined, m.other
end
class QuoteService
end
class Portfolio
def initialize
@quote_service = QuoteService.new
end
def value
@quote_service.quote
end
end
def test_portfolio_value
flexmock(QuoteService).new_instances do |m|
m.should_receive(:quote).and_return(100)
end
port = Portfolio.new
value = port.value # Portfolio calls QuoteService.quote
assert_equal 100, value
end
end
|