简体   繁体   English

从屏幕继承的单元测试ViewModel(Caliburn.Micro)

[英]Unit Test ViewModels Inheriting from Screen (Caliburn.Micro)

I've started learning TDD, and I bumped into ViewModel testing. 我已经开始学习TDD,并且参加了ViewModel测试。 I'm using Caliburn.Micro as my MVVM framework, and my ViewModels are inheriting from Screen. 我使用Caliburn.Micro作为MVVM框架,并且ViewModels从Screen继承。

When I create try to test a ViewModel, I get this error: 创建尝试测试ViewModel时,出现以下错误:

The type 'Caliburn.Micro.Screen' is defined in an assembly that is not referenced. 在未引用的程序集中定义了“ Caliburn.Micro.Screen”类型。 You must add a reference to assembly 'Caliburn.Micro' 您必须添加对程序集“ Caliburn.Micro”的引用

Is this OK? 这个可以吗? I think isolating my classes from the framework is the right path, but I don't know how should I do it (preserving the benefits the framework gives by inheriting from Screen) 我认为将类与框架隔离是正确的方法,但是我不知道该怎么做(保留从Screen继承来的框架所带来的好处)

Thanks, Martín 谢谢,马丁

Yes it is okay to add the reference. 是的,可以添加参考。 The only alternative is dynamically creating the type, which does not really offer you much advantage. 唯一的选择是动态创建类型,这实际上并没有给您带来太多优势。

I assume your ViewModel will not try to display any screens. 我假设您的ViewModel不会尝试显示任何屏幕。 If it does you will need to subclass it to get a version that can be unit tested. 如果是这样,您将需要对其进行子类化以获取可以进行单元测试的版本。

I haven't used Caliburn.Micro but it sounds a bit odd that your ViewModel inherits from Screen. 我没有使用Caliburn.Micro,但是您的ViewModel是从Screen继承的,这听起来有些奇怪。 Normally with MVVM the ViewModel knows about the View but does not inherit from one. 通常,使用MVVM,ViewModel可以了解View,但不能从View继承。 A quick Google search suggests that in Caliburn.Micro the ViewModel implements the IScreen interface, as opposed to inheriting from Screen. Google的快速搜索表明,在Caliburn.Micro中,ViewModel实现了IScreen接口,而不是从Screen继承。

Does the dependency on Screen pull in a bunch of other things too... if not, might not be a big deal. 对Screen的依赖是否还会引入其他方面的内容...如果没有,可能没什么大不了的。

The ViewModels are part of the presentation layer and are usually coupled with the GUI framework that you use.. Corollary: the ViewModels might need to be reworked/thrown away when the GUI framework changes unless you design for it (separate the business logic from the presentation and push it down). ViewModels是表示层的一部分,通常与您使用的GUI框架耦合。推论:除非您为GUI框架进行设计(将业务逻辑与业务逻辑分开),否则当GUI框架发生更改时,可能需要重新制作/扔掉ViewModels。演示文稿并向下推)。 If you're incensed by this para, check out Robert Martin's talk on Clean architecture at NDC Oslo (available online) 如果您对本段内容感到恼火,请在NDC Oslo 查看Robert Martin关于“清洁架构”的演讲 (在线提供)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM