You need to sign in to do that
Don't have an account?
DevAccount
How to write Test method for Upsert Statement. Please help..
How to write Test method for Upsert Statement. Please help..
public PageReference reset()
{
TallySheetItemList= [select ID, MELT__c, BND__c, GR_WT_KGS__c, Tare__c,
NET_WT_KGS__c, Melt_Date__c, INGOTS__c, Ref__c, IsDeleted__c from TS_NASAAC_Item__c where Tally_Sheet_Ref__c = :TS.ID and IsDeleted__c = false order by CreatedDate limit 100 ];
return null;
}
public PageReference saveItem()
{
upsert TallySheetItemList;
reset();
return null;
}
Currently I have it in my testMethod
/* Test Case */
static testMethod void myUnitTest() {
PageReference PageRef = Page.vfTallySheet;
test.setCurrentPage(PageRef);
Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Tally_Sheet__c());
TallySheetItems controller = new TallySheetItems(sc);
controller.reset();
controller.saveItem();
}
/* Test Case ends....*/
I'm getting 100% test coverage when I run this (with some additional stuff, like a constructor).
What's the behavior that you're expecting but not seeing?
[Edit]: One possibility that occurs to me is that you're expecting to see the results of the upsert. But you won't--transactions inside a testMethod are rolled back when that method ends, so unless you check the results *during method execution*, you won't see any changes.
Hi Avrom,
Even though my current test method covers 77% (red fonted are not currently covered), but salesforce is not allowing me to move it production
Below is my whole apex class with test method and every thing. I would certainly appreciate any kind of help on corrrecting my test method to cover 100% and move it to production.
When I moved this to production I got the below two failure..
Aha. I think I see what's wrong.
You're calling
in the method saveItem(). But TS, in your test method, is a brand new TallySheet, so can't be updated. You probably mean
By the way, I'm not quite sure why you're creating the PageReference in your testMethods. You don't use it to get your controller (instead, you're calling the controller's constructor directly), and you don't really do anything else with it as far as I can see.