- Home /
Nunit NotNull assert strangeness
Hello!
I'm started using Nunit.framework to do my tests - for example I check if everything is assigned properly in Awake():
public GameObject test;
// Use this for initialization
void Start () {
Debug.Log(test == null);
Assert.IsNull(test);
}
the variable test is not assigned, so the two printouts for these statements are first true (as expected) but the second is :
AssertionException: Expected: null
But was: <null>
What does this mean? Is there some other type of null?
If I now explicitly set test to null, it all just works as expected, which let me to believe there is a difference between not assigning something in the editor and it being null.
Thanks for any clarification!
Seb
I'd start by searching the plugin documentation. Questions about plugins are generally best directed to the author or any community they offer. null is null, there's no special type of null. I couldn't guess at what the issue is without the documentation.
I should have added that (I'll edit the question), if I explicitly assign null like so: test = null; it works as expected.
What does the documentation have to say about this problem? Sorry to say, it's fairly unlikely to get plugin help here unless it's a very prolific plugin.
Answer by Landern · Dec 31, 2014 at 07:32 AM
In the back end(unity implementation) the gameobject isn't techincally null. They override the operators(!=, == .Equals, etc) to derive a value that is meaningful through your implementation, but if Assert.IsNull is literally taking the Object and checking null, you will be surprised. You can see this by adding a breakpoint into a location in your script/code that you assume an unassigned gameobject should be null, but you will find it isn't in the sense that it will just display null, it will have property like it was instantiated and all that jazz. Do it, check it out.
Behold a screen:
You can see where this has been asked previously:
http://answers.unity3d.com/questions/524998/testing-for-null-bug-or-feature.html
http://forum.unity3d.com/threads/fun-with-null.148090/
http://answers.unity3d.com/questions/794152/gameobject-not-null-before-instantiate.html
http://forum.unity3d.com/threads/null-check-inconsistency-c.220649/
Hi Landern, thanks a lot for the answer. Wow. How interesting - and confusing! :) I'll have to change my sanity functions ..
Thank you for this! This might be obvious, but if anybody else runs into this like I just did, here is a simple workaround to use ins$$anonymous$$d of Assert.IsNull:
Assert.IsTrue(objectThatShouldBeNull == null);
Your answer
Follow this Question
Related Questions
Debugging unit tests with Monodevelop 0 Answers
The Assembly is referenced, But the dll is not allowed to be included or could not be found 0 Answers
Marking a directory as a development dependency 1 Answer
Unity NavMeshComponents Errors 1 Answer
Run unit tests (nUnit) in debug mode (MonoDevelop) 0 Answers