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
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.
That really useful information. Thank you. I think manually calling GetDefault would be the way to go for me.
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?
In a unit test I would try to simply instatiate a new one, and set any required properties on it.
var contentType = new ContentType()
Name = "SamplePageType";
ModelType = typeof(SamplePage)