Sha256: 1f8b2149ec849ccba2058692fe5e691abbe7463dd51ebde6d3ea3930ff689335

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

class MockObject
  def initialize(name, options={})
    @name = name
    @null = options[:null_object]
  end

  def method_missing(sym, *args, &block)
    @null ? self : super
  end
end

class MockProxy
  def initialize(type=nil)
    @multiple_returns = nil
    @returning = nil
    @yielding  = []
    @arguments = :any_args
    @type      = type || :mock
  end

  def mock?
    @type == :mock
  end

  def stub?
    @type == :stub
  end

  def count
    @count ||= mock? ? [:exactly, 1] : [:any_number_of_times, 0]
  end

  def arguments
    @arguments
  end

  def returning
    if @multiple_returns
      if @returning.size == 1
        @multiple_returns = false
        return @returning = @returning.shift
      end
      return @returning.shift
    end
    @returning
  end

  def times
    self
  end

  def calls
    @calls ||= 0
  end

  def called
    @calls = calls + 1
  end

  def exactly(n)
    @count = [:exactly, n_times(n)]
    self
  end

  def at_least(n)
    @count = [:at_least, n_times(n)]
    self
  end

  def at_most(n)
    @count = [:at_most, n_times(n)]
    self
  end

  def once
    exactly 1
  end

  def twice
    exactly 2
  end

  def any_number_of_times
    @count = [:any_number_of_times, 0]
    self
  end

  def with(*args)
    raise ArgumentError, "you must specify the expected arguments" if args.empty?
    @arguments = *args
    self
  end

  def and_return(*args)
    case args.size
    when 0
      @returning = nil
    when 1
      @returning = args[0]
    else
      @multiple_returns = true
      @returning = args
      count[1] = args.size if count[1] < args.size
    end
    self
  end

  def and_yield(*args)
    @yielding << args
    self
  end
  
  def yielding
    @yielding
  end
  
  def yielding?
    !@yielding.empty?
  end

  private

  def n_times(n)
    case n
    when :once
      1
    when :twice
      2
    else
      Integer n
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mspec-1.2.0 lib/mspec/mocks/proxy.rb
mspec-1.4.0 lib/mspec/mocks/proxy.rb
mspec-1.1.1 lib/mspec/mocks/proxy.rb
mspec-1.1.0 lib/mspec/mocks/proxy.rb
mspec-1.3.0 lib/mspec/mocks/proxy.rb
mspec-1.3.1 lib/mspec/mocks/proxy.rb
mspec-1.5.0 lib/mspec/mocks/proxy.rb