Question

When I attempt the following JustMock:

var mockedField = Mock.CreateLike<Field>(field => field.DisplayName == "Test Display");

I get the following error:

   at Sitecore.Data.Fields.Field.GetValue(Boolean allowStandardValue, Boolean allowDefaultValue)
   at Sitecore.Data.Fields.Field.get_Value()
   at Sitecore.Data.Fields.Field.ToString()
   at System.Linq.Expressions.ExpressionStringBuilder.VisitConstant(ConstantExpression node)
   at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionStringBuilder.OutMember(Expression instance, MemberInfo member)
   at System.Linq.Expressions.ExpressionStringBuilder.VisitMember(MemberExpression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionStringBuilder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Linq.Expressions.ExpressionStringBuilder.ExpressionToString(Expression node)
   at System.Linq.Expressions.Expression.ToString()
   at Telerik.JustMock.Core.ExpressionUtil.ConvertMockExpressionToString(Expression expr)
   at Telerik.JustMock.Core.MocksRepository.Arrange[TMethodMock](Expression expr, Func`1 methodMockFactory)
   at Telerik.JustMock.Mock.<>c__DisplayClassa`1.<Arrange>b__8()
   at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
   at Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression)
   at Telerik.JustMock.Expectations.ReturnArranger.ArrangeReturn[T](Expression`1 callPattern, LambdaExpression returnValueExpr)

This seems to be because it's attempting to cast the "Field" class as a string. The "ToString" of the "Field" class is overwritten with logic that will not work in the test environment. So, naturally I tried to override the "ToString" method for all instances of Field:

var fieldMocker = Mock.Create<Field>();
Mock.Arrange(() => fieldMocker.ToString()).IgnoreInstance().Returns("Override To String");

I verified that it is working when I create instances of "Field", but for some reason the original "ToString" is being called when I attempt to setup the Mock.

Any Ideas?

Was it helpful?

Solution

The field.ToString() in this case happens to be called within the DMZ, or the De-Mockerized Zone. Hence, the arrangements you made are not respected. It definitely looks like a bug, so I've logged the issue.

Thanks for reporting the bug. Unfortunately, I can't offer you any workarounds (except to not use CreateLike for now), so I'll get to fixing it ASAP.

Update: the bug fix is checked in. It will be made officially available in the JustMock 2015 Q1 release or an internal build before that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top