function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MikeCampMikeCamp 

How do I Assert in a CanvasLifecycleHandler test class

I have a CanvasLifecycleHandler thst rewrites the url with a timeStamp.  Is it possible to assert in the test class that the url has been changed?  If so, how?

CanvasLifecycleHandler
public class CANV_LIFECYCLE_Registration
implements Canvas.CanvasLifecycleHandler {
    public Set<Canvas.ContextTypeEnum> excludeContextTypes(){
        Set<Canvas.ContextTypeEnum> excluded = new Set<Canvas.ContextTypeEnum>();
        return excluded;
    }
        
    public void onRender(Canvas.RenderContext renderContext) {
        Canvas.ApplicationContext app = renderContext.getApplicationContext();
        // get urlpath from canvas app
        string newURL = getURLPath(app.getCanvasUrl());

        //add random string to prevent caching
        newURL += '?R='+ DateTime.now().gettime();

        //update the url
        app.setCanvasUrlPath(newURL);
    }

    private string getURLPath(string canvasURL){
        String[] URLList = canvasUrl.split('/');
        string URLPath = '';
        for(Integer a = 3; a < URLList.size(); a++){
            URLPath += '/' + URLList[a];
        }
        return URLPath;
    }
}
What I have so far for Test Class (strait from docs here (https://developer.salesforce.com/docs/atlas.en-us.platform_connect.meta/platform_connect/canvas_testing_your_canvaslifecyclehandler.htm))
@isTest
private class CANV_LIFECYCLE_Registration_TEST {
    static testMethod void testDefaultMockValues(){
        // Test handler using the default mock RenderContext Canvas.Test creates
        CANV_LIFECYCLE_Registration handler = new CANV_LIFECYCLE_Registration();
        Canvas.Test.testCanvasLifecycle(handler,null);
    }
}

 
Best Answer chosen by MikeCamp
James LoghryJames Loghry
I haven't done much with Canvas, but it looks like you might need to change app (line 9 in your example) to be a  member variable in your handler (with at least a public getter), then you could use the following in your test class:
 
System.assertEquals(EXPECTED_URL,handler.app.getCanvasURL());

I'm basing that information off this method being available in the documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_canvas_ApplicationContext_getCanvasUrl.htm

All Answers

James LoghryJames Loghry
I haven't done much with Canvas, but it looks like you might need to change app (line 9 in your example) to be a  member variable in your handler (with at least a public getter), then you could use the following in your test class:
 
System.assertEquals(EXPECTED_URL,handler.app.getCanvasURL());

I'm basing that information off this method being available in the documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_canvas_ApplicationContext_getCanvasUrl.htm
This was selected as the best answer
MikeCampMikeCamp
@James Loghry, Thank you, That worked great.  I was able to asset my change in test class.