     @_replaceMethodOnObject()
 
   call: ->
-    call = new Spec.MethodStub.PossibleCall()
+    call = new Spec.MethodStub.PossibleCall(this)
     @possibleCalls.push call
     call
 

  _stubMethod: ->
    stubMethod = (args...) =>
      if call = @_findPossibleCall(args)
        result = call.call args
        if result is Spec.MethodStub.PossibleCall.PASSTHROUGH
          @original args
        else
          result
      else if @possibleCalls.length
        Spec.fail "expected ##{@method} to be called#{@possibleCalls[0].argumentsString()}, actual arguments: &ldquo;#{args.join ', '}&rdquo;"
        null

    stubMethod._stub = this
    stubMethod

  _findPossibleCall: (args) ->
    for call in @possibleCalls
      if call.matchesArguments(arguments)
        return call
    false

  _replaceMethodOnObject: ->
    @object[@method] = @_stubMethod()

class window.Spec.MethodStub.PossibleCall
  PASSTHROUGH: {}

  with: (args...) ->
    @arguments = args
    this

  andReturn: (returnValue) ->
    @return = returnValue
    this

  andPassthrough: ->
    @return = Spec.MethodStub.PossibleCall.PASSTHROUGH
    this

  expect: ->
    # TODO: Make this description better, possibly with name of

    else
      true

  call: (args) ->
    if @matchesArguments(args)
      @expectation.meet() if expectation
      @return.apply @methodStub.object, args if @return
    else
      @_failOnInvalidArguments args
      null
  
  _failOnInvalidArguments: (args) ->
    Spec.fail "expected ##{@method} to be called#{@_argumentsString()}, actual arguments: &ldquo;#{args.join ', '}&rdquo;"

  _argumentsString: ->
    if @arguments
      " with arguments &ldquo;#{@arguments.join ', '}&rdquo;"
    else
      ''
