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

Less verbose Mock declarations #185

Open
alestiago opened this issue Jan 30, 2023 · 3 comments
Open

Less verbose Mock declarations #185

alestiago opened this issue Jan 30, 2023 · 3 comments
Labels
question Further information is requested waiting for response Waiting for follow up

Comments

@alestiago
Copy link
Contributor

alestiago commented Jan 30, 2023

Problem
Currently defining a Mock looks like this:

class _MockDog extends Mock implements Dog {}
final Dog mockDog = _MockDog();

Solution I would like
I would like to have a one-line syntax alternative (or completely remove the old syntax):

final Dog mockDog = Mock<Dog>();

The advantages are:

  • Reduces the chances of creating a public Mock being shared, which leads to non self contained test files.
  • Reduces file pollution, sometimes a test file can have multiple mocks and the class definitions of such pollutes the readability of the file.
  • Somewhat forces developers that want to have any directly implemented
    overridden fields or methods in a Mock to use a Fake. As detailed by the documentation ("A class which extends Mock should not have any directly implemented overridden fields or methods" ).

Additional notes
I'm willing to work on a pull request for this change if there is an agreement on the new syntax. As far as I am concerned having something exactly like the proposed solution would lead to a breaking change (depends on Dart's current and future capabilities). I'm opened to read about solutions that will avoid introducing a breaking change with the new syntax. I believe that a breaking change can be justified since the new syntax has many advantages and solves a couple of problems (as outlined in Additional Context).

@felangel
Copy link
Owner

Hi @alestiago thanks for opening the issue!

Do you have an idea for what the implementation would look like? I don’t see how you would be able to achieve the following:

final Dog mockDog = Mock<Dog>();

because you would be creating an instance of a Mock and if that object doesn’t extend or implement Dog, it cannot be assigned to a variable of type Dog unless I’m missing something.

Do you have a simple POC to illustrate how this proposal would be implemented?

Thanks again!

@felangel felangel added question Further information is requested waiting for response Waiting for follow up labels Jan 30, 2023
@alestiago
Copy link
Contributor Author

alestiago commented Feb 1, 2023

@felangel I don't have a POC to illustrate how the one line example syntax can be implemented (I tried though). I think it is not possible in Dart 2.19.0 without code generation and/or mirrors.

The most obvious solution would be to do as follows, but is currently not possible to do:

class Mock<T> implements T {}

The one liner final Dog mockDog = Mock<Dog>() is what I would wish to have; but anything that results in a single line that satisfies final Dog mockDog = *; (where * denotes any expression) would suffice as long as mockDog is mockable and no code generation (build runner) is required.

Regardless of it being currently possible I wanted to open the issue to describe the most ideal scenario. Maybe there exists a way to satisfy the above statement right now that I'm not aware of. Or maybe feature Dart versions allow satisfying the above.

The aim of the issue is to encourage other developers to tackle the problem and to have a written specification of the ideal API for creating mocks.

It would be a dream 🤩 to do something as:

final Dog mockDog = Mock<Dog>();

Thanks for the package 💙 and fast reply!

@tenhobi
Copy link

tenhobi commented Feb 16, 2023

Exploring some options using the upcoming Dart's static generation would be interesting for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested waiting for response Waiting for follow up
Projects
None yet
Development

No branches or pull requests

3 participants