- Home /
              This post has been wikified, any user with enough reputation can edit it. 
            
 
            Time.time and Unittesting can't work together?
Hi,
If a unit test is run from Monodevelop that relies on Time.time, this exception occurs:
 System.MissingMethodException : Cannot find the requested method
E.g. this code:
 [TestFixture]
 public class Tests
 {
     [Test]
     public void TestUnityTime()
     {
         var result = new Foo().UnityTime();
         Assert.Greater(result, 0);
     }
 }
 public class Foo 
 {
     public float UnityTime()
     {
         return Time.time;
     }
 }
To be fair it does make sense that Unity's time doesn't run during a unit test. But, is there a way we can test time-using code in Unittests then? Can I manually start the timer? Have you found a way to mock it? How can one unit test any code that uses Time.time?
Thanks
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by gaggle · Aug 03, 2015 at 12:40 AM
One way I'm trying now is to switch to this amazing alternative implementation of Time.time:
 public static class Time2
 {
     public static float time
     {
         get
         { 
             float time;
             try
             {
                 time = Time.time;
                 
             } catch (System.MissingMethodException)
             {
                 time = 0.0f;
             }
             return time;
         }
     }
 }
I guess that'll work.
It does though, System.$$anonymous$$issing$$anonymous$$ethodException is thrown when used in a unittest and run directly from $$anonymous$$onodevelop. Try running my post's code yourself. 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                