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

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
主站蜘蛛池模板: 比如县| 日照市| 会理县| 嘉兴市| 喀喇沁旗| 龙州县| 陇西县| 上虞市| 墨竹工卡县| 轮台县| 东阳市| 鹤山市| 吉木萨尔县| 驻马店市| 谢通门县| 房山区| 石首市| 峨眉山市| 平利县| 讷河市| 庆元县| 棋牌| 南宫市| 迁安市| 昌吉市| 平罗县| 开江县| 井研县| 德保县| 临湘市| 天门市| 湘潭县| 高青县| 闽清县| 黑山县| 囊谦县| 太原市| 叶城县| 方城县| 乌鲁木齐县| 资阳市|