Carlos
Carlos

Reputation: 6021

Unit testing SocketAsyncEventArgs

I'm trying to set up a unit test of a method that takes a SocketAsyncEventArgs, ie one of the IOCP functions from the Sockets class.

In order to shove one of these classes into my function, I need to change the .BytesTransferred property of the SAEA. How does one do this? I tried to use a PrivateObject.SetFieldOrProperty, but it tell me it's unable to find the property:

pargs.SetFieldOrProperty("BytesTransferred", expected); 

I get the following exception: "Method 'System.Net.Sockets.SocketAsyncEventArgs.BytesTransferred' not found."

Upvotes: 1

Views: 364

Answers (1)

usr
usr

Reputation: 171236

This property does not have a setter. Two solutions:

  1. Don't use SocketAsyncEventArgs for testing. Instead, test on some kind of mock object.
  2. Set the private field m_BytesTransferred. Use reflector to look at implementation details.

Solution #2 would make me nervous. I'd go with #1.

Upvotes: 3

Related Questions