This is a migrated thread and some comments may be shown as answers.

Arranged static method is not arranged on different thread

3 Answers 161 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Todd K
Top achievements
Rank 1
Todd K asked on 18 May 2012, 12:13 AM
I am trying to test my application that uses Tasks. I have arranged a static method, if I call that method right after arranging it, I get the Mocked return value. But, if I call the method from a task, the method is not mocked.
I am running on version 2011.2.713.2  as my subscription has run out, and since this is a static method, I cannot use the free edition.

I do have JustMock enabled in the VS menu Telerik->JustMock->Enable JustMock
I managed to reproduce the problem with a simple example:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
  
namespace TestMockingStatic
{
   public class ClassWithStatic
   {
      public static int NumGot = 0;
      public static int GetANumber(int foo, int bar)
      {
         throw new NotImplementedException();
      }
   }
     
   [TestClass]
   public class UnitTest1
   {
      public void GetValue()
      {
         Task.Factory.StartNew(() =>
         {
            ClassWithStatic.NumGot = ClassWithStatic.GetANumber(5, 10);
         });
      }
  
      [TestMethod]
      public void TestMethod1()
      {
         Mock.SetupStatic<ClassWithStatic>(Behavior.Strict);
         Mock.Arrange(() => ClassWithStatic.GetANumber(Arg.AnyInt, Arg.AnyInt)).Returns(42);
  
         Assert.AreEqual(42, ClassWithStatic.GetANumber(5, 10), "Mocked Method did not return expected result");
  
         GetValue();
         SpinWait.SpinUntil(() => ClassWithStatic.NumGot > 0, 20000);
  
         Assert.AreEqual(42, ClassWithStatic.NumGot, "Mocked method called via TASK, did not return correct value");
      }
   }
}

Any help would be great,
Todd

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 23 May 2012, 12:13 AM
Hi Todd,

Thanks again for reporting the issue. 

We have identified the problem and logged an issue for it into our system. However, should you need the fix urgently please open a support ticket where I will send you the fix asap.


Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Mark
Top achievements
Rank 1
answered on 19 Jul 2013, 12:39 PM
Have you guys solved this?  I'm having the same problem.

0
Kaloyan
Telerik team
answered on 22 Jul 2013, 10:13 AM
Hi Mark,

With the current version of JustMock it is not possible to keep expectations for static members/methods against multi-threaded scenarios.

This is into our backlog for future release. Please, check this item from the JustMock Ideas and Feedback portal in order to vote for its faster implementation.

To assist you further, I would suggest the using of a singleton class:
public class Singleton
{
    private static Singleton myInstance = new Singleton();
    private static object syncRoot = new Object();
 
    private Singleton() { }
 
    public static Singleton GetInstance
    {
        get
        {
            if (myInstance == null)
            {
                lock (syncRoot)
                {
                    if (myInstance == null)
                        myInstance = new Singleton();
                }
            }
            return myInstance;
        }
    }
 
    public int NumGot = 0;
 
    public int GetANumber(int foo, int bar)
    {
        throw new NotImplementedException();
    }
}
 
 
[TestClass]
public class UnitTest1
{
    public void GetValueSingleton()
    {
        Task.Factory.StartNew(() =>
        {
            var mySingletonInstance = Singleton.GetInstance;
            mySingletonInstance.NumGot = mySingletonInstance.GetANumber(5, 10);
        });
    }
 
    [TestMethod]
    public void TestMethodSingleton()
    {
        var mySingleton = Singleton.GetInstance;
        Mock.Arrange(() => mySingleton.GetANumber(Arg.AnyInt, Arg.AnyInt)).Returns(42);
 
        Assert.AreEqual(42, mySingleton.GetANumber(5, 10), "Mocked Method did not return expected result");
 
        GetValueSingleton();
        SpinWait.SpinUntil(() => mySingleton.NumGot > 0, 20000);
 
        Assert.AreEqual(42, mySingleton.NumGot, "Mocked method called via TASK, did not return correct value");
    }
}

The above scenario works as expected.

I hope this helps.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Todd K
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Mark
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or