Unable to Mock the Generic Constraint in static class

17 posts, 0 answers
  1. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 03 Apr 2013 Link to this post

    In the following code unable to mock the static class- Thanks in advance for kind act
    [TestClass]
       public class FooTest
       {
           [TestMethod]
           public void Test_MockStaticClass()
           {
        //By no means able to Mock the below static class
            Mock.SetupStatic(typeof(Foo), Behavior.Loose, StaticConstructor.Mocked);//gives error of constraint
              
           }
       }
     
       public static class Foo
       {
           static Foo() { }
           public static List<T> AllFoos<T>() where T : Base, ICloneable
           {
               return new List<T>();
           }
       }
       [Serializable]
       [DataContract]
       public abstract class Base
       {
           public Base() { }
       }
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 08 Apr 2013 Link to this post

    Hello Mohd,

    Thank you for reporting this issue.

    It appears, this is a bug in JustMock. Our development team is currently working on it. A fix should be shipped with one of the next future releases.

    However, as a workaround, I will suggest to use direct Mock.Arrange() instead of the Mock.SetupStatic(). I prepared an example, which follows:
          public static class Foo
        {
            static Foo() { }
            public static List<T> AllFoos<T>() where T : Base, ICloneable
            {
                return new List<T>();
            }
        }
     
        [Serializable]
        [DataContract]
        public abstract class Base
        {
            public Base() { }
        }
     
        public class Wrapper: Base, ICloneable
        {
            public string Name;
     
            public object Clone()
            {
                throw new NotImplementedException();
            }
        }
    [TestClass]
    public class FooTest
    {
        [TestMethod]
        public void Test_MockStaticClass()
        {
            Mock.Arrange(() => Foo.AllFoos<Wrapper>()).ReturnsCollection(new List<Wrapper>() { new Wrapper() { Name = "Test" } });
     
            var actual = Foo.AllFoos<Wrapper>();
     
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual("Test", actual[0].Name);
     
        }
    }

    I hope this helps.

    P.S. For reporting this issue, we have granted you some Telerik points.

    All the best,
    Kaloyan
    the Telerik team
    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.
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 May 2013 Link to this post

    Hello again Mohd,

    We managed to fix this issue. However, the fix won`t be included in the planned for tomorrow JustMock Q2 2013 SP2 release.

    What we can offer you at this point is an internal JustMock build, containing the fix and many other improvements. However, the internal build is fully supported by us and any feedback about its behavior will be highly appreciated.

    If you decide to give the internal JustMock build a try, you will need to open a support ticket on that thread.  There we will be able to send it to you.

    I hope this helps.

    Kind regards,
    Kaloyan
    the Telerik team
    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.
  5. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 07 May 2013 Link to this post

    Hello Kaloyan,
    Can you send me the internal build so that I can test my code. There are similiar classes on the same issue. I want check them as well.
    But I notice one thing, if profiler is off, then issues is not coming, I tested one private method.
    But other private method which has static call inside, profiler get on. This issues again comes up.

    If you can send me trial build, my trial support expired, have been using my team member support poonam.rane@allscripts.com
    Once the order is executed, then we would have support.
    Do let me know the way out to test my issues.

    Thanks
    Moyeen
  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 08 May 2013 Link to this post

    Hello Moyeen,

    I have sent the private build to the address provided.

    I hope it helps. Any feedback will be highly appreciated.

    Greetings,
    Kaloyan
    the Telerik team
    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.
  7. Poonam
    Poonam avatar
    2 posts
    Member since:
    Apr 2013

    Posted 09 May 2013 Link to this post

    Hi Kaloyan,
    Thanks for the new build,I installed the new build. Build version Q1 2013 SP1 (2013.1.508.0), is it still SP1
    Is the build correct, I was expecting something Q2 2013 SP2 

    I try to run the same test case, asking to turn the Profiler On, I am doing that as well.
    Still unable to run the test case. As I told you we have various classes of this type, we are getting the same error. I will try to make sample out of those, and would send you.

    Do let me know, why the profiler is getting turn off despite making it turn on from the menu.


    [TestClass]
        public class FooTest
        {
            [TestMethod]
            public void Test_MockStaticClass()
            {
                //By no means able to Mock the below static class
                Mock.SetupStatic(typeof(Foo), Behavior.Loose, StaticConstructor.Mocked);//gives error of constraint
     
            }
        }
     
        public static class Foo
        {
            static Foo() { }
            public static List<T> AllFoos<T>() where T : Base, ICloneable
            {
                return new List<T>();
            }
        }
        [Serializable]
        //[DataContract]
        public abstract class Base
        {
            public Base() { }
        }


    Thanks,
    Mohd Moyeen
  8. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 09 May 2013 Link to this post

    Hi Moyeen,

    Yes, this is the correct build. As it is internal, it is still with the old version string.

    I tried the very same test on my machine with the same internal JustMock build. It passes.

    I would suggest you to try the following:
    1. Completely uninstall JustMock from your machine.
    2. Delete the JustMock root folder (ex.: C:\Program Files (x86)\Telerik\JustMock).
    3. Install JustMock Q1 2013 SP1 2013.1.508.0 (the internal build).
    4. Open your project and change the JustMock references in it with the newly deployed.
    5. After rebuilding the solution, try to run your tests again.

    If the issue still remains after the above steps, we would need a copy of the exception stacktrace so we could investigate it further.

    Please notify us with the results. Thanks in advance.

    Regards,
    Kaloyan
    the Telerik team
    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.
  9. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 10 May 2013 Link to this post

    Hello Kaloyan,
    Thanks for detail step provided to use the new build. Test case running successfully.
     I have couple of existing test cases related to same issue. I noted couple of points with new Build.
    First, the new Build taking around over two minutes to execute the one test case. 
    Secondly I am unable to step through in the private method using F11. (To ensure that private right method name is correct, I provided wrong name then method did not get invoked, exception thrown,which is right).

    I would be sending you the sample. By then If you any update then do let me know.

    Thanks
    Moyeen
  10. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 10 May 2013 Link to this post

    Hello Moyeen,

    I am glad your tests are passing now.

    To investigate the slow test execution, I need to know some more things:
    • First, which test runner is being used for that current test execution (vstest, MSTest or any other compatible test runner like: JustCode, TestDriven.NET, etc.)?
    • If using Visual Studio 2012 with the vstest execution engine, let me know if "Keep test execution engine running between test runs" is checked (I have attached a screenshot to guide you where to find this).
    • Does the issue occurs for every test in your test assembly or is it happening only once (at the beginning) for a full test run.
    We believe this is due to a slow initialization of your test runner, and the above information will help us find what could be causing it.

    I have also attached a sample project to show what could be debugged when using JustMock for non-public methods.

    Kind regards,
    Kaloyan
    the Telerik team
    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.
  11. Poonam
    Poonam avatar
    2 posts
    Member since:
    Apr 2013

    Posted 13 May 2013 Link to this post

    Hello Kaloyan,
    Thanks for response, test running time seems to improved. But as I said, Now private are not getting Mocked and Invoke , I attached the sample project which  available with ticket ID: 692468

    Thanks
    Mohd Moyeen
    mohd.moyeen@allscripts.com
  12. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 13 May 2013 Link to this post

    Hello Moyeen,

    I am glad you have managed to improve the test execution performance.

    Our development team is currently investigating the issue from ticket with ID: 692468. I will reply in it when a solution can be provided.

    Regards,
    Kaloyan
    the Telerik team
    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.
  13. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 04 Jun 2013 Link to this post

    Hi Kaloyan,
    Does the newly released JustMock Q2 2013 (version 2013.2.530) has the solution for ticket ID  692468

    We are facing the issue in test execution regarding the profiler related on the TFS, perfectly running fine on Local deveplopment machine.
    For the test execution we are using the mstest from the msbuild scripts, test cases are failing showing the error as 
    "Class Initialization method Test.Foo  threw exception. Telerik.JustMock.Core.ElevatedMockingException: Telerik.JustMock.Core.ElevatedMockingException: The profiler must be enabled to mock/arrange the specified target.".

    However all the test cases are running perfectly fine on the local machine and providing the code coverage results as well.


    Do let me know how we can configure the msbuild scripts so that profiler get enabled

    Regards
    Mohd Moyeen
    mohd.moyeen@allscripts.com
  14. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 05 Jun 2013 Link to this post

    Hello Moyeen,

    Yes, the fix for the issue in ticket with ID 692468 is included in the latest internal builds. As an addition, I should note yesterday we released JustMock 2013.2.603. Please, feel free to try it out.

    To integrate JustMock in MSBuild task, please follow this article and let me know if there are any further issues.

    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.
  15. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 05 Jun 2013 Link to this post

    Hello Kaloyan,
    Thanks for your response.
    I have two environment and have queries around test case runner and profiler and then eventually getting the code coverage.

    1)ON LOCAL MACHINE USING VISUAL STUDIO:
     I turn the profiler on and all the test cases are running as passed, however no code coverage results. I am setting the code coverage from the visual studio.
    •  In this case which is profiler is getting on , is it microsoft or Telerik. I am assuming it is microsoft profiler ?
    •   which is test case runner is it MSTEST or JustMock runner?

    2)ON TFS 2010 USING MSBUILD and MSTEST and provided code coverage setting file .testsettings
    Test cases are compiling from the build definition and mstest attempting to run the test cases but failed because profiler is not turned on. so after doing the setting as you refer in the below link

    •  In this case which profiler is running would it e  microsoft or Telerik. I am expecting it is microsoft profiler ? But also worried about the code coverage results ?
    •  Since test case  runner  MSTEST as defined in the msbuild, so after doing the setting as you refer in the below link, would  it require the JustMock runner. We only want to use mstest in the TFS ?

    link refer by you    
    http://www.telerik.com/help/justmock/integration-msbuild-tasks.html

    Help me to understand above four bullet points. My objective to use mstest in the TFS and get the code coverage (dont know which profiler is need, however I wish to have visual studio 2010 profiler )

    Thanks
    Moyeen
    mohd.moyeen@allscripts.com


  16. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Jun 2013 Link to this post

    Hello Moyeen,

    First, I should note that JustMock does not support the code coverage from Visual Studio 2012. However, it should work without a problem with the Visual Studio 2010 Code Coverage.

    I will try to answer your questions below:
    1. If you are using VS2010 and its Code Coverage, you could check this article in order to configure it in your environment.
      • The Visual Studio 2010 Code Coverage tool does not use profiler. So in this case, there should remain only our profiler (Telerik.CodeWeaver.Profiler) enabled.
      • If running tests in Visual Studio 2010, the test runner for MsTests will be MsTest (QTAgent*).
        If running tests in Visual Studio 2012, the test runner for MsTests will be VsTest (vstest.executionengine*).
      The JustMockRunner is not an actual test runner. The only thing it does, is to set a proper environment for the JustMock profiler. For more details, please check this link.
    2. If everything from the integration article is correct and you are using the Code Coverage from VS 2010, your tests should not fail with the profiler not enabled exception. However, if you are integrating JustMock with the VS2012 Code Coverage your tests will always fail with this exception. This is due to the fact, currently JustMock does not support integration with the VS 2012 Code Coverage.
      • As the VS2010 Code Coverage does not use profiler, the JustMock profiler should be the only one enabled. However, please check if there are any 3rd party profiler using tools installed and running on your build environment. Tools like TypeMock for example, may prevent our profiler from loading. If this happens to be the case, I would suggest to disable/uninstall them temporary and then check again if the issue still remains.
      • If you follow the workflow described in this article, you won`t need to use the JustMockRunner and everything should work as expected with the MsTest runner.

    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.
  17. Mohd
    Mohd avatar
    13 posts
    Member since:
    Jan 2013

    Posted 10 Jun 2013 Link to this post

    Hello Kaloyan,
    Thanks for detailing out the relation between JustMock runner and MSTEST and visual studion and the profiler.

    You provide me Q2 2013 2013.1.515 internal build because then latest build caused my all test cases failed.

    I am trying to do integration with TFS 2010, with VS 2010. My current installation do not have Telerik.CodeWeaver.dll . So not able to configure the setting for  the code coverage result using TFS 2010 VS 2010 MSTEST.

    I really looking forward the new version of the build Q2 2013 2013.2.530. 


    How can I get this latest version and see if my test cases are running, than I would like  to integrate JustMock with TFS 2010 VS 2010 MSTEST and get chance to pass the test case on the TFS, so far only running and passing on local environment but without code coverage result.



    Regards
    Mohd Moyeen
    mohd.moyeen@allscripts.com






  18. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 10 Jun 2013 Link to this post

    Hello Moyeen,

    You could get the last JustMock internal build from here (JustMock_2013.2.603_Dev). You will need to use your JustMock licensed account for this.

    To integrate JustMock with TFS2010, you will need to also install JustMock on you build machine. This, will take care about registering the JustMock profiler on your build server. Then, following the MsBuild integration article should guide you through the process.

    Further, using TFS 2010 with VS 2010, you should not have any problems using the Code Coverage coming with it, along with JustMock.

    Let me know, if there are any other issues.

    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.
Back to Top
DevCraft R3 2016 release webinar banner