Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display matching invocations alongside expectations #394

Conversation

nitishr
Copy link
Contributor

@nitishr nitishr commented Oct 24, 2019

As suggested in #178

@floehopper
Copy link
Member

At first glance, this looks really promising. I'll try to do a full review as soon as I can, but it might not be for a couple of days. Thanks again for all your contributions - they are much appreciated! 😄

Copy link
Member

@floehopper floehopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now reviewed the commits up to and including the "DRY up tests" commit, i.e. I haven't looked in detail at the commits where you move invocations into cardinality. In general it looks great and I'm really grateful for all your work on this.

I've made a number of suggestions in inline comments. I've reviewed the changes commit by commit and it looks as if some of the inline comments have been hidden by GitHub, because it doesn't really respect the commits within the review process.

I also have a couple more general suggestions:

  • It would be nice to add unit tests for the new classes you've introduced, e.g. Invocation, RaisedException, etc. Presumably we could effectively move some of the existing unit tests into these new tests...?

  • It would also be nice if the #assert_invocations method you extracted in the acceptance test was less brittle i.e. did not assert the existence of other lines of the failure messages particularly details of the expected method. I can see that we need at least one test to ensure the invocations are displayed in the correct position within the other lines, but maybe that could be done separately...?

I'm sorry I haven't yet looked at the later commits, but given that I've suggested a bunch of changes, I wondered if it might be easier to get this first part of the PR merged as a separate PR and then tackle the latter commits in another PR. What do you think?

If you'd prefer, I'd be happy to address my suggestions myself, but unless I hear otherwise, I'll work on the assumption that you'll make the changes yourself.

lib/mocha/expectation.rb Show resolved Hide resolved
lib/mocha/expectation.rb Outdated Show resolved Hide resolved
lib/mocha/expectation.rb Outdated Show resolved Hide resolved
lib/mocha/expectation.rb Show resolved Hide resolved
@floehopper
Copy link
Member

Just to expand slightly on my review, I think the most pressing issues are handling the Expectaction#throws scenario and avoiding rescuing Exception.

@floehopper floehopper self-assigned this Nov 4, 2019
@nitishr nitishr force-pushed the display-matching-invocations-alongside-expectations branch 2 times, most recently from 9dca773 to ff8c3a3 Compare November 5, 2019 22:11
Since an expectation can have multiple matching invocations, and an invocation
can yield multiple times, we will need to remember the sequence of yields per
invocation. Yields also don't correspond 1:1 with returned values or raised
expectations. Therefore, remembering the results/effects of an invocation is
best done by a different object.
This will allow the yields to be captured and remembered by the invocation
@nitishr nitishr force-pushed the display-matching-invocations-alongside-expectations branch 2 times, most recently from e056a78 to f8e640a Compare November 6, 2019 05:46
previously the expected arguments were being displayed here (using Expectation#method_signature), but now the actual arguments are displayed
The tests were helpful for test-driving, but not all are needed due to overlap
@nitishr nitishr force-pushed the display-matching-invocations-alongside-expectations branch from f8e640a to 9391c7e Compare November 6, 2019 07:29
Less state to maintain. Array#size is O(1), so should be fine performance-wise.
expectations test

extract the assert invocations method. Note that the method is a bit
fragile as it assumes a certain fixture, without which the calling test
would fail.
Cardinality and invocations go hand-in-hand. So, the target is to move
invocations to Cardinality.
Cardinality and invocations go hand-in-hand. So, the target is to move
invocations to Cardinality.
The target is to encapsulate it inside Cardinality in a tell-don't-ask style.
The target is to encapsulate it inside Cardinality in a tell-don't-ask style.
expected hadn't quite sounded right because it also included allowed
This helps us avoid rescuing an exception raised by code elsewhere in the Mocha
code. Also, enables us to handle the missed scenario where Expectaction#throws
is called, 'coz without it, we'd need a generic way to catch any thrown tag and
that might be quite awkward.
@nitishr nitishr force-pushed the display-matching-invocations-alongside-expectations branch from 9391c7e to 4e32c0c Compare November 6, 2019 10:36
@floehopper
Copy link
Member

@nitishr I see you've done a bunch more work on this. Let me know if/when you'd like me to review anything. And in particular whether you're happy with the idea of only merging the first half of this PR as a first step...? I'm also happy to take it off your hands now you've done all the hard work if you're fed up with it!

Copy link
Contributor Author

@nitishr nitishr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fed up. I've addressed all your commit-based comments except where it was minor and too painful for me to fix using fixup and rebase.

I'm now gonna try to address your two general comments:

  • unit tests for new classes
  • make assert_invocations less brittle

I don't expect to make any more changes to non-test code, so feel free to review now if you'd like or wait until I'm done with the above 2 points.

As for breaking the PR up, while I'm not opposed to it, I'm not too keen to do the (what feels to me like) git gymnastics required to split unless not splitting is actually making the review harder for you (or you don't approve of the later changes). I see that the PR could be seen as two logically distinct pieces of work, but the oppositve view seems equally fair to me.

I'll commit my test changes to the same branch/PR, and you could decide if you wanna review and merge it in one go or two (or discard the cardinality-related changes altogether).

checking that invocations follow 'satisfied expectations:' ensures the
invocations are displayed in the correct position within the other lines
@nitishr
Copy link
Contributor Author

nitishr commented Nov 6, 2019

OK, I think I'm now done with this PR. I've made assert_invocations less brittle. I think that assertion as well as the whole test file could be further improved, but I don't have any bright ideas atm.

As for unit tests for the new classes, I did write one for Invocation but decided to discard it because I thought it didn't do much more than to introduce an extra set of tests that're more coupled to the structure of the system than the acceptance tests. I understand that those unit tests would still be valuable to pinpoint the causes of failures more narrowly than the acceptance tests do. However, I think the risk of difficulty focusing on the cause of a failure isn't high enough to offset the cost of the unnecessary/redundant structural coupling between the implementation and unit tests.

Also, upon reflection, I'm now much more open to splitting into 2 PRs if it makes it easier for you to review or manage as I noticed there's still a number of commits for you to review. However, I'll leave it to you to decide.

@floehopper
Copy link
Member

floehopper commented Nov 8, 2019

Cool. I've created a new version of the branch to which I've done the following:

I still haven't yet reviewed the later cardinality-related commits. I plan to tackle that next or at least decide whether to merge the earlier commits first.

@floehopper
Copy link
Member

floehopper commented Nov 8, 2019

I still haven't yet reviewed the later cardinality-related commits. I plan to tackle that next or at least decide whether to merge the earlier commits first.

I've had an initial look at the remaining commits. I'm not yet 100% convinced that the invocations belong "inside" the cardinality, but I need to have a bit more of a think about it.

Rather than add review comments to this PR for the later commits, I've decided to get the first commits merged and then open a new PR with the remaining commits.

floehopper added a commit that referenced this pull request Nov 8, 2019
First bunch of commits from #394.

Co-authored-by: Nitish Rathi <[email protected]>
@floehopper
Copy link
Member

I've now merged the first set of commits in 00f0540.

@floehopper
Copy link
Member

I've now opened a 2nd PR containing the other commits. Closing this one.

@floehopper floehopper closed this Nov 8, 2019
floehopper added a commit that referenced this pull request Nov 16, 2019
Also handles Expectaction#throws scenario.

PR: #410

Second bunch of commits from #394.

Co-authored-by: Nitish Rathi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants