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
ZedroidZedroid 

How to write testMethod for Remoting method?

Hi All,

 

I wanted to write a test method for my Javascript Remoting controller.

global class remoteTest {
@RemoteAction
global static Contact[] findContacts(string Name) {
Name = '%'+Name+'%';
Contact[] c = [SELECT ID, Name, Phone, Email from Contact where NAME LIKE :Name ];
return c;
}
}

 It would be appreciative if any one could assist regarding this, thanks in advance.

 

Regards,

Zubair.

sfdcfoxsfdcfox

You would perform the test the same as any other:

 

global class globalTest {
    @RemoteAction
    global static Contact[] findContacts(String name) {
        return [select id,firstname,lastname,email from contact where name = :name];
    }
    
    global static testMethod void testClass() {
        Contact c = new Contact(FirstName='▼',LastName='▼');
        insert c;
        Contact[] cc = globalTest.findContacts('▼ ▼');
        System.assertEquals(c.id,cc[0].id,'Failed assertion');
    }
}

It's normal Apex Code, and as such, it can be covered like any other Apex Code. Note that as a matter of simplification of the test, the name of the contact was set to something incredibly unlikely to found in any database (▼ is the control character U+001F).