Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 05, 2020 @ 16:04
    Ismail Mayat
    0

    Tdd with IContent and models builder

    I have a test suite that tests logic of a class that validates IContent object based on its property values. All works fine.

    In the class under test to get property values I am using magic strings, ideally I want to use models builder to get the property so instead of:

    alertContent.GetValue("units")
    

    I want to do:

    alertContent.GetValue(Alert.GetModelPropertyType(x=>x.Units).PropertyTypeAlias)
    

    However trying to do that causes the tests to blow up with exception

    System.NullReferenceException : Object reference not set to an instance of an object.
    

    So is there a way of stopping that?

    Regards

    Ismail

  • Matthew Wise 271 posts 1373 karma points MVP 4x c-trib
    Jun 08, 2020 @ 15:45
    Matthew Wise
    0

    I have the same issue in v8 using hangfire.

    When creating the task I can get this alias. However when the enqueued job runs I get a null reference exception

    Product.GetModelPropertyType(x => x.Sku).Alias;
    

    Stack trace (not much)

    Object reference not set to an instance of an object.
     at Umbraco.ModelsBuilder.Embedded.PublishedModelUtility.GetModelContentType(PublishedItemType itemType, String alias)
    
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 08, 2020 @ 15:56
    Ismail Mayat
    100

    Matt,

    So I worked around this. Again the beauty of tdd. What it exposed is in my validator class i should not be passing in IContent. Reason is my class then is doing more than one thing i.e getting values then validating. Its responsibility should only be validation.

    So bit of refactor now my method looks like:

      public  ValidationResult Validate(IAlert alertContent)
    

    Now in my event I can create new IAlert and map properties using modelsbuilder. However in my test I can pass in IAlert I have created myself with know values although I get those values via IContent mock but the aliases are magic strings. Not ideal but better than before.

    Ismail

  • Matthew Wise 271 posts 1373 karma points MVP 4x c-trib
    Jun 08, 2020 @ 16:14
    Matthew Wise
    0

    In v8 I set in my background task once I had used an IUmbracoContextFactory create me a context.

    Current.UmbracoContextAccessor.UmbracoContext = umbracoContext.UmbracoContext;
    
Please Sign in or register to post replies

Write your reply to:

Draft