You need to sign in to do that
Don't have an account?
Kamil Mieczakowski
Test trigger failing
I created the following class that updates the Websites field after a new website is added. It basically normalises it by removing 'http://', 'https://', and 'www'.
Here's the class:
However when using the following test the assertion relating to removing the 'www.' component fails:
The error is:
Any idea as to why this test may be failing?
Here's the class:
trigger standardiseWebsitesTrigger on Account (after insert, after update ) { if(checkRecursive.runOnce()){ Set<Id> AccIds = new Set<Id>(); List<Account> acctsToUpdate = new List<Account>{}; for (Account acc : Trigger.new){ AccIds.add(acc.Id); } List<Account> accounts = [SELECT Id, Website FROM Account WHERE Id IN :AccIds]; for (Account acc : accounts){ string website = acc.Website; string hostDomain1 = 'http://'; string hostDomain2 = 'https://'; string justWWWAddress = 'www'; if (acc.Website.startsWith(hostDomain1) || acc.Website.startsWith(hostDomain2) && acctsToUpdate.isEmpty()){ Url u = new Url(acc.Website); website = u.GetHost(); acc.Website = u.getHost().replaceFirst('^(https?://www\\.|https?://|www\\.)',''); acctsToUpdate.add(acc); } if(acc.Website.startsWith(justWWWAddress) && acctsToUpdate.isEmpty()){ acc.website = website.substring(4); acctsToUpdate.add(acc); } update acctsToUpdate; } } }
However when using the following test the assertion relating to removing the 'www.' component fails:
@isTest public class standardiseWebsitesTriggerTest { static testmethod void standardiseWebsiteTriggerHTTP() { testSetup('HTTP', 'http://I_AM_HTTP', true); } static testmethod void standardiseWebsitesTriggerWWW() { testSetup('WWW', 'WWW.I_AM_WWW', false); } public static void testSetup(String accName, String accWebsite, Boolean webProtocol) { Account acc = new Account( Name = accName, Website = accWebsite ); insert acc; Account updatedAccount = [select Website from Account where id = :acc.Id]; if(webProtocol) { Url u = new Url(acc.Website); System.assert(u.getHost() == updatedAccount.Website); } else { System.assert(updatedAccount.Website == acc.Website.substring(4)); } } }
The error is:
> Assertion failed Class.standardiseWebsitesTriggerTest.testSetup: line 25, column 1 Class.standardiseWebsitesTriggerTest.standardiseWebsitesTriggerWWW: line 9, column 1
Any idea as to why this test may be failing?
Here is the reason of getting this assert got failed. updatedAccount.Website and acc.Website.substring(4) were holding different values.
updatedAccount.Website 's value is 'WWW.I_AM_WWW' while acc.Website.substring(4)'s value is 'I_AM_WWW'
You may get this by replacing your line number 25
by
If this helped you to resolve your problem then please mark this as best answer so that it can be useful for others. .
Thanks
Sandeep Singhal
http://www.codespokes.com/
All Answers
Here is the reason of getting this assert got failed. updatedAccount.Website and acc.Website.substring(4) were holding different values.
updatedAccount.Website 's value is 'WWW.I_AM_WWW' while acc.Website.substring(4)'s value is 'I_AM_WWW'
You may get this by replacing your line number 25
by
If this helped you to resolve your problem then please mark this as best answer so that it can be useful for others. .
Thanks
Sandeep Singhal
http://www.codespokes.com/
Let us know if this will help you