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

Testing Site.createPortalUser
Hi everyone,
Do you know a way to test a class that uses Site.createPortalUser and expect it to be successfull?
Do we have any way to "fake" the success of this call?
I have a lot of code that depends on the success of this call and I don't know how to test it.
Cheers,
Laurent
There is an example in the SiteRegisterController class that is provided out of the box with Force.com Sites. Here is the test method code:
Thanks for the answer.
I have read this example and it actually is a good example to demonstrate my point.
If you launch this test the test coverage is only 86% even though this class is really small. This is because the Site.createPortalUser will always return null, so the rest of the class is never tested.
Obviously this is not a good solution and we would need a way:
- either to "fake" the success of the method
- or to make createPortalUser works in a test environment.
I have tried to use System.runAs(SiteUser) but without success.
Cheers,
Laurent
I would also very much like to be able to use this in my testing.
Here's how I solved this issue. It's a hack but that's what you have to do I guess.....
Make your "after insert" code just one line by sending everything to a global class. Then if you don't have a before update requirement, fake it so you can at least get test coverage.
Here's my trigger:
Here's my test class:
This got me 80% coverage so I can deploy.
While I understand your frustration it is not the case that you can't create a User in a test, you simply can't create a portal user using the Site.createPortalUser method.
My trigger is on the user object and fires after insert. In my test class I tried to create a user with my customer portal's profile id. When I inserted the user without an Id there was no error but I got 0% coverage. When I added the profileId and tried the insert I got an invalid cross-reference id. I can insert a user with standard user profiles, just not profles that use the customer portal license. So that's why I went the route that I did. Does anyone have code that manually creates a customer portal user inside a test class?
Try this: