标签: jasmine-marbles

带延迟测试 NGRX 效果

我想测试一下效果如下:

  1. 如果调度了 LoadEntriesSucces 操作,则效果开始
  2. 等待5秒
  3. 5 秒后发送 http 请求
  4. 当响应到达时,将调度新的操作(取决于响应是成功还是错误)。

Effect 的代码如下所示:

  @Effect()
  continuePollingEntries$ = this.actions$.pipe(
    ofType(SubnetBrowserApiActions.SubnetBrowserApiActionTypes.LoadEntriesSucces),
    delay(5000),
    switchMap(() => {
      return this.subnetBrowserService.getSubnetEntries().pipe(
        map((entries) => {
          return new SubnetBrowserApiActions.LoadEntriesSucces({ entries });
        }),
        catchError((error) => {
          return of(new SubnetBrowserApiActions.LoadEntriesFailure({ error }));
        }),
      );
    }),
  );
Run Code Online (Sandbox Code Playgroud)

我想测试的是5秒后是否发出效果:

it('should dispatch action after 5 seconds', () => {
  const entries: SubnetEntry[] = [{
    type: 'type',
    userText: 'userText',
    ipAddress: '0.0.0.0'
  }];

  const action = new SubnetBrowserApiActions.LoadEntriesSucces({entries});
  const completion = new SubnetBrowserApiActions.LoadEntriesSucces({entries});

  actions$ = hot('-a', { …
Run Code Online (Sandbox Code Playgroud)

rxjs ngrx ngrx-effects jasmine-marbles rxjs-marbles

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

如何将预期的 $.length = 2 修正为等于 1

我想用茉莉花大理石测试 ngLogger 功能,但出现错误

Expected $.length = 2 to equal 1.

Expected $.length = 2 to equal 1.

Expected $[0].frame = 0 to equal 10.

Expected $[0].notification.value to be a kind of Observable, but was Object({ type: 'TECHNICAL', level: 'ERROR', msg: 'test' }).

Expected $[1] = Object({ frame: 0, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }) to equal undefined.
Run Code Online (Sandbox Code Playgroud)

测试:

Expected $.length = 2 to equal 1.

Expected $.length = 2 to equal 1.

Expected $[0].frame …
Run Code Online (Sandbox Code Playgroud)

rxjs angular jasmine-marbles

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

可观察到的特殊结果:用茉莉花弹珠进行测试

我在 Angular 7 中有一个小功能,正在用 Jest 进行测试。该函数如下所示:

private checkFreeProduct(allowance: SubscriberConnectivityAllowanceInterface): Observable<SubscriberConnectivityAllowanceInterface> {

    // TODO: This is currently just a temp function to be extended when required
    return of(allowance);

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,目前它所做的只是根据其输入创建一个可观察对象,但它正在开发中并将进行扩展。

我正在用 Jest 测试它,如下所示:

it('should return an observable of the allowance', () => {

    const allowance: SubscriberConnectivityAllowanceInterface = {
        hotspotAuthenticated: HotspotAuthenticationEnum.TRUE,
        remainingOctets: 100,
        remainingSeconds: 200,
        activeProductCost: ConnectivityProductCostEnum.PAID,
        activeProductDuration: ConnectivityProductDurationEnum.FLIGHT,
        activeProductType: ConnectivityProductTypeEnum.PREMIUM,
        connectivityProducts: []
    };

    const expected = hot('a|', {
        a: allowance
    });

    expect(hotspotService['checkFreeProduct'](allowance)).toBeObservable(expected);

});
Run Code Online (Sandbox Code Playgroud)

然而,由于一些时间问题,测试失败了。可观察到的结果expected如下所示:

[
  {
    "frame": 0,
    "notification": …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine jestjs angular jasmine-marbles

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

如何测试使用 rxjs 弹珠发出分组事件的 observables?

根据rxjs 弹珠文档,同步分组的当前行为如下:

'(ab)-(cd)': on frame 0, emits a and b then on frame 50, emits c and d
Run Code Online (Sandbox Code Playgroud)

从文档:

虽然一开始可能不直观,但在所有值同步发出后,时间将推进等于组中 ASCII 字符数的帧数,包括括号

好的,但是我如何测试这样的 observable(使用弹珠或任何其他技术):

const observable$ = of(1, 2).concat(of(3, 4).delay(20));
Run Code Online (Sandbox Code Playgroud)

有什么解决方法吗?

Stack Overflow 上有一个类似的问题,但没有关于“如何实际解决它并测试这种可观察的”的答案。

谢谢!

rxjs marble jasmine-marbles

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

ngrx + 弹珠测试 + 延迟

假设我有效果

@Effect()
someEffect$ = this.actions$.pipe(ofType(X), switchMap(() => 
of(Y).pipe(delay(3000)))
Run Code Online (Sandbox Code Playgroud)

大理石测试应该是什么样子的?

const action = new X();
const result = new Y();

actions$.stream = hot('-x', { x: action });
const expected = cold('-y', { y: result }); // ? adding frames or 3s doesn't work
expect(effects.someEffect$).toBeObservable(expected);
Run Code Online (Sandbox Code Playgroud)

作为回报,我得到

Expected $.lenght = 0 to equal 1. 
Run Code Online (Sandbox Code Playgroud)

rxjs ngrx jasmine-marbles

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

RxJs - 茉莉花弹珠 forkJoin 操作符测试

这是 forkJoin 运算符 jasmine 大理石测试:

it('test1', () => {

 const a = cold('---a|', { a: 1 });
 const b = cold('---b|', { b: 2 });

 const observable = forkJoin(
   a,
   b
 );

 const expected = cold('---21');
 expect(observable).toBeObservable(expected);
});
Run Code Online (Sandbox Code Playgroud)

测试产生以下错误:

Expected $[0].frame = 40 to equal 30.
Expected $[0].notification.value = [ 1, 2 ] to equal '2'.
Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = …
Run Code Online (Sandbox Code Playgroud)

jasmine rxjs angular jasmine-marbles

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

RxJs - 有损 zip 弹珠测试

在这篇文章之后,我决定为这个操作员编写一个弹珠测试

这是一个基本测试:

  it('Test lossy zip', () => {

    const a = hot('a---a--------a-');
    const b = hot('--b----b---b---');

    const observable = zip(
      a.pipe(take(1)),
      b.pipe(take(1))
    ).pipe(
      mergeMapTo(from(['1'])),
      repeat()
    );
    const expected = cold('--1----1-----1-');
    expect(observable).toBeObservable(expected);
  });
Run Code Online (Sandbox Code Playgroud)

该测试按预期通过。然而,当我决定像这样发射两次发射时,它失败了:

const a = hot('a---a--------a-');
const b = hot('--b----b---b---');

const observable = zip(
  a.pipe(take(1)),
  b.pipe(take(1))
).pipe(
  mergeMapTo(from(['1', '2'])), //Here, emitting two items instead of one
  repeat()
);
Run Code Online (Sandbox Code Playgroud)

我希望得到的可观察结果如下所示:

const expected = cold('--(12)----(12)-----(12)-');
Run Code Online (Sandbox Code Playgroud)

或者至少是这样的:

const expected = cold('--12---12----12-');
Run Code Online (Sandbox Code Playgroud)

然而他们都失败了。

这是一个错误jasmine-marbles还是我的期望是错误的?

javascript jasmine rxjs rxjs5 jasmine-marbles

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

如何用茉莉花大理石测试受试者

Angular 6,Rxjs,Jest,茉莉花大理石。

非常常见的情况:搜索服务器端项目的组件。在该组件中,有一些控件可以更改搜索条件,我想以“反应式”的方式进行编码。所以在组件代码中,我有这样的东西:

class SearchComponent  implements OnInit {
  public searchData: SearchData = {};
  public searchConditions$ = new Subject<SearchData>();

  constructor(private searchService: SearchService) { }

  public results$: Observable<ResultData> = this.searchConditions$.pipe(
    distinctUntilChanged(this.compareProperties), // omissis but it works
    flatMap(searchData => this.searchService.search(searchData)),
    shareReplay(1)
  );

  // search actions
  ngOnInit() {
    this.searchConditions$.next(this.searchData);
  }
  public onChangeProp1(prop1: string) {
    this.searchData = { ...this.searchData, prop1 };
    this.searchConditions$.next(this.searchData);
  }
  public onChangeProp2(prop2: string) {
    this.searchData = { ...this.searchData, prop2 };
    this.searchConditions$.next(this.searchData);
  }
}
Run Code Online (Sandbox Code Playgroud)

也就是说,Subject每当用户界面中的某些内容发生更改时,它就会触发搜索条件。

现在,我想测试搜索服务将仅针对不同的输入被调用。我可以用这种方式“不用弹珠”来做:

test('when searchConditions$ come with equal …
Run Code Online (Sandbox Code Playgroud)

rxjs jestjs angular jasmine-marbles

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

Marble 用茉莉花大理石测试受试者随时间的行为

所以我试图测试一个主题的行为,但它不起作用,而且似乎有些事情我没有正确理解。考虑以下测试:

it('marble testing subject test', () => {
    const obs$: Subject<number> = new Subject<number>();

    obs$.next(42);
    obs$.next(24);

    expect(obs$.asObservable()).toBeObservable(hot('xy', { x: 42, y: 24 }));
  });
Run Code Online (Sandbox Code Playgroud)

这失败并显示以下错误消息:

Expected $.length = 0 to equal 2.
Expected $[0] = undefined to equal Object({ frame: 0, notification: Notification({ kind: 'N', value: 42, error: undefined, hasValue: true }) }).
Expected $[1] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: 24, error: undefined, hasValue: true }) }).
Run Code Online (Sandbox Code Playgroud)

我想我有点明白为什么:(Subject每个文档)只在订阅开始后发出值。该toBeObservable()函数(我假设)订阅了Subject …

unit-testing jasmine rxjs jasmine-marbles

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

Jest 的 Marble 测试失败,但 Jasmine 的等效测试成功

我正在尝试将我的单元测试从 Jasmine 转换为 Jest。一些测试在转换为 Jest 后开始失败。有人能解释一下为什么他们在 Jest 上失败了吗?

我设法将问题隔离到下面的测试用例中。

Jasmine 运行成功:

import { JasmineMarble } from './jasmine-marble';
import { cold } from 'jasmine-marbles';
import { switchMap } from 'rxjs/operators';
import { EMPTY, Observable } from 'rxjs';

class Service {
  foo(): Observable<any> {
    return EMPTY;
  }

  bar(a): Observable<any> {
    return EMPTY;
  }
}

describe('JasmineMarble', () => {
  it('should create an instance', () => {
    const service = new Service();

    spyOn(service, 'foo').and.returnValue(cold('a|', { a: 'A' }));
    spyOn(service, 'bar').and.returnValue(cold('a-b-c|', { a: 'A', b: …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine jestjs jasmine-marbles

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