4

I am drafting up a couple of unit tests for this code.

Here is what the code looks like so far:

public class ContentController {

    public ContentController() {
        file = new ContentVersion();
    }

    public ContentVersion file { get; set; }
    public PageReference go() {
        insert file;
        return new PageReference('**url**');
    }
}

The initial unit test for testing just a file upload for this code is drafted up as such:

@isTest
private class BestDoctorsTest{
    @isTest
    private static void testUpload(){
        ContentController controller = new ContentController();
        controller.file.Body = Blob.valueOf('populated');
        PageReference redirect = controller.go();
        system.assertEquals(1, [SELECT count() FROM ContentVersion]);
    }
}

Given how Blobs work and the functionality of ContentVersions this should work fine. However, apex returns this error: enter image description here

How to I correctly obtain the body of a ContentVersion?

1 Answer 1

6

For ContentVersion, the property that contains the blob data is 'VersionData'. Line 6 should be changed to:

controller.file.VersionData = Blob.valueOf('populated');

See more here:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

2
  • Welcome to SFSE!
    – sfdcfox
    Jan 10, 2018 at 18:05
  • Yes, welcome! Looks like you're off to a solid start :)
    – Matt Lacey
    Jan 11, 2018 at 4:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .