1

I'm using the WebFormsMvp framework to do TDD while developing a DNN Module (DNN 6.1).

I'm following the most recent tutorials I can find, but have run into issues with DNN's ModuleInstanceContext class. E.g., if I try to call ModuleContext.EditUrl in my presenter, unit tests fail (running the module for real doesn't fail) because the ModuleInstanceContext has dependencies that resolve to a concrete instance of HttpContext and/or want to make actual Db calls (to fetch PortalAlias and the like).

Is there a best practice within the DNN community for unit testing when calls to methods on the ModuleInstanceContext are necessary?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239

1 Answers1

0

In those cases, I've created an NavigationService class which I initialize with the context in the presenter's constructor. For example:

public MyPresenter(IMyView view) : this(view, null) {}
internal MyPresenter(IMyView view, INavService navService) {
    this.navService = navService ?? new DnnNavService(this.ModuleContext);
}

If I need to access the querystring from the navigation service, it's not initialized yet in the constructor, so I pass in a Lazy<NameValueCollection> pointing to it instead.

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • I don't quite get your reference to the querystring, since my question is in regard to calling the ModuleContext.EditUrl function in code that is being unit tested, and is not related to accessing the querystring. But I think what you are saying is that I should create a wrapper class around the ModuleInstanceContext, and then that wrapper class can implement an interface which I can mock in my unit tests. Correct? – E Michael Bradley Aug 07 '12 at 18:50
  • Right. I was just adding a note that if you create a "NavigationService", it might be interested in the querystring (either to propagate parameters, or if it reads the querystring in addition to navigating), and it's not 100% straightforward to reference it in the constructor. – bdukes Aug 07 '12 at 18:54