#Test private method

9 messages · Page 1 of 1 (latest)

astral nacelle
#

I'm trying to understand how I can do this type of test within the class I have a private method that is called. but I don't understand how I can change this call or if this private method really needs testing ```ts
async execute({
options,
searchParam,
}: FindAllAllowedOriginsProps): Promise<
Pagination<FindAllAllowedOriginResultDto>

{
const allowedOrigins =
await this.allowedOriginCustomRepository.findByOriginNameOrChannelOrOriginId(
{
options,
searchParam,
},
);

const items = await Promise.all(
  allowedOrigins.items.map(async (allowedOrigin) => {
    const originName = await this.getOriginName(
      allowedOrigin.originId,
      allowedOrigin.originName,
    );
    console.log('originName', typeof originName);

    return {
      id: allowedOrigin.id,
      originId: allowedOrigin.originId,
      originName,
      channelId: allowedOrigin.channel.id,
      channelName: allowedOrigin.channel.channelName,
      backhalfLink: allowedOrigin.backhalfLink,
      shortenLink: allowedOrigin.shortenLink,
    };
  }),
);

const allowedOriginsFormatted = Object.create(allowedOrigins);
allowedOriginsFormatted.items = items;
allowedOriginsFormatted.meta = allowedOrigins.meta;
allowedOriginsFormatted.links = allowedOrigins.links;
console.log('allowedOriginsFormatted', allowedOriginsFormatted);

return allowedOriginsFormatted;

}

private async getOriginName(
originId: number,
originName: string | undefined,
): Promise<string> {
if (originName) {
return originName;
} else {
const commercialOrigin = await this.getOriginPierlabsService.execute(
originId,
);
return commercialOrigin.nome;
}
}

burnt hearth
#

Generally you won't directly test private methods. You'll have them indirectly tested by calling public methods that call to them

astral nacelle
#

in this case I should create just a mock of your result

burnt hearth
#

I assume the getOriginName method is what you're referring to here? You should mock the getOriginPierlabsService.execute method

astral nacelle
#

correct, but I call it here in the public class, this doesn't need to be put in the test```ts
const originName = await this.getOriginName(
allowedOrigin.originId,
allowedOrigin.originName,
);

burnt hearth
#

this doesn't need to be put in the test
What do you mean by this?

astral nacelle
#

I have never come across this type of class with private methods, so my leader told me that he would need to test this class and that this method can return a name if a name parameter is passed in the search, and that if no parameter is passed it brings a list of names

reef root
#

I think as long as you have this method in getOriginName mocked out:

const commercialOrigin = await this.getOriginPierlabsService.execute(
        originId,
      );

Then you shouldn't have to worry about the private method at all. getOriginName just sends something back based on what's going on in the calling method, which is what you actually care about testing.

If you really need to, you can do something like expect(someMethod).toBeCalled() and make sure the right flow of operations is happening, but again, that should be decoupled from the private function itself.