Mocking in Windows store app

亡梦爱人 提交于 2019-12-10 19:23:01

问题


I'm not probably first who deals with mocking in windows store application for testing purpose. I would like to test my ViewModels and to use some of mocking frameworks to mock them. Of course, all of available (common) frameworks are not able to use in windows store application project. I have one idea how to solve it but I'm not sure that it is best solution. My solution consists of these projects. Main point is to divide presentation layer to two parts :

Presentation - Windows store application

Start-up project that contains only presentation views (Pages) and presentation parts that do not need to be tested. This project has reference to PresentationLogic.

PresentationLogic - Portable Class Library, Targets : Windows store application, .NET Framework 4.5

This project contains all presentation logic like ViewModels, Converters, Helpers etc. that should be tested

UnitTests - Class library

Classical class library containing unit tests with ability to mocking all interfaces from PresentationLogic. This library has reference to PresentationLogic.

It is quite strange to divide Views and ViewModel to two layers but I did not find another solution for this.

Do you have please any idea how to deal with this problem? What about splitting of the presentation layer to the two layers of another project type? Can it cause some problems in further development?


回答1:


You're definitely on the right track. A couple of notes:

Using MvvmLight (which is available portable, by the way), you can use their built-in ServiceLocator and DependencyInjection to do things like inject test controllers for platform specific processes. This will allow a ton of your logic to remain portable, by defining interfaces and injecting the implementations (including mocked implementations).

Based on your PCL, you will likely (in my experience) be unable to include Converters (which inherit from IValueConverter) in your PCL. The library is generally different between the platforms (especially Silverlight/WinRT/4.5/Mono), as the most common use for them is for UI, such as binding processing. Same with things like DataTemplateSelectors. These will likely have to be rewritten for most of your platforms (though luckily that's not that hard and is still quite a bit of copy-paste).

As to the rest of it, you have it spot on. You can have your Presentation app be Universal, so it can cover both Windows Store and Windows Phone Store apps. The vast majority of your 'business logic' should be in your PCL. You may run into some issues in this regard because sometimes it's just unavoidable to want to put some UI helpers inside the VM for ease of use. If this is absolutely necessary, you can make your Portable ViewModel abstract, then use the Dependency Injection mentioned above to insert the platform-specific implementations. It's quite easy to do and very useful.

The one thing that you are missing is UI tests. You could include them in your unit test class library, or make another Coded UI Test class library, up to you.

Anyway, hope that helps.




回答2:


FYI, you can now use JustMock in order to mock directly into Windows 8.1 Unit Test projects.

See my answer



来源:https://stackoverflow.com/questions/23641737/mocking-in-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!