2
\$\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\$

2 Answers 2

1
\$\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\$
3
  • \$\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\$ Feb 18, 2016 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, 2016 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\$ Feb 20, 2016 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .