开发者

How could you write a salesforce test class for a simple user-agent lookup?

开发者 https://www.devze.com 2023-03-23 19:26 出处:网络
I\'m looking to write a test for a开发者_StackOverflow function that just returns a value - that\'s it. I\'m not sure how you could do that. I\'m under the impression you have to use system.assert or

I'm looking to write a test for a开发者_StackOverflow function that just returns a value - that's it. I'm not sure how you could do that. I'm under the impression you have to use system.assert or something. New to SFDC, but have programmed in many other languages. Here's some sample code:

static String getBrowserName()
{
String userAgent = ApexPages.currentPage().getHeaders().get('User-Agent');
    if (userAgent.contains('iPhone'))
        return 'iPhone-Safari';
    if (userAgent.contains('Salesforce'))
        return 'Salesforce';
    if (userAgent.contains('BlackBerry'))
        return 'BlackBerry';
    if (userAgent.contains('Firefox'))
        return 'Firefox';
    if (userAgent.contains('Safari'))
        return 'Safari';
    if (userAgent.contains('internet explorer'))
        return 'ie';
    return 'other';
}

How can you obtain 100% test coverage for that?


While Salesforce's lack of a mocking framework is infuriating because of the hoops you have to jump through when testing things like page controllers, it's important to think about what you want to test here. Assuming that what you specifically want to test is that given the user agent strings your code returns the appropriate string, then I think something like the following should work:

static String getBrowserName(string userAgentStringToTest)
{
    PageReference pageRef = getPageReference(userAgentStringToTest);
    String userAgent = getUserAgent(pageRef);
    ...
}

PageReference getPageReference(string userAgentStringToTest)
{
    if(userAgentStringToTest.Length == 0)
    {
         return ApexPages.currentPage();
    }
    else
    {
         PageReference pageRef = new PageReference('someURL');
         pageRef.getHeaders().put('User-Agent', userAgentStringToTest);
         return pageRef;
    }
}

String getUserAgent(PageReference pageRef)
{
    pageRef.getHeaders().get('User-Agent');
}

You would then call the getBrowserName method with the empty string in your production code and with the string you want to test in your test code.

There are a few different flavours to this of course - you could overload the methods and have a parameterless method for the main code and a parameterized method for testing. It's not ideal, but I don't know of another way to do it on the force.com platform currently.

EDIT: Just for completeness, I'm adding sample tests to clarify things. My example showed how to refactor the production code to make it testable, but did not give an example of how to write a test like the OP asked for.

Your tests would look something like this:

static testMethod void checkIPhoneBrowser() 
{
    String actualBrowserName = getBrowserName('string containing iPhone somewhere');
    String expectedBrowserName = 'iPhone-Safari';
    System.assertEquals(expectedBrowserName , actualBrowserName );
}

static testMethod void checkIEBrowser() 
{
    String actualBrowserName = getBrowserName('string containing internet explorer somewhere');
    String expectedBrowserName = 'ie';
    System.assertEquals(expectedBrowserName , actualBrowserName );
}

...
0

精彩评论

暂无评论...
验证码 换一张
取 消