Skip to content

TASK: DefaultPortalAlias unit test#1769

Closed
johnhenley wants to merge 30 commits intoDNNCommunity:devfrom
johnhenley:experimental/portalsettings-defaultportalalias-usertest
Closed

TASK: DefaultPortalAlias unit test#1769
johnhenley wants to merge 30 commits intoDNNCommunity:devfrom
johnhenley:experimental/portalsettings-defaultportalalias-usertest

Conversation

@johnhenley
Copy link
Copy Markdown
Collaborator

Adding unit tests and they are failing when portalsettings.DefaultPortalAlias is accessed.
Here is a simple example to demonstrate

@johnhenley johnhenley assigned johnhenley, bdukes and valadas and unassigned johnhenley Feb 5, 2026
@johnhenley
Copy link
Copy Markdown
Collaborator Author

@valadas @bdukes want to get your help if possible. I am trying to add some unit tests to the forums module. I've successfully added a bunch over the years and have stumbled my way through setting up the required mocks and DI containers to support the tests that access portal, user, module, etc. entities as required for some tests.

However, I'm stumped on this one. No matter what I try in the TestBase.cs setup, I always get a NullReference Exception when accessing DefaultPortalAlias in PortalSettings. Since some of the key code in the forums module accesses this property, I want to ensure unit tests around that code but can't until I can figure this out....

Here's a screenshot of the exception:
image

When I look at the platform code, I see this:
image

And in my TestBase, I'm setting up:
image

image

Any help greatly appreciated !

@valadas
Copy link
Copy Markdown
Member

valadas commented Feb 5, 2026

Unfortunately some of those DNN objects are not easy to mock as they lack proper abstractions in DNN. It is something I would like to contribute to DNN when I have some time.

One of the ways I have been dealing with this in the past is to build an adapter that my code uses in order to call those external hard to test things. See https://github.com/DNNCommunity/DNN.UserVoice/blob/develop/Adapters/UserControllerAdapter.cs for an example. DNN does have IUserInfo but most APIs return UserInfo instead. So this does exactly call the DNN implementation but returns the IUserInfo abstration. Then in my tests, I call the easy to mock IUserControllerAdapter instead of the real UserController so that I can mock easily.

Once we have proper abstractions for TabInfo and ModuleInfo, I plan to contribute this https://github.com/DNNCommunity/DNN.UserVoice/blob/develop/Controllers/Context/DnnRequestContext.cs to DNN so make it really easy to get all that information out of anything that has an httpContext. Then you can request IDnnRequestContext anywhere to receive all the very popular DNN stuff in an very easy to mock way...

@johnhenley
Copy link
Copy Markdown
Collaborator Author

Unfortunately some of those DNN objects are not easy to mock as they lack proper abstractions in DNN. It is something I would like to contribute to DNN when I have some time.

One of the ways I have been dealing with this in the past is to build an adapter that my code uses in order to call those external hard to test things. See https://github.com/DNNCommunity/DNN.UserVoice/blob/develop/Adapters/UserControllerAdapter.cs for an example. DNN does have IUserInfo but most APIs return UserInfo instead. So this does exactly call the DNN implementation but returns the IUserInfo abstration. Then in my tests, I call the easy to mock IUserControllerAdapter instead of the real UserController so that I can mock easily.

Thanks @valadas. I was hoping there was an easy solution since DefaultPortalAlias is a property of PortalSettings and should return a value which is the case for other properties like PortalID. I will add a method (GetDefaultPortalAlias) to deal with it so it can be testable.

@johnhenley
Copy link
Copy Markdown
Collaborator Author

Replaced by PR #1798

@johnhenley johnhenley closed this Feb 25, 2026
@johnhenley johnhenley self-assigned this Feb 25, 2026
@johnhenley johnhenley added tasks General maintenance tasks that aren't typical development technical debt Issue that doesn't directly affect usability but will improve technical debt posture labels Feb 25, 2026
@johnhenley johnhenley added this to the 09.06.03 milestone Feb 25, 2026
@johnhenley johnhenley modified the milestones: 09.06.03, 09.07.00 Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tasks General maintenance tasks that aren't typical development technical debt Issue that doesn't directly affect usability but will improve technical debt posture

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TASK: Unable to create unit test that accesses PortalSettings.DefaultPortalAlias

3 participants