Reputation: 7542
I am working on a unit test where I want to assert that the object coming back is the same as the model.
var actual = scope.InstanceUnderTest.GetContent(expectedId);
var newFileStreamer = new FileStreamer(scope.TestDocument.Data, "application/octet-stream");
Assert.IsTrue(actual.Result.Equals(newFileStreamer));
actual.Result appears to be one level away from the object that is the same as newFileStreamer:
How do I get access to the inner filestream object to check if they are the same?
Upvotes: 0
Views: 89
Reputation: 605
It is not one level away, the debugger shows it that way because it performs a cast to the correct type. If you right-click on the second-node and select add-watch you will see that the expression watched contains a cast.
To check for the internals you could use reflection. Note that is not recomended to test for private field in unit testing, but if you really want to, here is some code:
var actual = scope.InstanceUnderTest.GetContent(expectedId);
var type = actual.Result.GetType();
var dataField = type.GetField("_data",
BindingFlags.NonPublic |
BindingFlags.Instance);
var contentTypeField = type.GetField("_contentType",
BindingFlags.NonPublic |
BindingFlags.Instance);
Assert.IsTrue(dataField.GetValue(actual.Result) == scope.TestDocument.Data);
Assert.IsTrue(contentTypeField.GetValue(actual.Result) == "application/octet-stream");
Upvotes: 3
Reputation: 386
Based on the comments it seems like this should be fine:
if( (FileStreamer)actual.Result == newFileStreamer) {
//do work here
}
Or I guess
if( (actual.Result as FileStreamer) == newFileStreamer) {
//do work here
}
Upvotes: 0
Reputation: 5487
The default behavior for .Equals() for reference types is to check for reference equality (i.e. do they point to the same object on the heap). Since one of the two objects you instantiate just before your assert then there is no way they would ever be considered the same object (since there is no way for another object to get a reference to newFileStreamer between its instantiation and the assert).
If you have not yet provided an overload for .Equals for your fileStreamer type then you can solve your problem by doing so and stating within the method what exactly would constitute being equal.
Upvotes: 1