Try our conversational search powered by Generative AI!

Unit testing auto setting of shortcut properties


Hi all

I'm trying to setup a page that sets the shortcut property automatically. I'm getting unexpected results from unit testing

The that sets the default shortcut is code is

        //Sets the default property values        
        public override void SetDefaultValues(ContentType contentType)        
            VisibleInMenu = false;
            this.LinkType = PageShortcutType.Shortcut;
            this[MetaDataProperties.PageShortcutLink] = ContentReference.StartPage;

And the unit test is

       public void Page_SetsNotNavigationOnPublish()
            SamplePage page = new SamplePage();
            Assert.True(folder.LinkType == PageShortcutType.Shortcut);

I'm expecting the shortcut type to by Sortcut bu tit is Inactive. Do I need to trigger a fake publish to get this to work?

Grateful as always for any hints, tips or insights

Tim B

Dec 06, 2013 16:21


The SetDefaultValues method is only called when new content is created through the content repository. I.e. someone calls IContentRepository.GetDefault. Since you are instantiating your page data directly, the SetDefaultValues will never be called.

My suggesting is that you simply call SetDefaultValues on your SamplePage explicitly in your test, before the Assert. Otherwise you would start to test the framework rather than your code, which you would like to avoid in a unit test.

The other option is of course to make a complete integration test, load an IContentRepository, and call GetDefault on that to get your SamplePage. But the would probably require quite a bit of setup.


Per Gunsarfs

Dec 06, 2013 16:52

That really useful information. Thank you. I think manually calling GetDefault would be the way to go for me.

Dec 09, 2013 10:17

Do you know how to call GetDefault with the ContentType parameter

At the moment I am calling as GetDefault(null) then doing a null check to stop it calling the base implementation. I guess it's good enough but feels hacky to me. Is there a better way to do it?


Dec 11, 2013 16:12

In a unit test I would try to simply instatiate a new one, and set any required properties on it.

Something like:

var contentType = new ContentType()
  Name = "SamplePageType";
  ModelType = typeof(SamplePage)



Dec 11, 2013 16:22
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.