欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

expectations——轻量级的单元测试框架

程序员文章站 2024-01-06 22:38:04
...

项目主页:http://expectations.rubyforge.org/

安装的话:gem install  expectations

它将测试分为state based和behavior based,语法很DSL,作者写了篇文章《implementing internal dsl in ruby

例子,

 

state base test:

        # State based expectation where a value equals another value
          expect 2 do
            1 + 1
          end 

          expect /a string/ do
            "a string"
          end

          # State based test checking if actual is in the expected Range
          expect 1..5 do
            3
          end

          # State based test to determine if the object is an instance of the module
          expect Enumerable do
            []
          end

          # State based test to determine if the object is an instance of the class
          expect String do
            "a string"
          end

          # State based test to determine if the modules are the same
          expect Enumerable do
            Enumerable
          end
  

 behavior based test:

         # Behavior based test using a traditional mock
          expect mock.to_receive(:dial).with("2125551212").times(2) do |phone|
            phone.dial("2125551212")
            phone.dial("2125551212")
          end

          # Behavior based test on a concrete mock
          expect Object.to_receive(:deal) do
            Object.deal
          end