From 04e8614f8f1eee3dbe4c2e9005536be102e444b9 Mon Sep 17 00:00:00 2001 From: nicolasparrague Date: Wed, 22 May 2024 16:22:17 -0500 Subject: [PATCH] Data factory class for the mock records --- .../main/default/classes/TestDataFactory.cls | 14 +++++++++++ .../classes/TestDataFactory.cls-meta.xml | 5 ++++ .../tests/TrackPreviewControllerTest.cls | 23 ++++++++++--------- 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 force-app/main/default/classes/TestDataFactory.cls create mode 100644 force-app/main/default/classes/TestDataFactory.cls-meta.xml diff --git a/force-app/main/default/classes/TestDataFactory.cls b/force-app/main/default/classes/TestDataFactory.cls new file mode 100644 index 0000000..67ef9d5 --- /dev/null +++ b/force-app/main/default/classes/TestDataFactory.cls @@ -0,0 +1,14 @@ +public class TestDataFactory { + + public ClientCredential__c creds; + + public void createCreds() { + + creds = new ClientCredential__c( + User__c = UserInfo.getUserId(), + ClientId__c = 'ClientId__c', + ClientSecret__c = 'ClientSecret__c', + SpotifyUserId__c = 'SpotifyUserId__c' + ); + } +} \ No newline at end of file diff --git a/force-app/main/default/classes/TestDataFactory.cls-meta.xml b/force-app/main/default/classes/TestDataFactory.cls-meta.xml new file mode 100644 index 0000000..019e850 --- /dev/null +++ b/force-app/main/default/classes/TestDataFactory.cls-meta.xml @@ -0,0 +1,5 @@ + + + 59.0 + Active + \ No newline at end of file diff --git a/force-app/main/default/classes/controllers/controllers/tests/TrackPreviewControllerTest.cls b/force-app/main/default/classes/controllers/controllers/tests/TrackPreviewControllerTest.cls index c82d18c..9276cdd 100644 --- a/force-app/main/default/classes/controllers/controllers/tests/TrackPreviewControllerTest.cls +++ b/force-app/main/default/classes/controllers/controllers/tests/TrackPreviewControllerTest.cls @@ -2,24 +2,25 @@ private class TrackPreviewControllerTest { @IsTest static void itShouldReturnSpotifyTrackPreviewUrl() { - // prepare data + + // DATA List responseList = new List(); addResponse(responseList, '{"access_token":"1111"}'); addResponse(responseList, '{"preview_url":"this is the url"}'); Test.setMock(HttpCalloutMock.class, new HttpCalloutMockFactory(responseList)); - ClientCredential__c newCretentials = new ClientCredential__c( - User__c = UserInfo.getUserId(), - ClientId__c = 'ClientId__c', - ClientSecret__c = 'ClientSecret__c', - SpotifyUserId__c = 'SpotifyUserId__c' - ); - insert newCretentials; - // test + // mock credentials + CredentialTestDataFactory testDataFactory = new CredentialTestDataFactory(); + testDataFactory.createCreds(); + ClientCredential__c creds = testDataFactory.creds; + insert creds; + + // TEST Test.startTest(); String url = trackPreviewController.findTrackPreviewUrl('123456'); Test.stopTest(); - // assert + + // ASSERT String.isNotBlank(url); } @@ -29,5 +30,5 @@ private class TrackPreviewControllerTest { baseResponse.setStatusCode(SpotifyApiHttpClient.SUCCESS_STATUS_CODE); baseResponse.setBody(body); responseList.add(baseResponse); - } + } }