title: How to unit test Behaviour Subjects in Angular
body: <p>I want to test get and set method of <code>my user.store.ts</code>. I have a <code>get()</code> which is used to get <code>users</code> and <code>addUsers()</code> which is used to add Users into the <code>BehaviourSubject</code>. How Can I achieve that?</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { BehaviorSubject } from 'rxjs';
  import { User } from 'ngx-login-client';
  
  @Injectable({
    providedIn: 'root'
  })
  
  export class UserStore {
  
    private _users: BehaviorSubject&lt;User[]&gt; = new BehaviorSubject([]);
  
    get users() {
      return this._users.asObservable();
    }
  
    addUsers(users: User[]) {
      this._users.next(users);
    }
  }
  </code></pre>
  
  <p>I expected the output to be values getting added when <code>addUsers()</code> is called and can <code>get</code> users when I call <code>get users()</code>.I'm new to Angular Testing.</p>
  
  <p>Getting an error something like:</p>
  
  <blockquote>
    <p><code>Expected Observable({ _isScalar: false, source: BehaviorSubject({
    _isScalar: false, observers: [  ], closed: false, isStopped: false, hasError: false, thrownError: null, _value: [ Object({ attributes:
    Object({ fullName: 'name', imageURL: '', username: 'myUser' }), id:
    'userId', type: 'userType' }) ] }) }) to equal [ Object({ attributes:
    Object({ fullName: 'name', imageURL: '', username: 'myUser' }), id:
    'userId', type: 'userType' }) ].</code></p>
  </blockquote>
  
  <p>My <code>User[]</code> is of type:</p>
  
  <pre><code>{
      'attributes': {
          'fullName': 'name',
          'imageURL': '',
          'username': 'myUser'. 
      },
      'id': 'userId',
      'type': 'userType'
  }
  </code></pre>
  
  <p><strong>update: My user.store.spec.ts file</strong>.</p>
  
  <pre><code>import { TestBed, async } from '@angular/core/testing';
  
  import { UserStore } from './user.store';
  import { BehaviorSubject } from 'rxjs';
  import { User } from 'ngx-login-client';
  
  describe('UsersStore', () =&gt; {
    beforeEach(() =&gt; TestBed.configureTestingModule({}));
  
    it('should be created', () =&gt; {
       const store: UserStore = TestBed.get(UserStore);
       expect(store).toBeTruthy();
    });
    it('should add Users', async(() =&gt; {
      let store: UserStore;
      store = TestBed.get(UserStore);
      let user: User[];
      const testUser: User[] = [{
        'attributes': {
        'fullName': 'name',
        'imageURL': '',
        'username': 'myUser'
      },
      'id': 'userId', 
      'type': 'userType'
     }];
     store.addUsers(testUser);
     store.users.subscribe(users =&gt; {
       expect(users).toBe(testUser);
       });
    }));
  });`
  </code></pre>