0

I create a test method and I do not understand why the result is a failure.

1, we have a TestProfile Custom profile with create permission on Account object.

2, The next snippet of code run with failure:

@isTest
public class testTestProfile {

    @isTest
    public static void testAccountCreatablePermission(){
        Profile p = [SELECT Id FROM Profile WHERE Name='TestProfile']; 
        system.debug(p);
        User u = new User(Alias = 'standt', Email='[email protected]', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');

        System.runAs(u){
            Schema.DescribeSObjectResult dsr = Schema.SObjectType.Account;
            System.assert(dsr.isCreateable());
        }        
    }    
}

Can anybody explain why the test case is fail?

4
  • what do you get in "system.debug(p);"? Do you get correct profile ID? Commented Dec 3, 2014 at 12:19
  • Yes, there is no problem with the profile ID. Commented Dec 3, 2014 at 13:11
  • I cant see a DML that actually inserts the User that you are creating. Commented Dec 3, 2014 at 13:18
  • regarding this example we don't have to save the user just created salesforce.com/us/developer/docs/apexcode/Content/… The System method is able to see the user as a logged in user Commented Dec 3, 2014 at 14:01

2 Answers 2

0

I tried a similar snippet as above and it works like a charm for me. It gets me correct user/correct profile and assertion does not fail.

I tried it using a standard profile as well as a custom profile. I would go back and check 'TestProfile' again and see if I have given only "Read" permission on account or "Read" and "Create". I would also check if User Licence of this profile is really "Salesforce".

@isTest
private class TestRunAs {
   public static testMethod void testRunAs() {
      // Setup test data
      // This code runs as the system user
      Profile p = [SELECT Id FROM Profile WHERE Name='Custom-Hiring Manager']; 
      User u = new User(Alias = 'standt', Email='[email protected]', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
     System.runAs(u) {
         // The following code runs as user 'u' 
         System.debug('Current User: ' + UserInfo.getUserName());
         System.debug('Current Profile: ' + UserInfo.getProfileId()); 
         Schema.DescribeSObjectResult dsr = Schema.SObjectType.Account;
            System.assert(dsr.isCreateable());
      }
   }
}

Let me know!

4
  • Hmmm... Interesting. I tried the code with inserting the user not just initialized it. And it is working with it. Maybe the API version has some effect on it. I'm using the '32.0'. Otherwise the User License of the profile is 'Salesforce' Commented Dec 4, 2014 at 8:07
  • I added code snippet that worked for me in the EDIT. Its on 32.0 as well ! Commented Dec 4, 2014 at 9:12
  • hmmm... It became wired. I try it again and it is working in both way (with and without user insert DML). So I have to say the problem was solved, but I can't explain why... :) Commented Dec 5, 2014 at 9:49
  • It worked for me without DML as well, I have snippet attached ! Commented Dec 5, 2014 at 9:55
0

The problem solved by itself. And the RunAs method works independently user insert using DML operation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .