Open Willey1986 opened 4 years ago
I just figured out that the second solution from the description above Implement the abstract class and pass the implementation to the mocks array. isn't working at all.
NullInjectorError: StaticInjectorError(DynamicTestModule)[MyComponent -> AbstractType]:
StaticInjectorError(Platform: core)[MyComponent -> AbstractType]:
NullInjectorError: No provider for AbstractType!
EDIT1: The following code works instead which is even worse because it needs an implementation of the abstract type and a manual entry in the providers array.
class ConcreteType extends AbstractType {
...
}
createComponentFactory({
...
providers: [
{provide: AbstractType, useValue: createSpyObject(ConcreteType)}
]
})
The options available are in this file:
Which means there is no option for mocking of abstract services?
The provided example only shows how to provide an implementation for an abstract service. The provided service (in the example) is not a jasmine spy so I cannot mock its behaviour.
spectator.get(AbstractQueryService).someMethod.andReturn(...);
This is also applicable to mocking said providers, e.g. mockProvider(AbstractQueryService)
will fail with the same error, thus forcing me to have a stub class in my test files instead of automatically having a stubbed provider.
I'm submitting a...
Current behavior
At the moment when passing an abstract type to the
mocks
array the following error is shown when runningng test
The only working methods I have found so far to mock this are:
manually creating a jasmine spy
Problem here is that later in my test I have to the jasmine way to use the mock instead of the spectator way. Using the spectator way results in an error.
jasmine way
spectator way
Problem with this solution is that I have to remember which mock was created how and have to use one way or the other. This might lead to confusion when a different team member is extending my test.
Implement the abstract class and pass the implementation to the mocks array.
Problems
Expected behavior
mocks
array can handle abstract types ORcreateSpyObject()
can create mocks of abstract types ORWhat is the motivation / use case for changing the behavior?
Consistent less error prone tests.
Environment