You need to sign in to do that
Don't have an account?

Test class - Task Trigger
Hi,
I have a trigger that update a field in opportunity when a task related to this opportunity have the status set to completed.
Here's my trigger:
trigger TestTask on Task (after update) { public Id oppId; for(Task t:Trigger.New){ if(t.isClosed){ oppId = t.WhatId; } } List<Opportunity> opp = [select Id from Opportunity where id = :oppId]; for(Opportunity o : opp){ o.Tarefa__c = 'Não'; update o; } }
I have to write a text class but i'm not sure how can I test the value of the updated field. I've inserted an opportunity and a task, but I don't know how can I finish the test class.
@isTest private class testMyTaskTrigger { public static testMethod void testTTrigger(){ Opportunity opp = new Opportunity(); opp.name = 'test opp 1'; opp.StageName = 'Oferecer projeto/orçamento'; opp.LeadSource = 'Google'; opp.Projeto__c = 'COM'; opp.Regiao__c = 'Baixada Santista'; opp.Forma_de_atencimento__c = 'individual'; opp.Garantia__c = '01 ano contra defeito de fabricação'; opp.CloseDate = date.today(); insert opp; Task t = new Task(); t.OwnerId = UserInfo.getUserId(); t.Subject='Donni'; t.Status='Not Started'; t.Priority='Normal'; t.Responsavel__c='Felipe'; insert t;
Any help?
Thanks!!
Hi Felipe. Brasileiro nao eh ? :)
Test something like this:
But I'd insert more than one record for Opportunity and task, so that the test can really assert the correct values.
Breno
Hi Breno. Sim Sim =]
I recieved an error:
System.AssertException: Assertion Failed: Expected: Não, Actual: null
Class.testMyTaskTrigger.testTTrigger: line 30, column 5 External entry point
Do you know how can i fix that?
Thanks.
Felipe.
Try this... Just saw that your trigger was "after update"..
Are you suing the Force.com IDE to test ?
When you are inserting a Task in Test Class then you need to give the Whatid also.
t.Whatid = opp.id;
Good catch... I had my code copied from an instance where I've inserted multiple Opportunities. Thats why the opp[0].id there..
I can't save the class... I got an error in this part:
List<task> tasktoupdate = New List<task>{ [select id from task where id in :t]};
for(task t:tasktoupdate)
t.status = 'Completed';
Error: "Duplicate variable: t"
I'm using Sales Force to run tests. Button run tests on apex classes page.
oops.. had used the t already on previous task ..
try it:
I'm having the same error =/
"System.AssertException: Assertion Failed: Expected: Não, Actual: null"
any help?
my code:
Felipe, Try changing the 2nd line to reference you trigger, as you've specified on the trigger name:
Should read " Private class testTesttask { "
Also, are the values of the Task status field changed to Portuguese ? Like " Nova, Nao iniciada, completa, etc.." ?
Were you able to get this to work ?
The problem was the task status...
The code is working now!
Thanks!!