logo
down
shadow

Testing that a component behaves properly when the service it calls throws an exception with Angular 2


Testing that a component behaves properly when the service it calls throws an exception with Angular 2

By : Jakub Mirovský
Date : November 28 2020, 11:01 PM
I wish this helpful for you You can't do that as the error will just be thrown and bubble up to the test. What you need to do is allow the user the subscribe with callbacks, and then you can call the error callback. For example
code :
class FakeAuthService {
  authenticateUser(user: User) {
    // return this service so user can call subscribe
    return this;
  }

  subscribe(onNext, onError) {
    if (onError) {
      onError(new InvalidCredentialsError());
    }
  }
}
authenticateUser(user: User) {
  return Observable.throw(new InvalidCredentialsError());
}


Share : facebook icon twitter icon
Making service calls using ServiceStack and a C# client with Windows Authentication throws Unauthorized exception

Making service calls using ServiceStack and a C# client with Windows Authentication throws Unauthorized exception


By : gerard
Date : March 29 2020, 07:55 AM
help you fix your problem By adding a RequestFilter to the client you can sets the credentials to the current logged in user, the same way the browser does for you.
See CredentialCache.DefaultCredentials for more info.
code :
JsvServiceClient client = new JsvServiceClient("https://host:port/");
...
client.RequestFilter = req => {
    req.Credentials = CredentialCache.DefaultCredentials;
};
testing angular service internal method calls

testing angular service internal method calls


By : wangjijun
Date : March 29 2020, 07:55 AM
around this issue What happens here is that you set on a spy on service.a method, but the internal a (which is called internally by b) is still the same internal method (i.e not the spy), that's why your test fail.
If you really want to do that, the only way is to not call the internal a but the method of your service:
code :
app.factory('Shoes', function() {
    return {
        a: function a() {
            return 12;
        },

        b: function b() {
            return this.a();
        }
    };
});
Angular 2+ Unit testing a service that calls a service that calls an httpclient get

Angular 2+ Unit testing a service that calls a service that calls an httpclient get


By : Dor Shitrit
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further When you unit testing, you test what the service being tested does. You don't test what its dependencies do.
This means, you only have to test if the service is calling the other service, and mock the return.
code :
US = injector.get(UserService);
CS = injector.get(ConfigService);

it('Testing a function called myFunc in your service ...', () => {
  spyOn(US, 'myUserServiceFunctionCalled')
    .and.returnValue(Observable.of('The mocked answer returned by this function'))
  spyOn(CS, 'myConfigServiceFunctionCalled')
    .and.returnValue(Observable.of('The mocked answer returned by this function'))

  sut.myFunc(); // .subscribe if it's an Observable, and put your expects in it
  expect(US.myUserServiceFunctionCalled).toHaveBeenCalled();
  expect(CS.myConfigServiceFunctionCalled).toHaveBeenCalled();
});
Angular Component calls Service that calls service: both need to do something with result

Angular Component calls Service that calls service: both need to do something with result


By : user2923965
Date : March 29 2020, 07:55 AM
Hope this helps This can be accomplished using tap and returning the observable from the service.
service.ts
code :
public doSomething(payload: any): Observable<TypeHere> {
    return this._httpService.doGet(url).pipe(tap((result) => {
        // do something with the result
    }));
}
public doSomethingInService(payload: any): void {
    this.myService.doSomething(payload).subscribe(_ => /* do something */);
}
Providing/Injecting third party service to component while angular component unit testing

Providing/Injecting third party service to component while angular component unit testing


By : Mcilvena
Date : January 10 2021, 02:04 PM
wish of those help The problem is that you use the original FuseService.
And that FuseService has dependencies. That means you would have to provide them also. And their dependencies. And theirs. And so on.
code :
 TestBed.overrideProvider(OriginalService, {useValue: new MockService()});
shadow
Privacy Policy - Terms - Contact Us © soohba.com