0
\$\begingroup\$

I need to create some tests for a bunch of variables that are initialized via the editor, So in the script they are always null and can't be set publicly.

Usually using the tag is a good way to encapsulate the code but with tests how do I check such a variable? Example:

public class thing{
    [SerializeField]
    private string line;

    //constructor(s), methods, etc..    

    public void setLine(string text){line = text; }
}

How do I check the value of the string after it is set inside a test?
(preferably without creating a getter method)

\$\endgroup\$
0
\$\begingroup\$

I ended up creating a constructor for each class that i wanted to test. So for the example above it would be:

thing(string line){
    //this will be the value of a [SerializeField] variable
    this.line = line;

    //ensure that start is called just like it would in the game
    Start();
}

Then in a test I just initialize the class with this value. This works out well as you can integrate all the start method stuff into the initialization which saves you calling it separately within the test.

\$\endgroup\$
0
\$\begingroup\$

Your initial code is initialized through the inspector, unless you call your function in void Update() the value will always be null or its initial value no matter what you put in the inspector.

\$\endgroup\$
  • \$\begingroup\$ I think you might have misunderstood. The question is about tests that are made inside unity, These aren't initialized the same way so there is a need to manually set the variables that the script expects. Unless what yous saying is that once I call update all the values are taken from the current scene that the script is in (which I image would only cause confusion)? \$\endgroup\$ – user3797758 Feb 18 '16 at 16:53
  • \$\begingroup\$ I thought you wanted to change values in an active scene with your function, and I said that it would not work unless called in unity's functions such as start, awake, update, etc. So I probably didn't understand correctly. @user3797758 \$\endgroup\$ – Valamorde Feb 19 '16 at 8:13
  • \$\begingroup\$ I'm talking about unit testing, you can google it (there is lots of info out there) but here is an article related to unity: blogs.unity3d.com/2014/07/28/… \$\endgroup\$ – user3797758 Feb 20 '16 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.