You need to sign in to do that
Don't have an account?
Michael Degn Jensen
Lookup user test class
Hi,
I starting out with unit testing of my first Apex Controller. I got a few test running, but this simple one is giving me trouble.
Apex Class:
I'm getting an error "Comparison arguments must be compatible types: String, User"
I have tried with string also, but fetchUser is expecting a User, so how to reference this?
Thanks,
Michael
I starting out with unit testing of my first Apex Controller. I got a few test running, but this simple one is giving me trouble.
Apex Class:
public class ALTaskController { @AuraEnabled public static user fetchUser(){ // query current user information User oUser = [select id,Name FROM User Where id =: userInfo.getUserId()]; return oUser; } }Test class:
@isTest(SeeAllData=true) private class ALTaskControllerTest { @isTest static void testfetchUser() { User u = ALTaskController.fetchUser(0050N000007fvAwQAI); System.assertEquals('0050N000007fvAwQAI', u); } }
I'm getting an error "Comparison arguments must be compatible types: String, User"
I have tried with string also, but fetchUser is expecting a User, so how to reference this?
Thanks,
Michael
Greetings to you!
You can use the below code:
Please note that SeeAllData == true will give access to all records in your org. But it is always recommended and as per best practices, you should create the test records in the test classes instead of referring the existing records in org.
Also, if you use the (SeeAllData=true), it might happen that test class pass in the sandbox but fails in production while deployment.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
Greetings to you!
You can use the below code:
Please note that SeeAllData == true will give access to all records in your org. But it is always recommended and as per best practices, you should create the test records in the test classes instead of referring the existing records in org.
Also, if you use the (SeeAllData=true), it might happen that test class pass in the sandbox but fails in production while deployment.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Thanks that makes sense and works like a charm!
And noted on the SeeAllData part.
Brgds,
Michael
You only need to call the class method it automatically covers your 100% code.
Try the following test class, it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha