Tuesday 15 May 2012

Unit Testing ASP.NET MVC -



Unit Testing ASP.NET MVC -

i testing index action contains global variables. have created few test methods index action. when run these methods simultaneously, values in global variable changes due asserts fail.

this index action:

public class setcontroller : controller { public static string previousorder; public static int previouspagenumber = 1; public static bool orderby = true; public actionresult index(string filter_value, int? page_no, string sorting_order, bool? orderby, string faqsetcreated) { int no_of_page = (page_no ?? 1); if (no_of_page == previouspagenumber) { if (sorting_order == previousorder) { if (orderby != null) { if ((bool)orderby) { orderby = false; } else { orderby = true; } } } else { orderby = true; } } } }

this test method:

public void faq_set_index_test_with_no_parameter_passed_returning_model() { //arrange string filter_value = null; int? page_no = null; string sorting_order = null; bool orderby = false; string faqsetcreated = null; //act var result = controller.index(filter_value, page_no, sorting_order, orderby, faqsetcreated) viewresult; //assert assert.istrue(result.viewbag.orderby); }

when run test methods together, after passing first test values of global variable changes due upcoming test gets changed values , cannot behave expected

what should value of static variables remain same default values after each test run.

most unit testing frameworks allow define method can run after each test using attribute, utilize reset static values after each test. haven't said testing framework you're using, mstest example, attribute testcleanup.

[testcleanup] public void resetdefaultvalues() { setcontroller.previousorder = null; setcontroller.previouspagenumber = 1; setcontroller.orderby = true; }

asp.net-mvc unit-testing

No comments:

Post a Comment