Unit Testing, Agile Development, Leadership & .NET - By Roy Osherove
Specifically, those dependencies aren't broken - they're intercepted. It's not entirely disingenuous to say that dependencies are broken, but the "break dependencies" terminology colloquially refers to a design activity rather than such a brute force countermeasure.
Wow, very timely. I have a spike to work on just this very item. Thanks.
Looks like Visual Studio has a bit of a issue with the code coverage feature and Silverlight applications. The test run config dialog disappears when you click on the code coverage list item.
Roy,
I was trying to run NUnit test from NUnit GUI.
I received invalid thread access exception. It looks like the NUnit thread is not able to access the Silverlight UI thread for some reason. How to overcome this crossthread access issue?
Your help would be appreciated.
Exception details:
Tests.UnitTest1.TestMethod1:
System.TypeInitializationException : The type initializer for 'System.Windows.DependencyObject' threw an exception.
----> System.UnauthorizedAccessException : InvalidCrossThreadAccess
-----
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex)
at System.Windows.UIElement..ctor(UInt32 nKnownTypeIndex)
at System.Windows.FrameworkElement..ctor(UInt32 nKnownTypeIndex)
at System.Windows.Controls.Control..ctor(UInt32 nKnownTypeIndex)
at System.Windows.Controls.UserControl..ctor()
at CheckFree.ConsumerBanking.AccountDetailsControl..ctor() in C:\dev\SLProject\WintellectLatest\ConsumerBanking\AccountDetailsControl.xaml.cs:line 11
at Tests.UnitTest1.TestMethod1() in C:\dev\SLProject\WintellectLatest\Tests\UnitTest1.cs:line 26
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at Tests.UnitTest1.TestMethod1() in C:\dev\SLProject\WintellectLatest\Tests\UnitTest1.cs:line 22
--UnauthorizedAccessException
at MS.Internal.XcpImports.CheckThread()
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
at System.Windows.DependencyObject..ctor()
at System.Windows.DependencyObject.ManagedReferencesToken..ctor()
at System.Windows.DependencyObject..cctor()
------
I wrote one sample test like below:
namespace Tests
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestFixture]
public class UnitTest1
[Test, Isolated]
public void TestMethod1()
Isolate.WhenCalled(() => App.IsDesignTime()).WillReturn(true);
AccountsControl accountsControl = new AccountsControl();
Assert.IsNotNull(accountsControl);
}
Chandra :
that is because the NUNIT gui was not running with Typemock Isolator enabled on it's process.
either run "TmockRunner.exe nunitgui.exe" which will enable this, or start Nunit from within visual studio (if you have testdriven .net you can right click on the test project and select test with->nunit"
It did not solve the issue.
Now, I also commented the typemock code and tried to run NUnit from visual studio as well.
It throws the same exception.
Invalid Cross Thread access.
I think the Nunit is running on different thread from Silverlight UI. So, it is complaining. Do I need to add any configuration file into my silverlight project so that Nunit tests will have access to silverlight thread?
--Chandra
Roy sent me the CThru that was built with TypeMock5.2.3. When I used this CThru 5.2.3 alogn with TypeMock5.2.3, it has resolved the CrossThread access issue. Now, the silverlight calls are being intercepted successfully when UnitTests were executed.
Thanks to Roy. I apprecieate your help.
I hope it would help somebody else too who might face this issue.