Should I unit-test my view in MVP(or VM) or how to keep the code in the view to a minimum?

牧云@^-^@ 提交于 2019-12-06 00:12:04

If you have some specialized logic (like drag&drop you mentioned) in your views, you can still move it out into separate class(es) - services, which can then be unit-tested in isolation. Sometimes you will need to refactor (or generalize) the code to achieve this, though. But the idea is to keep the view code as thin as possible, so that all of the other code is unit-testable.

And if you really want to cover views with tests, you can still use some WinForms GUI testing frameworks like White (although such testing is much more difficult and costly than simply writing unit tests with mocks).

Here's a good source about WinForms, MVP and unit-testing: Presenter First: Organizing Complex GUI Applications for Test-Driven Development

Any time you have logic that might go wrong you should probably write a test for it (if you can). That being said, you can probably have a pass on most of the UI if it truly does not have any hand coded logic. There is not much point in testing form mappings for each form as long as the form mapping system is either under test or is from Microsoft or some vendor

Tragically logic can sometimes leak into the view. A good way to help avoid this is to write functional tests using something like Fitnesse (yes there is a .NET version). Fitnesse is designed to test the app all the way through, as an alternative UI and when done right makes it hard for people to put something in the view.

You can also do tests of these sorts in a unit testing framework but I find the seperation to be helpful, plus Business people can help write and run the tests (because they are in wiki format).

I think you're on the right track with keeping business logic out of the View. That said, I see nothing wrong with presentation logic in the View.

Personally I try to keep my Views as lean as possible, use data binding whenever I can, and generally test presentation logic manually (running the application and putting it through its paces).

In my opinion, unit tests for non-visual classes are extremely valuable. There is also value in writing user acceptance, functional, and/or integration tests that cover the interactions of all or part of the system; tools such as FitNesse (as ryber mentioned) are geared towards these. Finally there is GUI testing; while automating this for web sites may provide some value, I feel the test-maintenance costs outweigh the benefits when it comes to WinForms.

However I'm not an expert. I'd definitely suggest researching what testers (not marketers) say about GUI testing and make up your own mind.

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