Chef:Powerful Infrastructure Automation
上QQ阅读APP看书,第一时间看更新

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