Not possible to create a Thread Mock

9 posts, 0 answers
  1. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 17 Feb 2014 Link to this post

    Hello,

    I've been trying to create a Thread mock just to check that some methods are called when needed (I don't need to execute anything, just assert that things are called). I've tried the answer from this post but it does not work:

    var mock = Mock.Create<Thread>(Constructor.Mocked);
    Assert.IsNotNull(mock);

    I get the following exception:

    Telerik.JustMock.MockException: Cannot create mock for sensitive types.

    I've tried using the new call:

    AllowedMockableTypes.Add<Thread>();

    To no avail. How can I mock a Thread?

    Thanks.
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 20 Feb 2014 Link to this post

    Hello Nacho,

    In the latest versions of JustMock, it is not possible to mock the Thread class. Even adding it to the mockable types list will not allow it, just as you have noticed.

    I would suggest following the answers in this StackOverflow post for possible workarounds. I hope it 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.
  3. DevCraft R3 2016 release webinar banner
  4. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 20 Feb 2014 in reply to Kaloyan Link to this post

    Sounds as a downgrade to us.

    Can we know why this happens now and not before (when we did the first trial)? We don't want to try to run anything with the mocks, we just need to assert that the tread is started when it should, it's a simple expectation.

    Thanks.


    Ignacio Soler Garcia
    Software Engineer 

    Omron Europe B.V. 
    MSC - Mechatronics Software Center 
    Josep Plà, 2 Edifici B2, Planta 10 | 08019 Barcelona - Spain 
    Tel. +34 93 214 0615, Fax. +34 93 214 0631 
    Email: ignacio.soler@eu.omron.com 
     
  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 25 Feb 2014 Link to this post

    Hello Ignacio,

    After further investigation we concluded that this was in fact a regression. I've logged the bug and a fix should be made available in one of our upcoming releases.

    The problem with mocking Thread (and other "sensitive" types) is that the simple act of creating a mock of Thread promptly crashes the CLR. This started happening after a few upgrades to JustMock that increased its mocking power allowing it to mock previously untouchable members.

    As a temporary workaround I suggest you use a partial mock, instead of a true mock.

    You could do it like so:
    var thread = new Thread(() => {});
    Mock.Arrange(() => thread.Start()).MustBeCalled();
     
    thread.Start();
     
    Mock.Assert(thread);

    Or, if you prefer explicitly asserting occurrences, you could also do it like so:
    var thread = new Thread(() => { });
    Mock.Intercept<Thread>();
     
    thread.Start();
     
    Mock.Assert(() => thread.Start(), Occurs.Once());
    The Mock.Intercept<Thread>() call tells JustMock to start counting invocation occurrences for the given type.

    I hope the above is enough to resolve your current issue. If you need further help, don't hesitate to contact us again.

    Regards,
    Stefan
    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.
  6. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 18 Mar 2014 Link to this post

    Hello Ignacio,

    I'm glad to inform you that I've just pushed the bug fix for mocking the Thread class. In the next release of JustMock, you'll be able to override the safety checks by using the AllowedMockableTypes.Add<Thread>(); statement. Then, you'll be able to create a mock using Mock.Create<Thread>();

    Nevertheless, I urge you to refrain from using AllowedMockableTypes whenever possible, because it might destabilize the runtime and result in hard-to-debug test run failures. In your case, I would keep using partial mocks of Thread, instead of full mocks.

    Regards,
    Stefan
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
  7. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 18 Mar 2014 in reply to Stefan Link to this post

    Thanks Stefan,

    how long are active the Mock effects?

    Can I destabilize the runtime outside the current test? I mean, if I mock a thread in one test that pass can this affect to any other test result?

    Thanks!
  8. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 19 Mar 2014 Link to this post

    Hi Ignacio,

    Tests normally do not have side effects. If a test passes and does not crash the runtime, then you should be OK. Rarely, there could be side effects during garbage collection, when finalizers are called.

    Regards,
    Stefan
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
  9. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 19 Mar 2014 in reply to Stefan Link to this post

    Then it's fine for us.

    Thanks for your help Stefan.

    Ignacio Soler Garcia 
    Software Engineer 

    Omron Europe B.V. 
    MSC - Mechatronics Software Center 
    Josep Plà, 2 Edifici B2, Planta 10 | 08019 Barcelona - Spain
    Tel. +34 93 214 0615, Fax. +34 93 214 0631 
    Email: ignacio.soler@eu.omron.com 
  10. Vladi
    Admin
    Vladi avatar
    167 posts

    Posted 31 Mar 2014 Link to this post

    Hello Ignacio,

    I'd like just to inform you that we've just released public internal build (v.2014.1.1519.1) which contains a bug-fix for mocking the Thread class. Please give it a try and let us know whether it works as expected for you.

    Thanks,
    Vladi
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
Back to Top
DevCraft R3 2016 release webinar banner