Skip to main content
Robert Lopez (Salesforce) a posé une question dans #Trailhead
I am trying to complete the Unit Testing on the Lightning Platform module and am stuck on the Create a permission-based test. I get the error "At least one of the test methods in 'PositivePermissions_Tests' reported a failure when running the tests. Double-check the instructions."

Looking at the test logs it shows the two classes below are where the issue must lie as each is showing 1 error.

I am trying to complete the Unit Testing on the Lightning Platform module and am stuck on the Create a permission-based test. I get the error "At least one of the test methods in 'PositivePermissions_Tests' reported a failure when running the tests. Double-check the instructions."

Looking at the test logs it shows the two codes below are where the issue must lie as each is having 1 error.

My Permissions_Tests.apxc code is:

 

@isTest

private class Permissions_Tests {

@TestSetup

static void testSetup(){

Account a = TestFactory.getAccount('No view For You!', true);

Private_Object__c po = new Private_Object__c(account__c = a.id, notes__c = 'foo');

insert po;

}

@isTest static void PermissionSetTest_Negative() {

User u = TestFactory.generateUser('standard user');

System.runAs(u){

Private_Object__c[] pos;

Test.startTest();

pos = [SELECT Id, Account__c, notes__c FROM Private_Object__c];

Test.stopTest();

system.assert(pos.size() == 0, 'a user without the permission set cannot see any records');

}

}

}

My PositivePermission_tests code is:

 

@isTest

public class PositivePermission_tests {

@TestSetup

static void testSetup(){

Account a = TestFactory.getAccount('No view For You!', true);

Private_Object__c po = new Private_Object__c(account__c = a.id, notes__c = 'foo');

insert po;

}

@isTest static void PermissionSetTest_Positive() {

User u = TestFactory.generateUser('Custom User');

System.runAs(u){

Private_Object__c[] pos;

Test.startTest();

pos = [SELECT Id, Account__c, notes__c FROM Private_Object__c];

Test.stopTest();

system.assertEquals(pos.size(),1);

}

}

}

Any help would be much appreciated.

 
6 réponses
  1. 19 janv. 2024, 09:04

    @isTest

        static void testPositivePermissionSet(){

            // GIVEN

            User u = new User(

                ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User']

                .Id,

                LastName = 'last',

                Email = 'Cpt.Awesome@awesomesauce.com',

                UserName = 'Cpt.Awesome.' + DateTime.now().getTime() + '@awesomesauce.com',

                Alias = 'alias',

                TimeZoneSidKey = 'America/Los_Angeles',

                EmailEncodingKey = 'UTF-8',

                LanguageLocaleKey = 'en_US',

                LocaleSidKey = 'en_US'

            );

            insert u;

            PermissionSet ps = [

                SELECT Id

                FROM PermissionSet

                WHERE Name = 'Private_Object_Access'

            ];

            insert new PermissionSetAssignment(

                AssigneeId = u.id,

                PermissionSetId = ps.Id

            );

            

            System.runAs(u) {

                // WHEN

                

                Test.startTest();

                Private_Object__c[] pos = [SELECT Id, Account__c, Notes__c FROM Private_Object__c];

                Test.stopTest();

                // THEN

                Assert.areEqual(

                    1,

                    pos.size(),

                    'A user without the permission set shouldn\'t see any records');

            }

        }

0/9000