Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returns not working on UdpClient.Receive() method. #439

Closed
mnicic opened this issue Sep 8, 2017 · 2 comments
Closed

Returns not working on UdpClient.Receive() method. #439

mnicic opened this issue Sep 8, 2017 · 2 comments

Comments

@mnicic
Copy link

mnicic commented Sep 8, 2017

Returns not working on UdpClient.Receive() method.

Class code:

var remoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
var receiveBytes = this.udpClient.Receive(ref remoteEndPoint);
var responseData = Encoding.ASCII.GetString(receiveBytes);

Unit testing code:

var remoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
var b = Encoding.ASCII.GetBytes("responseData");
udpClientMock.Setup(x => x.Receive(ref remoteEndPoint)).Returns(b);

Nothing is returned (byte[]). Exception is "The following setups were not matched:
IUdpClientExtended x => x.Receive(0.0.0.0:0)". It is also problem with replacing Returns with Throws.

With Strict mode, the error says: "Invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup."

@stakx
Copy link
Contributor

stakx commented Sep 8, 2017

I am not sure that I understand how your two repro code blocks are supposed to work and be related to one another. It seems incomplete. Could you please post a minimally complete code example that leads to the error message you're describing?

@stakx
Copy link
Contributor

stakx commented Sep 16, 2017

In absence of actual repro code, I will just guess you're doing something like the following (which produces the same error):

public interface IUdpClientExtended
{
    byte[] Receive(ref IPEndPoint endpoint);
}

var mock = new Mock<IUdpClientExtended>(MockBehavior.Strict);
var endpoint = new IPEndPoint(IPAddress.Any, 0);
mock.Setup(_ => _.Receive(ref endpoint)).Returns(Encoding.UTF8.GetBytes("Hello world"));

var differentEndpoint = new IPEndPoint(IPAddress.Any, 0);
var response = mock.Object.Receive(ref differentEndpoint);

This doesn't work because the instance of IPEndPoint you're passing to your mock (differentEndpoint) is not the exact same instance that you used during the mock setup (even though both have the same value).

AFAIK, Moq doesn't currently support It.IsAny<T> for ref parameters. So the solution here is either of these:

  • Change the definition of IUdpClientExtended so that Receive's parameter is by value instead of ref.
  • Use the exact same instance of IPEndPoint to your mock that you're using during the setup.

If the above doesn't help you, or my repro code doesn't match your scenario, please report back and we can reopen this issue.

@stakx stakx closed this as completed Sep 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants