DelegateCommand.RaiseCanExecuteChanged -- Can't test it :(

Topics: Prism v2 - Silverlight 2
Jun 5, 2009 at 3:23 AM


I am using Prism in my application and I am using NUnit to test my code.  I like using NUnit, because it lets me test things like my ViewModels without the overhead of the entire Silverlight runtime being executed.  

The problem I am running in to is that DelegateCommand.RaiseCanExecuteChanged calls into Deployment.Current which throws an exception in my unit test environment... I'm not surprised, since there is no current application in a unit test.

Aside from removing it from the DelegateCommand.Silverlight.cs:OnExecuteChanged(), does anyone know a way around this?

Here is my stack trace:

failed: System.Exception : Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

at MS.Internal.XcpImports.CheckHResult(UInt32 hr)

at MS.Internal.XcpImports.Deployment_GetCurrent(IntPtr& pDeployment)

at System.Windows.Deployment.get_Current()

at Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand`1.OnCanExecuteChanged()

at Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand`1.RaiseCanExecuteChanged()

C:\Users\BRIAN GENISIO\Documents\Visual Studio 2008\Projects\RecipeBox\RecipeBox\RecipeList\RecipeListViewModel.cs(82,0): at RecipeBox.RecipeList.RecipeListViewModel.set_SearchText(String value)

C:\Users\BRIAN GENISIO\Documents\Visual Studio 2008\Projects\RecipeBox\RecipeBoxTests\RecipeBox\RecipeListViewModelTests.cs(132,0): at RecipeBoxTests.RecipeBox.RecipeListViewModelTests.Search_With_Two_Pages_Requests_Second_Page()

Jun 5, 2009 at 9:12 PM

Hi BrianGenisio,


I don't know what functionality you are trying to test, but if you need to use NUnit one way you can workarround this unavailable current application is creating a MockDelegateCommand.


In most usual scenarios you won't need during the test the ExecuteChanged functionallity that is implemented in the prism DelegateCommand. So, to easily create a mock of the delegate command you can create a mock class that inherits from the prism DelegateCommand and overrides the necessary methods (in your case, you might override the OnExecuteChanged method). If the method you need to mock is not available to override you can implement the ICommand interface to mock the command.


Hope it helps!


Matias Bonaventura