官术网_书友最值得收藏!

RSpec

RSpec is a framework to test Ruby code that allows you to use a domain-specific language to provide tests, much in the same way Chef provides a domain-specific language to manipulate an infrastructure. Instead of using a DSL to manage systems, RSpec's DSL provides a number of components to express the expectations of code and simulate the execution of portions of the system (also known as mocking).

The following examples in RSpec should give you a high-level idea of what RSpec can do:

# simple expectation
it 'should add 2 and 2 together' do 
  x = 2 + 2
  expect(x).to eq 4
end

# Ensure any instance of Object receives a call to 'foo' 
# and return a pre-defined value (mocking)
it 'verifies that an instance receives :foo' do 
  expect_any_instance_of(Object)
    .to receive(:foo).and_return(:return_value)
      
  o = Object.new       
  expect(o.foo).to eq(:return_value)     
end

# Deep expectations (i.e client makes an HTTP call somewhere
# inside it, make sure it happens as expected)
it 'should make an authorized HTTP GET call' do 
  expect_any_instance_of(Net::HTTP::Get)
    .to receive(:basic_auth)
  @client.make_http_call
end
主站蜘蛛池模板: 囊谦县| 天津市| 清丰县| 眉山市| 登封市| 洛浦县| 吉安市| 金秀| 无为县| 临西县| 石泉县| 兰西县| 商丘市| 达孜县| 竹北市| 香港| 乌恰县| 新泰市| 霍城县| 榆社县| 微山县| 嘉禾县| 壤塘县| 龙陵县| 嫩江县| 盖州市| 衢州市| 长兴县| 镇平县| 武平县| 临洮县| 共和县| 泾川县| 祥云县| 新竹市| 小金县| 紫金县| 洪江市| 安丘市| 科尔| 都匀市|