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:
I get the following exception:
I've tried using the new call:
To no avail. How can I mock a Thread?
Thanks.
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.
8 Answers, 1 is accepted
0
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
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
0
Nacho
Top achievements
Rank 1
answered on 20 Feb 2014, 01:38 PM
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
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
0
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:
Or, if you prefer explicitly asserting occurrences, you could also do it like so:
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
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());
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
0
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
Nevertheless, I urge you to refrain from using
Regards,
Stefan
Telerik
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.
0
Nacho
Top achievements
Rank 1
answered on 18 Mar 2014, 05:22 PM
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!
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!
0
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
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.
0
Nacho
Top achievements
Rank 1
answered on 19 Mar 2014, 08:31 AM
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
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
0
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
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.