I have a Component that subscribes to a Account Service property:-
export class AppComponent {
user: User;
authenicated: boolean;
constructor(private accountService: AccountService) {
this.accountService.user.subscribe(x => this.user = x);
}
logout() {
this.accountService.logout();
}
}
I want to test this but i am receiving Failed: this.accountService.user.subscribe is not a function
The Account service snippet:-
export class AccountService {
private userSubject: BehaviorSubject<User>;
**public user: Observable<User>;**
constructor(
private router: Router,
private http: HttpClient
) {
this.userSubject = new BehaviorSubject<User>(JSON.parse(sessionStorage.getItem('user')));
this.user = this.userSubject.asObservable();
}
I can mook other observable types in other test classes without problems but i cant get this property working Now i have tried mocking, Spying spyOnProperty, each resulting in many different errors
My Example
const fakeUser: User ={
id: 0,
userName: 'TestUser',
password: 'Password',
firstName: 'Test',
lastName: 'User',
token: '',
userType: {id: 1, type: 'god'},
active: true,
deleted: false,
deletedDate: null,
createdBy: 'mock',
createdDate: new Date(),
modifiedBy: '',
modifiedDate: null
}
//const fakeAccountService = jasmine.createSpyObj('accountService', [ 'login', 'user']);
let mockAccountService = {
user: () => of(fakeUser)
};
//spyOn(mockAccountService, 'user').and.returnValue(of(fakeUser));
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent]
, schemas: [NO_ERRORS_SCHEMA],
providers: [{provide: AccountService, useValue: mockAccountService }]
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
Aucun commentaire:
Enregistrer un commentaire