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

C# mocking the RemoteEndpointProperty(I think?)

4 Answers 197 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 11 Jul 2014, 08:23 PM
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;
        }

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 17 Jul 2014, 07:29 AM
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.

 
0
Matthew
Top achievements
Rank 1
answered on 17 Jul 2014, 12:42 PM
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.)
0
Matthew
Top achievements
Rank 1
answered on 17 Jul 2014, 12:51 PM
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. 
0
Stefan
Telerik team
answered on 18 Jul 2014, 07:09 AM
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.

 
Tags
General Discussions
Asked by
Matthew
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or