开发者

UI logic unit test

I have tens of conditions to check that set boolean values for seven or eight ui buttons enabled property.

So I setup boolean variables for each of the buttons (for example, isAction1Allowed, isAction2Allowed, etc.

How would I write a unit test for this case?

Currently, I have one method that contains all the logic which is nice but not sure how to turn into unit test (keep in mind very new to MVC and unit testing)

public void StateChecker() {  //This method resides in HtmlHelper
  bool isAllowed1 = false;
  bool isAllowed2 = false;
  bool isAllowed3 = false;

  if (condition1) {
    isAllowed1 = true;
  }
  else
  {
    isAllowed2 = true;
  }

  if (condition2) {
    isAllowed4 = true;
开发者_开发问答    isAllowed2 = true;
  }

// At the end of the method
Button1.Enabled = isAllowed1;
Button2.Enabled = isAllowed2;
Button3.Enabled = isAllowed3;

}

Will I have to break the method up? Is there a better way to do what I'm trying to do? Remember there are a lot more conditions and buttons than the example shows but this is the gist of it. Basically, it's a small workflow or state machine.


Normally you should use view models which are classes specifically adapted to your views. Those classes may contain such UI properties. It's up to the controller to populate them from the domain model classes, so you could would test them as any other class.

Personally I use AutoMapper to convert between my domain model classes and my views models, so it I unit test my mapper classes which are responsible for this conversion.


Here's one way (use optional model):

  public YourModel{
    public bool IsAction1Allowed {get;set;}
    }

    public ActionResult Index(YourModel model = null){

    model = model ?? new Yourmodel();

    return View(model)
    }



 or (use public properties)




  public MyController:Controller{

    public bool IsAction1Allowed {get;set;}

   public ActionResult Index(){

    vare model = Yourmodel();
    model.IsAction1Allow = IsAction1Allowed

    return View(model)
    }



    }


or (use constructor)

   public MyController(Settings setting){


    public ActionResult Index(){

    vare model = Yourmodel();
    model.IsAction1Allow = settings.IsAction1Allowed

    return View(model)
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜