November Happy Hour will be moved to Thursday December 5th.

Unit testing auto setting of shortcut properties

Vote:
 

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)        
        {            
            base.SetDefaultValues(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

#79030
Dec 06, 2013 16:21
Vote:
 

Hi

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.

Regards

Per Gunsarfs

#79036
Dec 06, 2013 16:52
Vote:
 

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

#79066
Dec 09, 2013 10:17
Vote:
 

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?

 

#79246
Dec 11, 2013 16:12
Vote:
 

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)
}

page.SetDefaultValues(contentType);

    

#79249
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.