2011-03-17 60 views
1

我在實例化從資源加載圖像的視圖模型時遇到問題。WPF單元測試DirectoryNotFoundException

,在彙編失敗行我測試的是:

get { return new ImageSourceConverter().ConvertFromString("pack://application:,,,/Resources/Icons/Commands/DisabledNewSessionIcon.png") as ImageSource; } 

的例外是: 無法創建類

GPAnalysisSuite.Tests.View_Models.Session_Controller.SessionControllerViewModel_NonDefaultConstructorTester. Error: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\TGP\GP Analysis Suite\Application\Tests\TestResults\Paul_PAUL-GP 2011-03-17 11_27_28\Out\Resources\Icons\Commands\DisabledNewSessionIcon.png'..

的情況下我已經找到了解決一併將以下內容添加到TestClass中:

[AssemblyInitialize] 
    public static void InitialisePackageUriHelper(TestContext context) 
    { 
     PackUriHelper.Create(new Uri("reliable://0")); 
     new FrameworkElement(); 
     System.Windows.Application.ResourceAssembly = typeof(App).Assembly; 
    } 

I可以看到我需要保留正在測試的程序集的Uri,但不知道如何去做,任何人都可以幫忙嗎?

回答

1

我似乎已經通過將資源構建操作從內容更改爲資源來解決問題。
雖然每次我想運行單元測試時都必須重新構建解決方案,但現在至少可以使用該解決方案。