This is a migrated thread and some comments may be shown as answers.

SerializationException with ReadXml and mock IPrincipal

5 Answers 48 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
paullinville@hotmail.com
Top achievements
Rank 1
paullinville@hotmail.com asked on 23 Oct 2012, 05:35 PM

I have Visual Studio 2010 and Visual Studio 2012 installed. Using JustMock version 2012.3.1016.3

Running the tests below I get the following in both VS 2010 and 2012.

Test Name: TestReadXML
Test FullName: DemoSerializationExceptionMock.ReadXMLMockIBusinessPrincipal_Tests.TestReadXML
Test Source: C:\Projects\MAS Applications\Experimental\Source\DemoSerializationExceptionMock\DemoSerializationExceptionMock\ReadXMLMockIBusinessPrincipal_Tests.vb : line 47
Test Outcome: Failed
Test Duration: 0:00:00.00937

Result Message: 
Test method DemoSerializationExceptionMock.ReadXMLMockIBusinessPrincipal_Tests.TestReadXML threw exception:
System.Runtime.Serialization.SerializationException: Type is not resolved for member 'IPrincipalProxy+bce4039c8c9143c29217f51daf99a9a8,Telerik.JustMock, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b221631f7271365'.
Result StackTrace: 
at System.AppDomain.get_Evidence()
   at System.AppDomain.get_EvidenceNoDemand()
   at System.AppDomain.get_Evidence()
   at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName)
   at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Xml.XmlConfiguration.XmlReaderSection.get_ProhibitDefaultUrlResolver()
   at System.Xml.XmlReaderSettings.GetXmlResolver_CheckConfig()
   at System.Xml.Schema.XmlSchemaSet.PreprocessSchema(XmlSchema& schema, String targetNamespace)
   at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, XmlSchema schema)
   at System.Xml.Schema.XmlSchemaSet.Add(XmlSchema schema)
   at System.Xml.Schema.XmlSchemaInference.CreateXmlSchema(String targetNS)
   at System.Xml.Schema.XmlSchemaInference.AddElement(String localName, String prefix, String childURI, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, Int32 positionWithinCollection)
   at System.Xml.Schema.XmlSchemaInference.InferSchema1(XmlReader instanceDocument, XmlSchemaSet schemas)
   at System.Xml.Schema.XmlSchemaInference.InferSchema(XmlReader instanceDocument)
   at System.Data.DataSet.InferSchema(XmlDocument xdoc, String[] excludedNamespaces, XmlReadMode mode)
   at System.Data.DataSet.ReadXml(XmlReader reader, Boolean denyResolving)
   at System.Data.DataSet.ReadXml(TextReader reader)
   at DemoSerializationExceptionMock.ReadXMLMockIBusinessPrincipal_Tests.TestReadXML() in C:\Projects\MAS Applications\Experimental\Source\DemoSerializationExceptionMock\DemoSerializationExceptionMock\ReadXMLMockIBusinessPrincipal_Tests.vb:line 43

 

Imports Microsoft.VisualStudio.TestTools.UnitTesting.Web
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Telerik.JustMock
Imports System.Security.Principal
 
<TestClass()> _
Public Class ReadXMLMockIBusinessPrincipal_Tests
    Private testContextInstance As TestContext
 
    Public Property TestContext As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal Value As TestContext)
            testContextInstance = Value
        End Set
    End Property
 
    <TestInitialize()>
    Public Sub TestInit()
        Dim idnty As IIdentity = Mock.Create(Of IIdentity)()
        Mock.Arrange(Function() idnty.AuthenticationType).Returns("MOCK")
        Mock.Arrange(Function() idnty.IsAuthenticated).Returns(True)
        Mock.Arrange(Function() idnty.Name).Returns("mockident")
        Dim prince As IPrincipal = Mock.Create(Of IPrincipal)()
        Mock.Arrange(Function() prince.Identity).Returns(idnty)
        Mock.Arrange(Function() prince.IsInRole(Arg.AnyString())).Returns(True)
        System.Threading.Thread.CurrentPrincipal = prince
    End Sub
 
    Dim elem As XElement = <Root>
                           </Root>
 
    <TestMethod()> _
    Public Sub TestReadXML_IgnoreSchema()
        Dim ds As New DataSet
        ds.ReadXml(New IO.StringReader(elem.ToString), XmlReadMode.IgnoreSchema) '<- Passed
    End Sub
 
    <TestMethod()> _
    Public Sub TestReadXML()
        Dim ds As New DataSet
        ds.ReadXml(New IO.StringReader(elem.ToString)) '<-Fails
    End Sub
End Class            

5 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 26 Oct 2012, 02:13 PM
Hi Paul,

Thanks again for sending the sample. 

I found out that your second test is somehow trying to serialize the JustMock assembly. Since I have  no control over the source. The best will be to mock ReadXml itself to return your expected XmlReaderNode. As this is a .net assembly we quietly possible don’t know whats going inside or tweak our tests based on that.

 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
paullinville@hotmail.com
Top achievements
Rank 1
answered on 29 Oct 2012, 03:41 PM
Of course this is the free edition so I cannot mock the ReadXML.  Plus this is a test created to  just for you, Telerik, to demo what is happening. In the real world ReadXml could be (and is) happening in many different places.

Is this something that will be fixed? Is this a bug that I should present to Microsoft? Maybe Telerik should present this bug to Microsoft?
0
Ricky
Telerik team
answered on 29 Oct 2012, 09:34 PM
Hi Paul,

Thanks again for your reply. 

However, this is not a bug of either Microsoft or Telerik. It is two different overloads that behave differently. In order to figure out what’s going on, you need to decompile the ReadXml source and see what specifically it is trying to call and mock that member.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
paullinville@hotmail.com
Top achievements
Rank 1
answered on 30 Oct 2012, 07:35 PM
As far as I can tell I have mocked all the properties of IPrinciple and IIdentity. Do you see one I have missed?
0
Ricky
Telerik team
answered on 01 Nov 2012, 02:54 AM
Hi Paul,

Thanks again for your reply. In order to figure this out, you need to decompile the method to browse the content and identify the call that is failing and update your test based on that. So far, if you notice your log then you will see that it is failing while trying to serialize the mock object. You need to mock the method that tries to serialize the mock object so that it is skipped.

Hope these steps give you some pointers.

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
JustMock Free Edition
Asked by
paullinville@hotmail.com
Top achievements
Rank 1
Answers by
Ricky
Telerik team
paullinville@hotmail.com
Top achievements
Rank 1
Share this question
or