You need to sign in to do that
Don't have an account?
rotfil
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:
Thanks
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
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
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 ...!!
I just added the line to my test class:
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)
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 ?
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:
I now have a situation - that I didn't mention which I am not so happy with. (The above was just an example): 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
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.