chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi there.

I'm trying to mock a call like this:

Public Shared Sub PrintAttributeValue(ByVal node As XElement)
        Console.WriteLine(node.Attribute("Id").Value)
    End Sub


So basically I want the value of node.Attribute("Id").Value to be changed by mocking it. My first attempt to do this was:

<TestMethod()> _
Public Sub PrintAttributeValueTest()

        Dim fakeXElementNode As XElement = FakeInstance(Of XElement)(Members.ReturnRecursiveFakes)

        Using TheseCalls.WillReturn("123")
            Dim dummy As String = fakeXElementNode.Attribute("Id").Value
        End Using

        MyXElementClass.PrintAttributeValue(fakeXElementNode)

End Sub


But I get this error:

Test method Unit_Test.XElementTestTest.PrintAttributeValueTest threw exception: TypeMock.TypeMockException:
*** You are using TheseCalls.ReturnValue incorrectly - Method get_Value in type System.Xml.Linq.XAttribute returns System.Xml.Linq.XName and not System.String.
Please use TheseCalls.WillReturn(System.Xml.Linq.XName)..


Even though XElement.Attribute("Id").Value is a String, I'm getting the above exception mentioning an XName object. As an experiment, I change the unit test to this:

<TestMethod()> _
    Public Sub PrintAttributeValueTest()

        Dim fakeXElementNode As XElement = FakeInstance(Of XElement)(Members.ReturnRecursiveFakes)
        Dim fakeXName As XName = FakeInstance(Of XName)(Members.ReturnNulls)

        Using TheseCalls.WillReturn("TestName")
            Dim dummay As String = fakeXName.LocalName
        End Using

        Using TheseCalls.WillReturn(fakeXName)
            Dim dummy As String = fakeXElementNode.Attribute("Id").Value
        End Using

        MyXElementClass.PrintAttributeValue(fakeXElementNode)
    End Sub


This time, I go this error:

Test method Unit_Test.XElementTest.PrintAttributeValueTest threw exception: TypeMock.TypeMockException:
*** You are using TheseCalls.ReturnValue incorrectly - Method get_Value in type System.Xml.Linq.XAttribute returns System.String and not System.Xml.Linq.XName.
Please use TheseCalls.WillReturn(System.String)..



I'm a bit stuck on what to do, has anyone else managed to mock XElement.Attribute().Value?

Cheers.
Jas.
asked by MrClyfar (5.2k points)

2 Answers

0 votes
I'll need a reproduction to further investigate this issue let's take it offline
answered by dhelper (11.9k points)
0 votes
Hi there.

I have tried sending you an email with a zip file of the test project, but I'm getting postmaster replies when sending emails to your address.

Instead, I've emailed the zip file to Gil if that's OK.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
...