Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

Consider adding jMock custom actions to provide side effect functionality

State: new
Milestone: none
Assigned to: James Mead James Mead

Activity

  1. Alex
    Alex

    Am I wrong, or is the JMock custom action essentially the same thing as this patch?

    JMock uses 'will', I used 'runs'.

    jmock: oneOf (mangoTree).pickFruit(with(any(Collection.class))); will(addElements(mango1, mango2));

    mocha: mangoTree.expects(:pickfruit).runs{addElements(mango1, mango2)}

    November 11th, 2010 @ 06:28 PM

  2. Alex
    Alex

    To add a use case, I mocked an API and was using some test data (tumblr posts: 5 normal, 4 pics, 3 quotes etc.)

    To check things were saving as they should, I needed to mock 'save' and update an external counter every time it was called.

    November 11th, 2010 @ 06:30 PM

  3. eydaimon
    eydaimon

    Reposting here from github:

    Fopper,

    This below is/was my use-case which was solved by Alex's branch:

    I just want to make sure a method gets called on an instance. So let's say I have method foo: def foo; o = Object.new; o.some_meth; return o; end # how can I check if some_meth was called on o?

    foo() in my case was called deep in some code which I had no access to directly, so I could not rely on any other side-effects to check that it was called. How would I have handled this using mocha as it stands?

    November 11th, 2010 @ 06:34 PM

  4. James Mead
    James Mead

    @alex: I thought that one key difference was that you are allowing side effects to change anything, not just parameters passed into the stubbed method.

    @eydaimon: Can you post a complete example test and implementation that demonstrates what you are trying to do and I'll see if I can help. It's much easier to discuss concrete code rather than descriptions of code.

    November 11th, 2010 @ 06:48 PM

  5. James Mead
    James Mead

    @alex: to better understand your use case, it would be helpful to see the test and the code e.g. in a Gist

    November 11th, 2010 @ 06:59 PM

  6. Alex
    Alex

    Here's some code pulled from my tumblr-sync code.

    http://pastie.org/1290749

    I wanted to be able to get actual numbers for saves called etc. so that I could ensure my code was processing it's info properly. 'runs' seemed like a good way to approach that, and possible other situations.

    November 11th, 2010 @ 07:42 PM

  7. James Mead
    James Mead

    @alex: can you not use the existing Mocha expectation "cardinality" methods e.g. times, at_least, at_most?

    I've had a go at an alternative version of your pastie.

    November 12th, 2010 @ 11:40 AM

  8. eydaimon
    eydaimon

    I believe this should sufficiently describe it:

    https://gist.github.com/fdb0907fcae297006f92

    I am using Alex's code here. Not sure how I would proceed otherwise. Since i'm creating a new instance of LogMail, I can't set a flag on it to mark it sent.

    November 12th, 2010 @ 05:55 PM

  9. eydaimon
  10. James Mead
    James Mead

    @eydaimon: From what I can tell, you want to know that the LogMail#send method has been called. Is that correct? If so, can you not use the following :-

    LogMail.any_instance.expects(:send)
    

    This will give you an error along the following lines if the send method is not called :-

    not all expectations were satisfied
    unsatisfied expectations:
    - expected exactly once, not yet invoked: #<AnyInstance:LogMail>.send(any_parameters)
    

    An alternative would be to stub the LogMail.new method and return a mock :-

    log_mail = mock('log_mail')
    log_mail.expects(:send)
    LogMail.stubs(:new).returns(log_mail)
    

    I hope this helps. Cheers, James.

    November 15th, 2010 @ 10:26 PM

  11. James Mead
    James Mead

    @alex: Did my suggestion help...?

    November 15th, 2010 @ 10:28 PM

  12. eydaimon
    eydaimon

    That does help. thank you much James

    November 16th, 2010 @ 12:38 AM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.