小编Raj*_*ngh的帖子

如何在 Angular 中对行为主题进行单元测试

我想测试 .get 的 get 和 set 方法my user.store.ts。我有一个get()用于获取users和addUsers()用于将用户添加到BehaviourSubject. 我怎样才能做到这一点?

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { User } from 'ngx-login-client';

@Injectable({
  providedIn: 'root'
})

export class UserStore {

  private _users: BehaviorSubject<User[]> = new BehaviorSubject([]);

  get users() {
    return this._users.asObservable();
  }

  addUsers(users: User[]) {
    this._users.next(users);
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望输出是在addUsers()调用时添加的值,并且get在调用时可以添加用户。我get users()是 Angular 测试的新手。

得到类似的错误:

Expected Observable({ _isScalar: false, source: BehaviorSubject({ …

unit-testing typescript angular angular6 angular7

5
推荐指数
1
解决办法
1万
查看次数

Kubernetes 客户端找不到模块

我正在尝试连接到托管在 上的本地 Kubernetes 集群minikube,这是相同的代码,现在当我这样做时go run minikube.go,它给了我一个错误说:

../../../pkg/mod/k8s.io/client-go@v11.0.0+incompatible/kubernetes/scheme/register.go:26:2: module k8s.io/api@latest found (v0.19.0), but does not contain package k8s.io/api/auditregistration/v1alpha1`.
Run Code Online (Sandbox Code Playgroud)

现在,我尝试使用手动安装该软件包,go get然后我发现该软件包不存在。我怎样才能让它工作并解决这个问题?我的go.mod 文件,以防有人想看。

go kubernetes kubernetes-go-client go-packages go-modules

4
推荐指数
1
解决办法
7026
查看次数

如何对Angular的canActivate进行单元测试?

如何测试canActivateangular 的函数,该函数返回一个函数,该函数又返回一个boolean值?。

我尝试创建ActivatedrouterSnapshotand 的对象routerStateSnapshot并将其传递给canActivate函数,但这没有帮助。

export class AuthGuard implements CanActivate {
constructor(
private authService: AuthenticationService,
private loginService: LoginService,
private router: Router
) {}

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> 
| boolean {
return this.checkLogin(state.url);
}

checkLogin(url: string): boolean {
    if (this.authService.isLoggedIn()) {  return true; }

// Store the attempted URL for redirecting
this.loginService.redirectUrl = url;

// Navigate to the login page with extras
this.router.navigate(['/login']);
return false;
 }
}
Run Code Online (Sandbox Code Playgroud)

自从 …

angular-ui-router angular angular6 angular7

3
推荐指数
1
解决办法
6299
查看次数

错误:预期对标准“按功能匹配:”的一个匹配请求,未找到

我有一个userService.spec.ts要测试的文件。代码就是这样。它调用了一个get函数,该函数调用了ADMIN_API_URL。我尝试测试给定的文件,但标题中提到了错误。我的user.service.spec.ts文件。

 describe('Service: User service', () => {
 let httpMock: HttpTestingController;
 let userService: UserService;
 let url: string;

  beforeEach(() => {
   TestBed.configureTestingModule({
    imports: [HttpClientTestingModule],
     providers: [
    { provide: AUTH_API_URL, useValue: 'https://auth.example.com/api/' 
      },
    { provide: SSO_API_URL, useValue: 'https://sso.example.com/auth/api/' },
    { provide: WIT_API_PROXY, useValue: 'https://wit.example.com/api/'},
    { provide: ADMIN_API_URL, useValue: 'https://admin.example.com/api/'},
    { provide: REALM, useValue: 'realm' },
    Broadcaster,
    Logger,
    AuthenticationService,
    HttpHandler,
    UserService
  ]
});
httpMock = TestBed.get(HttpTestingController);
url = TestBed.get(ADMIN_API_URL);
userService = TestBed.get(UserService);
 });

 it('Get user by user name …
Run Code Online (Sandbox Code Playgroud)

unit-testing karma-jasmine angular6 angular7

2
推荐指数
1
解决办法
1912
查看次数

无法在 Angular 10 中处理 HttpClient

当我尝试这样做时ng build,我收到一条错误消息

ERROR in node_modules/@angular/common/http/http.d.ts:81:22 - error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class.

This likely means that the library (@angular/common/http) which declares HttpClient has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible …
Run Code Online (Sandbox Code Playgroud)

httpclient angular angular10

1
推荐指数
1
解决办法
1173
查看次数