VOOZH about

URL: https://www.digitalocean.com/community/tutorials/testing-angular-with-jasmine-and-karma-part-1

⇱ Testing Angular with Jasmine and Karma (Part 1) | DigitalOcean


Testing Angular with Jasmine and Karma (Part 1)

Updated on September 15, 2020

By Adam Morgan

👁 Testing Angular with Jasmine and Karma (Part 1)

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

👁 Adam Morgan
Adam Morgan
Author
Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thanks !! This is a helpful tutorial, I do got one minor improvement. Cause the function under test (‘all’ or ‘findOne’) is mocked the result will always pass. Function all doesn’t do very much, but the mistake of hardcoding the id by 4 (of findOne) will not get caught:

 findOne(id: string): Observable<object> {
 const user:any = this.users.find((u: any) => {
 return u.id === 4;
 });

 return of(user);
 } 

In this case one can mock property this.users of UsersService.

You’d get something like this:

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

@Injectable({
 providedIn: 'root'
})
export class UsersService {

 users: Array<object> = [ // Add employee object
 {
 id: '1',
 name: 'Jane',
 role: 'Designer',
 pokemon: 'Blastoise'
 },
 {
 id: '2',
 name: 'Bob',
 role: 'Developer',
 pokemon: 'Charizard'
 },
 {
 id: '3',
 name: 'Jim',
 role: 'Developer',
 pokemon: 'Venusaur'
 },
 {
 id: '4',
 name: 'Adam',
 role: 'Designer',
 pokemon: 'Yoshi'
 }
 ];

 getUsersFromDb(): Array<object> {
 return this.users
 }

 constructor() { }

 all(): Observable<Array<object>> {
 return of(this.getUsersFromDb());
 }

 findOne(id: string): Observable<object> {
 const user:any = this.getUsersFromDb().find((u: any) => {
 return u.id === id;
 });

 return of(user);
 }
}

import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { UsersService } from './users.service';

describe('UsersService', () => {
 let service: UsersService;

 beforeEach(() => {
 TestBed.configureTestingModule({});
 service = TestBed.inject(UsersService);
 });

 it('should be created', () => {
 expect(service).toBeTruthy();
 });

 // Add tests for all() method
 describe('all', () => {
 it('should return a collection of users', () => {
 const userResponse:Array<object> = [
 {
 id: '1',
 name: 'Jane',
 role: 'Designer',
 pokemon: 'Blastoise'
 },
 {
 id: '2',
 name: 'Bob',
 role: 'Developer',
 pokemon: 'Charizard'
 }
 ];
 let response:any;
 spyOn(service, 'getUsersFromDb').and.returnValue(userResponse);

 service.all().subscribe(res => {
 response = res;
 });

 expect(response).toEqual(userResponse);
 });
 });

 describe('findOne', () => {
 it('should return a single user', () => {
 const data:any = [
 {
 id: '1',
 name: 'Jane',
 role: 'Designer',
 pokemon: 'Blastoise'
 },
 {
 id: '2',
 name: 'Bob',
 role: 'Developer',
 pokemon: 'Charizard'
 }
 ];
 let response;
 spyOn(service, 'getUsersFromDb').and.returnValue(data);

 service.findOne('2').subscribe(res => {
 response = res;
 });

 expect(response).toEqual(data[1]);
 });
 });
});

👁 Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.