Awesome code of the day (soon in Spring4D):

Awesome code of the day (soon in Spring4D):

  myMock.Setup
    .Returns(42)
    .When(Args[1] = 'hello')
    .SomeProcedure(0, '', nil);

Comments

  1. Mason Wheeler If you used any mocking library in Delphi you would know. None (that I know of) can do argument matching (except writing your own code in the execute delegate that you can specify in most libs). So in this case the mock will be good whenever it is called having the string parameter contain 'hello' regardless the other arguments.

    ReplyDelete
  2. Meh.  I always figured mockery is something that belongs in interpersonal conversation, particularly before an audience, not code. ;)

    ReplyDelete
  3. Stefan Glienke​ mORMot allows mocking with simple parameter matching since years. See http://synopse.info/files/html/api-1.18/mORMot.html#TINTERFACESTUB_RETURNS
    But your syntax is nice.

    ReplyDelete
  4. A. Bouchez Hm, I don't see which of your overloads lets you define a partial match for the method parameters. Match all or none have been in DSharp mocks and I think Delphi Mocks also supports it.

    ReplyDelete
  5. Delphi Mocks will have this soon, we have been working on it. The biggest issue we had was the order of parameter passing is not defined in delphi (64bit seems to be consistent, 32bit not).. so it's not as neat as we had hope. - eg:
    mockCredit.Setup
    .WillReturn(6)
    .When
    .TakesTwoParams(It(0).IsEqualTo(1), It(1).IsEqualTo(true));

    See the f-param-matches branch on github.

    ReplyDelete
  6. BTW, we had hope to just use It.XXX without the param index but the parameter passing order forced use to provide the index.

    ReplyDelete
  7. Just joining in on David Heffernan's pissing contest ;)

    ReplyDelete
  8. Vincent Parrett  Parameter passing orders are pretty clear. What's not defined is the order of evaluation of arguments. That's never been defined.

    ReplyDelete
  9. Vincent Parrett Personally I am not sure if this Moq inspired API works so well in Delphi because of the poor type inference.

    What I like about this way though is that you don't have to pass any dummy values that make the statement look ambiguous. With the way I took though you can write statements like this (if that is ever useful is another story ^^):

    When((Args[0] = 1) or (Args[1] = 'Hello'))

    Maybe I can make them both work. Implementing the It thing should be easy enough ;p

    Also not a pissing contest at all imo, competition and discussing ideas is a good thing.

    ReplyDelete
  10. I guess I'll have to take a look a look at your implementation, it's not clear from your example, where does Args[] come from?  

    Ours is not as clean as we had hoped because the delphi language is lacking so many features that moq relies on.

    ReplyDelete
  11. David Heffernan The order issue is actually in the order of the Args array passed in TInterceptBeforeNotify and TVirtualInterfaceInvokeEvent not being consistent on 32bit.

    ReplyDelete
  12. Actually David Heffernan  you are correct it's the order of evaluation that was causing us problems (Jason just reminded me).

    ReplyDelete
  13. Stefan Glienke Allow me being off-topic, but I've just recently explored the awesomeness of your excellent DSharp :)

    ReplyDelete
  14. I'm glad to see that it knows the correct answer but what was the question again? :-)

    ReplyDelete
  15. Vincent Parrett Args is a const of a record type that has a default array property.

    ReplyDelete

Post a Comment