1
votes

I'm writing some tests for data mapping that takes a ContentItem that has as part of it a UserPart. When I try to use it in my unit tests I and assign the email property I get InfoSet errors. How do I go about mocking up things so that the InfoSet will properly be filled in absent a database?

When attempted using Bertrand's ContentHelpers, as soon as I try to set UserPart's Email property I receive the following:

System.Reflection.TargetException occurred HResult=-2146232829
Message=Non-static method requires a target. Source=mscorlib
StackTrace: at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) at Orchard.ContentManagement.InfosetHelper.Store[TPart,TRecord,TProperty](TPart contentPart, Expression`1 targetExpression, TProperty value) in .....Orchard.Source.1.8\src\Orchard\ContentManagement\InfosetHelper.cs:line 122 InnerException:

Example of usage:

        ContentItem usrItem = ContentHelpers.PreparePart(new UserPart(), "User") ;
        UserPart usrPart = usrItem.As<UserPart>();
        usrPart.Email = "[email protected]";

Link to Bertrand's Blog Post showing ContentHelpers: http://weblogs.asp.net/bleroy/faking-orchard-content-items-for-testing

1
That's weird, it looks like the record is null. Can you attach a debugger and check that?Bertrand Le Roy

1 Answers

0
votes

As @Bertrand Le Roy suggested in his comment, the record is null.

You are constructing your content item like this:

ContentItem usrItem = ContentHelpers.PreparePart(new UserPart(), "User");

If you compare that to the way Bertrand describes the process in his Blog, you'll notice that in addition to creating a BodyPart, he is also creating it's underlying BodyPartRecord. This is required because data backed Parts, like the BodyPart and UserPart delegate most of their functionality down to the underlying PartRecord.

To fix your problem, you just need to change the way you construct your content item to include the construction of the underlying record:

ContentItem usrItem = ContentHelpers.PreparePart(new UserPart { 
                                                     Record = new UserPartRecord() 
                                                 }, "User");