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

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
主站蜘蛛池模板: 大荔县| 西安市| 礼泉县| 河南省| 晴隆县| 锦屏县| 犍为县| 东明县| 桓台县| 泰和县| 武陟县| 凌云县| 博白县| 连州市| 青岛市| 建湖县| 杭锦后旗| 黎城县| 万源市| 武陟县| 宽甸| 改则县| 赣州市| 南平市| 融水| 漯河市| 贡嘎县| 绍兴县| 施秉县| 上栗县| 外汇| 仁化县| 永寿县| 柯坪县| 肇庆市| 龙里县| 墨玉县| 罗城| 攀枝花市| 喀喇沁旗| 灵武市|