No interface. NET code for unit testing

Source: Internet
Author: User

The work of the recent job is to study how to add unit test for a historical code project, have done, I would like to give you a share of the results, to listen to the feedback from everyone.

The project is still under the VS2010 C # code, although the use of inheritance, encapsulation and polymorphism, but the application of the interface is very few, so there is basically no way to use common Mock framework (such as MOQ, Rhino Mock, etc.) to write unit testing.

Considering this, the solution is nothing more than two: Refactoring existing code, implementing dependency injection via Interface (interface) (Dependency injection, DI), and looking for a mock framework that does not directly support virtual entity classes through interfaces, to intercept access to dependent objects, Returns the intended result. With scenario one, optimizing the code architecture is better in the long run, but with a lot of work to do, there is a great deal of testing to ensure that the existing code functionality is not changed. With scenario two, you don't need to make any changes to the existing code, just create the test code directly. So the plan two is still very attractive, I have done some research in this direction.

I found that there is no open source mock framework (. NET unit test related) to support virtual entity classes, and I only found Microsoft Fakes, Typemock isolator and Telerik Justmock three. Although there are some differences in usage, but the main functions are relatively close, the difference is mainly reflected in the following aspects:

1. From a price point of view:

    • Microsoft Fakes, there is no free version, only in vs2012/vs2013 Premium and Ultimate version, and our development environment is mainly professional version, The cost of upgrading to the appropriate version is high (VS2013 words Windows Store spreads $1770,msdn subscription spreads $4920)
    • Telerik Justmock Meet the demand version price is each developer $399, including one year free upgrade + support, (see buy Justmock)
    • Typemock Isolator Meet the demand version price is $799 per license,$150 per year upgrade + support (see Isolator Pricing)

Since the fakes price is too high and there is a need to upgrade the existing code, we do not consider it directly. Of course, if the corresponding version is already in use, still can consider, after all, Microsoft's own things are the most convenient to use.

2. Functionally, according to one of our test cases with Typemock and Justmock to write a bit, found justmock more powerful.

Our test case is to measure a time value that does not contain a year for a particular reason, so when it passes from a client to a server in a different time zone, it is resolved to the wrong year across the years. For example: The client was at Gmt+8 at 2014/01/01 07:00:00, and the service was at GMT, which was 2013/12/31 23:00:00, and the time was resolved to 2013/01/01. 07:00:00. The DateTime.ParseExact () method is called for parsing. Typemock provides limited support for virtual classes in mscorlib, and does not support the virtual of this method. Justmock is doing a better job in this area and is able to support the approach.

So, my final conclusion is either refactoring the code, using interfaces to implement dependency injection, then basically all kinds of mock frameworks can be used, or just buy justmock, do not move the existing code, directly add unit test just fine.

There are some other details of things, time is limited to add it another day.

No interface. NET code for unit testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.