C# mocking the RemoteEndpointProperty(I think?)

5 posts, 0 answers
  1. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 11 Jul 2014 Link to this post

    On another forum I found out how to mock the Operational context in this snippet

    what I want to do is mock the endpoint in it now, My unit test here needs to test all aspects and I am having issues actually mocking the endpoint

    truthfully I do not know what needs to be mocked or how but I know what I want the end result to be. I just need to be able to get endpoint to not be null and set
    retVal = endpoint.Address;
    Ive tried so many things that I could not get a result 

    The code:

            public static string GetCallerIPAddress()
            {
                string retVal = null;
                if (OperationContext.Current != null)
                {
                    MessageProperties prop = OperationContext.Current.IncomingMessageProperties;
                    RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                    if (endpoint != null)
                    {
                        retVal = endpoint.Address;
                    }
                }
                return retVal;
            }
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 17 Jul 2014 Link to this post

    Hi Matthew,

    Here is one way you can mock the endpoint address:
    public void Test()
    {
        var mockEndpoint = Mock.Create<RemoteEndpointMessageProperty>();
        Mock.Arrange(() => mockEndpoint.Address).Returns("1.2.3.4");
        var messageProperties = Mock.Create<MessageProperties>();
        Mock.Arrange(() => messageProperties[RemoteEndpointMessageProperty.Name]).Returns(mockEndpoint);
        Mock.Arrange(() => OperationContext.Current.IncomingMessageProperties).Returns(messageProperties);
     
        var ip = GetCallerIPAddress();
        Assert.Equal("1.2.3.4", ip);
    }

    If there's anything unclear in the above test method, or it's not working for you, don't hesitate to contact us again.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
  4. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 17 Jul 2014 in reply to Stefan Link to this post

    THANK YOU!!!!!!

    This is great. IT worked fine. The best thing is I was about 70% there I was just missing a few things that made it not work. I appreciate the response.

    On a side note, do I assume that it was hard to do when it took 5 days for a response?
     (I don't mean that in a bad way at all, I am just happy for a response and a working way to test this.)
  5. Matthew
    Matthew avatar
    16 posts
    Member since:
    Jun 2014

    Posted 17 Jul 2014 Link to this post

    Also would you guys have any tips to learning how to mock new things like this? it seems that who ever wrote this started from the inside out . which I never thought about doing. 
  6. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 18 Jul 2014 Link to this post

    Hi Matthew,

    I'm deeply sorry for being late with the response to your ticket. It is very rare for us to be this sloppy and let tickets linger for this long.

    The solution I gave you didn't take more than 5 minutes to write and test. It looks like it was written from the inside out, but it's not. I started writing it from the outermost part, OperationContext.Current, and as I delved deeper, I piled the newly mocked inner members at the top of the test method. Essentially, I wrote the code backwards, with new code getting inserted at the top.

    As an alternative to this style of writing, there is the expressive "mocking by example" approach, which produces code in a more top-down fashion:
    public void Test()
    {
        var operationContext = Mock.CreateLike<OperationContext>(oc =>
            oc.IncomingMessageProperties[RemoteEndpointMessageProperty.Name]
            == Mock.CreateLike<RemoteEndpointMessageProperty>(endpoint => endpoint.Address == "1.2.3.4"));
        Mock.Arrange(() => OperationContext.Current).Returns(operationContext);
     
        var ip = GetCallerIPAddress();
        Assert.Equal("1.2.3.4", ip);
    }

    You can mix and match the two styles to your liking. If you have any other questions regarding mocking, don't hesitate to ask us once again.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft R3 2016 release webinar banner