# Testing Effects

```typescript
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles';
import { Observable } from 'rxjs';

import {
  InsertUser,
  InsertUserSuccess,
  InsertUserFail
} from './user.actions';
import { generateUser, generateUserArray } from '../../core/user';
// TODO: Change this path when you move your service file:
import { UserService } from '../../core/user.service';
import { UserEffects } from './user.effects';

describe('UserEffects', () => {
  let actions: Observable<any>;
  let effects: UserEffects;
  let service;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        UserEffects,
        provideMockActions(() => actions),
        {
          provide: UserService,
          useValue: { create: jest.fn() }
        },
      ]
    });

    effects = TestBed.get(UserEffects);
    service = TestBed.get(UserService);
  });

  it('should be constructed', () => {
    expect(effects).toBeTruthy();
  });

  // Effect Test
  describe('insert', () => {
    it('should return InsertUserSuccess action with entity on success', () => {
      const entity = generateUser(),
        insertAction = new InsertUser({ user: entity }),
        successAction = new InsertUserSuccess({ result: entity }),
        response = cold('-b|', { b: entity }),
        expected = cold('-s', { s: successAction });

      actions = hot('a-', { a: insertAction });

      service.create = jest.fn(() => response);

      expect(effects.insert).toBeObservable(expected);
    });
  });

  // Effect Test - Failure Path
  it('should return InsertUserFail with error object on failure', () => {
    const entity = generateUser(),
      insertAction = new InsertUser({ user: entity }),
      failAction = new InsertUserFail({ error: 'fail' }),
      response = cold('-#|', {}, { message: 'fail' }),
      expected = cold('-f', { f: failAction });

    actions = hot('i-', { i: insertAction });

    service.create = jest.fn(() => response);

    expect(effects.insert).toBeObservable(expected);
  });
});

```

{% hint style="info" %}
Write tests for Tour of Heroes:&#x20;

<https://stackblitz.com/github/jessesanders/tour-of-heroes/tree/ngrx-entity>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://briebug.gitbook.io/ngrx-workshop/testing/testing-effects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
