Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 17, 2023 01:11 pm GMT

Stubs/fakes(casting as a particular object)

Stubs are a key tool for software testing, allowing developers to test individual units of code in isolation by replacing external dependencies with simplified versions.

Here's an example of a stub in use:

import { IncomingMessage } from "http";import { Utils } from "../../app/Utils/Utils";describe('Utils test Suite', () => {     test('getRequestPath valid request', () => {         const request = {            url: 'http://localhost:8080/login'        } as IncomingMessage        const requestPath = Utils.getRequestBasePath(request);        expect(requestPath).toBe('login')    })})

In this example, the Utils.getRequestBasePath(req: IncomingMessage): string function is being tested using a stub, specifically the const request = { url: 'http://localhost:8080/login' }as IncomingMessage object.

This object simulates an incoming HTTP request, and is used in place of an actual incoming request that would come from a client. The function is supposed to extract the base path of the request URL and return it. In the test case, the request URL is hardcoded to be "http://localhost:8080/login", and the expected output is "login". The test case uses the Jest testing framework to test the expected output of the function.

Stubs are not only useful in unit testing but also in integration testing and acceptance testing. They are used to replace external systems with simple implementations, allowing the developer to test how different parts of the system interact with each other, and make tests more efficient, reliable, and flexible.


Original Link: https://dev.to/csituma/stubsfakescasting-as-a-particular-object-105f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To