Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data factory class for the mock records #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
@@ -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'
);
}
}
5 changes: 5 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
private class TrackPreviewControllerTest {
@IsTest
static void itShouldReturnSpotifyTrackPreviewUrl() {
// prepare data

// DATA
List<HttpResponse> responseList = new List<HttpResponse>();
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just do insert testDataFactory.creds


// TEST
Test.startTest();
String url = trackPreviewController.findTrackPreviewUrl('123456');
Test.stopTest();
// assert

// ASSERT
String.isNotBlank(url);
}

Expand All @@ -29,5 +30,5 @@ private class TrackPreviewControllerTest {
baseResponse.setStatusCode(SpotifyApiHttpClient.SUCCESS_STATUS_CODE);
baseResponse.setBody(body);
responseList.add(baseResponse);
}
}
}