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

Unable to mock Static method in the given example,

1 Answer 102 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nikhil
Top achievements
Rank 1
Nikhil asked on 02 Oct 2013, 06:46 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;

namespace ClassLibrary1
{

    [TestClass]
    public class AsyncTests
    {
        [ClassInitialize]
        public static void ClassIntialization(TestContext t)
        {
            Mock.SetupStatic(typeof(StaticClass), StaticConstructor.Mocked);
            Mock.Arrange(() => StaticClass.Get1()).Returns(1);
        }

    
        [TestMethod]
        public async Task SecondTest()
        {
            var svc = new TestAsync();           
            var r = await svc.DoSomethingAsync();
            Assert.AreEqual(1, r);
            Assert.AreEqual(1, StaticClass.Get1());
        }
    }


    public class TestAsync
    {
         public Task<int> DoSomethingAsync()
       // public int DoSomethingAsync()
        {
            return Task<int>.Factory.StartNew(() => {
                
                var i = Get();
                var j = StaticClass.Get1();

                return j;
            
            });
        }

        public int Get()
        {
            return 2;
        }

    }

    public static class StaticClass
    {

        public static int Get1()
        {
            return 20;
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 02 Oct 2013, 07:49 AM
Hello Nikhil,

I've just replied to you in ticket with ID: 742864, as follows:
  • 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.

Let me know if I can assist you further.

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
Nikhil
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or