Flexmock is a more popular mock tool for http://www.aliyun.com/zixun/aggregation/13726.html ">rails unit tests,
Installation method: $ gem Install Flexmock
code example:
Require ' test/unit '
Require ' flexmock/test_unit '
Class Temperaturesampler
Def initialize (sensor)
@sensor = Sensor
End
def average_temp
Total = (0...3). Collect {
@sensor. read_temperature
}.inject {|i, s| i + s}
total/3.0
End
End
Class Testtemperaturesampler < Test::unit::testcase
def test_sensor_can_average_three_temperature_readings
Sensor = Flexmock ("temp")
Sensor.should_receive (: Read_temperature). Times (3).
And_return (10, 12, 14)
Sampler = temperaturesampler.new (sensor)
Assert_equal, Sampler.average_temp
End
End
Test::unit Integration Sample Code
Require ' flexmock/test_unit '
Class Testdog < Test::unit::testcase
def test_dog_wags
Tail_mock = Flexmock (: Wag =>: Happy)
Assert_equal:happy, Tail_mock.wag
End
End
RSPEC Integration Sample Code
Spec::runner.configure do |config|
Config.mock_with:flexmock
End
Describe ' Using flexmock with RSpec ' do
It ' should be Inc. to create a mock ' do
m = Flexmock (: Foo =>: Bar)
M.foo.should = =: Bar
End
End