gpt4 book ai didi

c# - 打包 URL 和单元测试。我的环境有问题?

转载 作者:IT王子 更新时间:2023-10-29 04:47:22 26 4
gpt4 key购买 nike

所以我得到了这个不错的小型 MVVM 解决方案,并且一切正常。我有一个标题栏的 View 模型,它根据应用程序的状态等调整图标。我已经完成了验收测试, View 模型工作得很好。

所以我想单元测试这个 View 模型的行为。我创建了我的单元测试项目,为 View 模型添加了一个新的单元测试,并编写了一个简单的冒烟测试。 (即给定模拟依赖项,类将实例化)。

不对

但是,该类在正常运行时运行良好。进一步检查,我的错误如下:

TestInitialize 抛出异常:System.UriFormatException:无效的 URI:指定的端口无效。

因此,根据调用堆栈,我得出结论,我用来加载资源流的包 URL 是引发错误的原因。

pack://application:,,,/Operations.Shell;component/Media/Images/User_Normal.png

(注:Operations.Shell为程序集名称,/Media/Images/User_Normal.png为图片路径/名称,本包url 在实践中有效。)

是我的 User_Normal.png 的包 url,文件存在,资源正确打包到程序集中(用反射器检查)。

问题源于 System.Uri 类无法解释包 url。 这就是我迷路的地方。为什么这在测试范围内不起作用。我的测试项目中引用了所有 WPF 程序集:

  • window
  • 演示核心
  • 展示框架
  • 系统.Xaml

我错过了什么?

更新

好的,原来的问题是 UriHandler 没有注册包 url。 (感谢 Julien Lebosquain)现在问题已解决,但仍有问题。

TestInitialize 抛出异常:System.NotSupportedException:无法识别 URI 前缀。

System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)System.Net.WebRequest.Create(Uri requestUri)MS.Internal.WpfWebRequestHelper.CreateRequest(Uri uri)System.IO.Packaging.PackWebRequest.GetRequest(Boolean allowPseudoRequest)System.IO.Packaging.PackWebRequest.GetResponse()MS.Internal.WpfWebRequestHelper.GetResponse(WebRequest request)System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()System.Windows.Media.Imaging.BitmapImage.EndInit()System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource, RequestCachePolicy uriCachePolicy)System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource)MyFramework.Resources.b__1(Uri u)MyFramework.Resources.ResourceType`1.Load(String path)Operations.Shell.AppShell.ViewModels.HeaderViewModel..ctor(IEventAggregator eventAggregator, ISecurityService securityService)Tests.Shell.AppShell.TestHeaderViewModel.TestInitialize()

看起来包 url 正在尝试解析为基于 web 的程序集包 url?看起来处理程序路由请求错误?还是我遗漏了什么?

最佳答案

我也被这个问题困扰过一次......

仅引用程序集是不够的。 WPF 需要使用自己的 URI 解析器调用 System.UriParser.Register(),以便 System.Uri 可以解释包 URL。

Reflecting 告诉我们这是由 System.IO.Packaging.PackUriHelper 的静态构造函数完成的。在您的测试中调用此类的任何方法,例如 PackUriHelper.Create() 以确保 URI 解析器已正确注册。有点丑,但应该可以。

关于c# - 打包 URL 和单元测试。我的环境有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3710776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com