+ Start a Discussion
rotfilrotfil 

How to perform a system assert for an update method (remote action)?

I have an example class I've just created for illustravive purposes that updates an acount name via a remote action.

I need to be able to write a test class to cover it, and it also needs to contain a system assert/assertequals?

I have written the test class but how do I cover it with a system assert?

Method:
@RemoteAction
global static void updateAccountName (String supplierId, String newName) {

    Account acc = [Select Id, Name From Account Where Id = : supplierId];

    acc.name = newName;
    
    update acc;

}
Test class:
@isTest
public class updateSupplierTest { 

	static testMethod void test () {

        updateAccount con = new updateAccount();

    	//Create an Account
        List<Account> accts = new List<Account>();
        Account b = new Account(Name='Test Buyer',type = 'Account');
        accts.add(b);
        insert accts;

        //Remote actions
        updateAccount.updateAccount(r.id, 'New Account Name');
        System.assertEquals(????); 

    }

}

Thanks
 
Best Answer chosen by rotfil
VamsiVamsi
@isTest
public class updateSupplierTest { 

	static testMethod void test () {

        updateAccount con = new updateAccount();

    	//Create an Account
        List<Account> accts = new List<Account>();
        Account b = new Account(Name='Test Buyer',type = 'Account');
        accts.add(b);
        insert accts;

        //Remote actions
        updateAccount.updateAccount(accts[0].id, 'New Account Name');
        
       List<Account> aclist = [select Name from account where id = accts[0].id ];

       system.assertEquals('New Account Name',aclist[0].Name); 

    }

}

Try the above updated code and make sure method name is correct in the above code. because I could see method name as "updateAccountName " in the remote action method.

Also what does updateAccount  ? is it a class name ?

All Answers

VamsiVamsi
Hi,

Retrieve that updated account and then perform an assert using that updated values.

List<Account> aclist = [select Name from account where id = r.id limit 1];

system.assertEquals('New Account Name',aclist[0].Name); // this is for single account and if you have multiple accounts remove the limit in above query and then iterate the results in for loop by placing this assert statement.

Hope this helps ...!!

Please mark as best answer if the above helps ...!!
rotfilrotfil

I just added the line to my test class:

List<Account> aclist = [select Name from account where id = r.id limit 1];
and got the following result:

Result: [OPERATION FAILED]: classes/updateAccountTest.cls: Unexpected token 'r.id'. (Line: 43, Column: 88)
classes/updateAccountTest.cls: Extra ';', at ']'. (Line: 43, Column: 100)
classes/updateAccountTest.cls: Expression cannot be a statement. (Line: 43, Column: 99)

VamsiVamsi
@isTest
public class updateSupplierTest { 

	static testMethod void test () {

        updateAccount con = new updateAccount();

    	//Create an Account
        List<Account> accts = new List<Account>();
        Account b = new Account(Name='Test Buyer',type = 'Account');
        accts.add(b);
        insert accts;

        //Remote actions
        updateAccount.updateAccount(accts[0].id, 'New Account Name');
        
       List<Account> aclist = [select Name from account where id = accts[0].id ];

       system.assertEquals('New Account Name',aclist[0].Name); 

    }

}

Try the above updated code and make sure method name is correct in the above code. because I could see method name as "updateAccountName " in the remote action method.

Also what does updateAccount  ? is it a class name ?
This was selected as the best answer
rotfilrotfil
Hey Vamsl,

Thanks a lot for your help. Sorry, I can see there were some mistakes in my example above - I can't see a way to edit the question now I hav eposted it. (Thumbs up!)

I think the error that I received was from a missing colon in the query you wrote above but with that fixed:
 
List<Account> aclist = [select Name from account where id = : r.id limit 1];
I now have a situation - that I didn't mention which I am not so happy with. (The above was just an example):
        ccSupplierListPopupController.reinstateSupplier(r.id);
        List<Relationship__c> rlist = [select Vendor_Code__c, Relationship_Status__c from Relationship__c where id =: r.id];
        system.assertEquals('In Sales',rlist[0].Relationship_Status__c);

ccSupplierListPopupController.updateSupplierDetailsWithRules(r.id,'Vendor Code 1','Invoice only', 'Range', 'No');
		rlist = [select Vendor_Code__c from Relationship__c where id =: r.id];
		system.assertEquals('Vendor Code 1',rlist[0].Vendor_Code__c);

        ccSupplierListPopupController.updateSupplierDetails(r.id, 'Vendor Code 2', buyerMask.id, 'User defined');
        rlist = [select Vendor_Code__c from Relationship__c where id =: r.id];
		system.assertEquals('Vendor Code 2',rlist[0].Vendor_Code__c);

        ccSupplierListPopupController.updateSupplierDetails(r.id, 'Vendor Code 3', buyerMask.id, 'Default');
        rlist = [select Vendor_Code__c from Relationship__c where id =: r.id];
		system.assertEquals('Vendor Code 3',rlist[0].Vendor_Code__c);

        ccSupplierListPopupController.updateVendorCode(r.id, 'Vendor Code 4');
        rlist = [select Vendor_Code__c from Relationship__c where id =: r.id];
		system.assertEquals('Vendor Code 4',rlist[0].Vendor_Code__c);
You can see that I have written the query 4/5 times for each of the methods/situations in the method.

Is there a way to put this query into a seperate method within the Test Class?

Many thanks for your help.

ROTFIL



 
VamsiVamsi
Hi,

Inorder to split each and every query into another method then you need to create test data required for that specific query in each method or else create some common data required for all the queries and then annotate that method with @TestSetup and then query those records in each method.