question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: safer expectations in idiomatic syntax

See original GitHub issue

Hi!

I’ve grown a bit frustrated by expectations at the end of the line, methodCall wasCalled once, because when method signature is long the reader can lose context before they read until wasCalled once, and worse, in the case of long signatures, it’s so easy to overlook adding (or drop) the verification completely. When verification is missing, the test will happily pass without complaining about anything. Additionally, when multiple verifications follow each another, they don’t line up as well as they used to, because actual verification clauses are no longer positioned one after another.

It wasn’t the case with vanilla mockito, where expectation comes first (verify(mock).foo(any)).

Strict mode solves the problem partially, but it isn’t always an option when migrating large projects from older versions of mockito: it’s physically impossible to bring thousands of mocks that have been stubbed for multiple purposes into strict mode.

To help keep verifications safe even in lenient mode, I propose replacing single-step verifications with a macro-based two-step verification.

Instead of

val myMock1 = mock[Service1]
val myMock2 = mock[Service2]

// call tested method

myMock.foo(*, *, *) wasCalled once
myMock2.bar("hello", *, *) wasCalled twice

we’d do this

val myMock1 = mock[Service1]
val myMock2 = mock[Service2]

// call tested method

expect {
  myMock.foo(*, *, *) wasCalled once
  myMock2.bar("hello", *, *) wasCalled twice
}

The entire verification section of the test should consist of expect block that would use a macro to fail compilation for every line within the block that is not a verification.

An alternative approach would be to add idiomatic syntax resembling specs2-mockito:

there was one(myMock).foo(*, *, *)
there were two(myMock2).bar("hello", *, *)

or scalamock-style

(myMock.foo _).expect(*, *, *)

What do you think?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bbonannocommented, Feb 11, 2020

Ok, we can do verification as a first step. I’m quite busy at the moment, but will take a look as soon as I finish off some other stuff.

Feel free to raise a PR if you feel like doing it yourself 😃

0reactions
einholencommented, Feb 29, 2020

Okay, I gave it a stab - please take a look when you have time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Teaching Idiomatic Expressions and Phrases - ERIC
Depending on their meanings and syntactic length, idiomatic phrases can form lexico-grammatical continua that can be treated as whole-unit ...
Read more >
Idioms: Phasehood and Compositionality - Squarespace
Abstract. This paper examines idiomatic expressions in light of the hypothesis that the vP-phase boundary defines a size limitation for verbal idioms.
Read more >
13 English Idioms for DOUBT & UNCERTAINTY - mmmEnglish
In this lesson, you'll learn to express doubt and uncertainty the RIGHT way. – When you are still thinking about a situation.
Read more >
Idioms and Expressions - Pima County Fair
We're ready to sign the contract.” Examples: 1. “The bank has confirmed the loan agreement, so It's a done deal.” 2. “The court...
Read more >
50 common business idioms - topcorrect
idiom meaning example Learn the ropes Learn the basics of something (e.g. a job) I'm learning the ropes in my new... Not going to fly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found