Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Angular2 testing: change the injected value of a service

I am testing a simple service. The service uses 2 values from another service.

Basically, I would like to test those 2 values : isLogged = false and isLogged = true.

Is it possible to just change the value of an injected service, or do I need to do something else ? (which I don't know, so if you could lead me on the way, I'd appreciate it).

Here is my code for testing :

EDIT I have found a solution to my problem. You need to inject the provider to the inject parameters, and you can change its properties as you want after.

  import { TestBed, async, inject } from '@angular/core/testing';
  import { AuthGuardService } from './authguard.service';
  import { Router } from '@angular/router';

  import { AuthService } from './auth.service';

  describe('AuthguardService', () => {

    let calledUrl = '/logged/main/last';

    let authServiceStub = {
      isLogged: false,
      redirectUrl: calledUrl
    };

    class RouterStub {
      navigate(url: string) { return url; }
    };

    beforeEach(() => {
      TestBed.configureTestingModule({
        providers: [
          AuthGuardService,
          { provide: AuthService, useValue: authServiceStub },
          { provide: Router, useClass: RouterStub },
        ]
      });
    });

    it('should ...', inject([AuthGuardService, Router], (service: AuthGuardService, router: Router) => {
      let spy = spyOn(router, 'navigate');

      service.checkLoginState(calledUrl);
      expect(spy).toHaveBeenCalledWith(['/login']);
    }));
  });
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your stated solution is one way. If all you want to accomplish is to change the values in a test, an even more concise way is simply to change the values in authServiceStub directly:

// inside tests:
authServiceStub.isLogged = false;
...
authServiceStub.isLogged = true;

^ Simple but not very encapsulated.

Or parameterize it:

let isLogged: false;
let authServiceStub = {
      isLogged: isLogged,
      redirectUrl: calledUrl
    };
...
// inside tests:
isLogged = false;
...
isLogged = true;

^ Concise but still not encapsulated.

Or add a setter:

let authServiceStub = {
      isLogged: false,
      setLogged: function(logged) { this.isLogged = logged; },
      redirectUrl: calledUrl
    };
...
// inside tests:
setLogged(false);
...
setLogged(true);

^ A bit more encapsulated. Could be combined with your injection approach.

Or if isLogged were reimplemented as a function, you could use spyOn:

// inside tests:
spyOn(authServiceStub, "isLogged").and.returnValue(false);
...
spyOn(authServiceStub, "isLogged").and.returnValue(true);

^ More black-box and potentially more flexible/future-proof, especially if combined with your injection solution (because you could change the stub or mock drastically and still get the behavior you want). This is more work up front, so may not be worth it. But thinking of AuthService in this black-box way may give you a reason to refactor isLogged into a function (or not).

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda