Flexmock v0.9.0.beta.0 publishes mock tools for rails unit tests

Source: Internet
Author: User
Keywords Rails unit tests Flexmock mock tools
Tags aliyun class code example gem html http install

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.