function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ken_devken_dev 

Run unit tests as different User

Is there a way to run unit tests as a different user? I would like to test validation logic setup for different profiles.

thanks,
ken
TehNrdTehNrd
You can try writing your test methods to update attributes on the User running tests. For example, if you have a trigger that only executes when the users profile equals sales rep you could do something like this.

Code:
pseudo code:

User testUser = [select profileId from User where Id = :UserInfo.getUserId];
testUser.profileId = "sales rep" //this would be in the form of an ID
update testUser;

Then run your tests

 This changes aren't committed to the database so no worries about changing the profile.



Message Edited by TehNrd on 05-19-2008 11:27 AM
ppatppat
I entered similar post.
http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=4967&jump=true

Tehnrd , your logic in test class is not working. Is there something I am missing here ?

Thanks for the help.