包URL和单元测试。我的环境问题? [英] Pack Urls and Unit Testing. Problem with my environment?

查看:314
本文介绍了包URL和单元测试。我的环境问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有了这个可爱的小MVVM解决方案,以及工作的事情很大。我有一个视图模型为调整基础上的应用,等等。我已经做了验收测试的状态图标的标题栏,视图模型的伟大工程。

So I've got this nice little MVVM solution, and things work great. I've got a view model for a header bar that adjusts the icon based on the state of the application, etc. I've done acceptance tests, the view model works great.

所以,我想的单元测试的这一观点模型的行为。我创建单元测试项目中,添加一个新的单元测试视图模型,并写一个简单的冒烟测试。 (即鉴于嘲笑的依赖,将类实例化)。

So I want to unit test the behavior of this view model. I create my unit testing project, add a new unit test for the view model, and write a simple smoke test. (i.e. Given mocked dependencies, will the class instantiate).

巴姆,没有

然而,类正常工作时正常运行。经进一步检查,我的错误是:

However, the class works fine when ran normally. Upon further inspection, my error is as follows:

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

那么下面的调用堆栈我在我的包URL中使用的加载资源流是那些踢了错误的结论。

So following the call stack I arrive at the conclusion that my pack URLs used to load resource streams are the ones kicking the errors.

包://应用:,,, / Operations.Shell;组件/媒体/图片/ User_Normal.png

(注: Operations.Shell 是集名称, /Media/Images/User_Normal.png 是图像路径/名称,而这个包的URL的在实践中。)

(Note: Operations.Shell is the assembly name, /Media/Images/User_Normal.png is the image path/name, and this pack url works in practice.)

时的包网址我有我的User_Normal.png,该文件存在,资源已正确装入大会(核对反射)。

Is the pack url I have for my User_Normal.png, the file exists, the resource is properly packed into the assembly (checked with reflector).

这个问题源于的System.Uri 类不能够跨preT包网址。 这是我迷路的。为什么在测试的范围这行不通。我把所有的WPF大会在我的测试项目中引用:

The problem arises from the System.Uri class not being able to interpret the pack url. This is where I'm getting lost. Why would this not work in the confines of testing. I have all the WPF Assemblies referenced in my test project:

  • WindowsBase
  • presentationCore
  • presentationFramework
  • System.Xaml

我在想什么?

更新

好的所以原来的问题是,UriHandler不是为包网址注册。 (感谢朱利安Lebosquain)现在,多数民众赞成固定它仍然有问题。

Alright so the original problem was that the UriHandler wasn't registered for pack urls. (Thanks to Julien Lebosquain) Now that that's fixed it's still having issues.

TestInitialize抛出异常:System.NotSupportedException:该URI preFIX无法识别


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试图解决的东西基于网络的程序集的包网址是什么?看起来像处理程序的路由请求错了吗?还是我失去了一些东西?

It looks like the pack url's trying to resolve to something web-based for an assembly pack url? Looks like the handler's routing the request wrong? Or am I missing something?

推荐答案

我得到了这个问题,一旦被咬过...

I got bitten by this problem once too...

引用的组件是不够的。 WPF需要调用 System.UriParser.Register()与自己的URI语法分析器,使的System.Uri 可以跨preT包的URL。

Referencing the assemblies isn't enough. WPF needs to call System.UriParser.Register() with its own URI parser so that System.Uri can interpret pack URLs.

反射告诉我们,这是由 System.IO.Packaging.PackUriHelper 的静态构造函数中完成。调用这个类中的任何方法,在您的测试,如 PackUriHelper.Create(),以确保URI解析器以及注册。那种丑陋的,但应该工作。

Reflecting tells us that this is done by the static constructor of System.IO.Packaging.PackUriHelper. Call any method of this class in your test, like PackUriHelper.Create() to ensure the URI parser is well registered. Kind of ugly, but should work.

这篇关于包URL和单元测试。我的环境问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆