I am learning about marble tests and when following this tutorial I reached a point where my simple test looks like this:
user-effects.service.spec.ts
imports ...
describe('User Effects', () => {
it('basic test', () => {
const source = cold('--a', { a: { type: LOGIN } });
const effects = new UserEffectsService(new Actions(source), ?? );
const expected = cold('--b', { b: { type: LOGIN_SUCCESS } });
expect(effects.login$).toBeObservable(expected);
});
});
However my UserEffectsService needs 2 arguments
user-effects.service.ts
@Injectable()
export class UserEffectsService {
constructor(private actions$: Actions, private userService: UserDataService) {
}
...
Maybe I could pass there a new instance of UserDataService, but UserDataService needs 2 more arguments etc. and there would be a long chain. I couldn't find any solution for injecting a dependency without a constructor. Any tips how to deal with this case are appreciated, thanks.
@edit anyone? :D
There is a way to do this.
In your beforeEach
you can inject services like this:
let service = TestBed.get(UserDataService)
then when you create new Effect Instance you can just pass instance of service.
const effectService = new UserEfectsService(action,service);
If you have multiple services you can do something like this:
let [service1, service2, serviceN] = [Service1, Service2, ServiceN].map(TestBed.get);
Mock the injections into your service.
const actionsMock = {};
const userServiceMock = {};
Then, put all the variables and functions used in your current service, in your mocks.
For instance, let's say you use the getAllUsers
function from your user service : then add it to your mock :
const userServiceMock = {
getAllUsers: () => Observable.of([/* array of users */])
};
Now, you can create any number of objects you want :
const instance = new UserEffectsService(actionsMock, userServiceMock);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.