开发者

Unit testing a menu in ASP.NET MVC 2

I have done some reading on unit testing and want to start with my first test. I am using ASP.NET MVC 2.

We determine the type of user by getting the staff number and checking it against the Roles table. If the user exists in this table then the UserType property is set.

We have a menu for each type of user, and each of the menu items are different for each type of user.

How would a test loo开发者_如何学Gok like for this displaying the menu items for each type of user?


If you have written your application, according to some you'll now be working with "Legacy code" (based on the Michael Feather's book Working Effectively With Legacy Code - by "Legacy Code" Feathers means any system that isn't adequately covered by unit tests).

As @bAN mentions, you will have to do a lot of work to isolate the methods you will be calling by using factory methods that you can replace with "stubs" using interfaces or by using override. Without this, you will be doing integration testing (as you will also be testing the integration between different atoms of functionality). But, a 1-to-1 ratio of test methods to production methods will not give enough coverage in almost all cases.

Depending on your version of Visual Studio, the amount of testing tools will vary. NUnit is a very popular testing framework: http://www.nunit.org/, if you aren't using the Visual Studio tools.

To get testing, you can:

Create a new project of type Test Project called YourProjectUnderTestName.Tests.Unit.

You will see that a Test Class has the attribute [TestClass] and test methods have the attribute [TestMethod].

You will also have to look at the Assert classes and their static methods http://msdn.microsoft.com/en-us/library/ms182530.aspx

You should follow the Arrange, Act, Assert pattern in your tests. Get everything you need to run the code you want to test in isolation, then run the code under test, then use Asserts to test against expected behaviour.

You will at some point also need to find out about [SetUp]/[TearDown] methods (in MSUnit) these have the attributes:
[ClassInitialize()],[TestInitialize()], and
[TestCleanup()],[ClassCleanup()]

The ClassInitialize runs before when any tests are started, TestInitialize runs before each test in the class, TestCleanup runs after each test in the class and ClassCleanup runs after all of the tests are finished.

As @bAN says have a look through the http://artofunittesting.com/ site and seriously consider getting the book. It's one of the ones I would grab if the office was on fire, along with Code Complete.

As @bAN and @Oliver Hanappi have mentioned TDD is a very good way to make sure you do "proper" unit testing and get good coverage.

Good luck!


Unit test is a "microscopic" testing unit, so you have to write Unit test for each method in the class you want to test. With Visual Studio Unit testing is quite easy, you just have to right clic on the class and change generate unit tests. He Make for you the Test class and you just have to write the code who test the method.

Every unit test have to test ONLY the method and not an another method of a an another class. So if the method you are testing call another one you have to test the CALL with mock objects..

A great and useful book about unit testing. Art of Unit Testing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜