Skip to content

Try subclasses while faking sealed classesΒ #675

Open
@fwemaere

Description

@fwemaere

I have some trouble when using sealed class.

This is a sample :

import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

// Annotation which generates the cat.mocks.dart library and the MockCat class.
@GenerateNiceMocks([MockSpec<PetsRepo>()])
import 'sample.mocks.dart';

sealed class Pet {}

class Cat implements Pet {
  const Cat();
}

class Dog implements Pet {
  const Dog();
}

// Real class
class PetsRepo {
  Pet getPet(int id) => const Dog();
}

void main() {
  // Create mock object.
  var petRepo = MockPetsRepo();

  Pet cat = Cat();

  when(petRepo.getPet(1)).thenReturn(cat);
  // ....
}

I have this error

`This means Mockito was not smart enough to generate a dummy value of type
'Pet'. Please consider using either 'provideDummy' or 'provideDummyBuilder'
functions to give Mockito a proper dummy value.

Please note that due to implementation details Mockito sometimes needs users
to provide dummy values for some types, even if they plan to explicitly stub
all the called methods.`

I have to add provideDummy(cat); before when.
If i do this it works.

It would be nice if sealed class was compatible without provide dummy value !

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestcontributions-welcomeContributions welcome to help resolve this (the resolution is expected to be clear from the issue)type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions