简体   繁体   中英

Unit test validation of inputfields in asp.net

I'm trying to learn how to make a unit test that actually checks some code too see if there is a problem or not. I have never worked with unit test before, but have seen some tutorials to see how to build the test class.

I have created a small project with testboxes, where a user can enter his: Firstname, Lastname, Email, Phone, Address I want to make a unit test that checks if the entered is valid (not empty)

Is this even possible?

My code:

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        UserModel um = new UserModel();

        return View(um);
    }

    [HttpPost]
    public ActionResult Index(UserModel um)
    {
        if (ModelState.IsValid)
        {
            Session["SelectedValues"] = um;
            return RedirectToAction("Index", "Result", new { model = um });
        }
        return View(um);
    }

}

Index view (HomeController)

<div class="col-sm-12">

@using (Html.BeginForm())
{

<div class="row">
    <div class="col-sm-6">
        @Html.Label("First name:")<br />
        @Html.TextBoxFor(m => m.FirstName, new { @class = "textbox" })

        <br />

        @Html.Label("Last name:")<br />
        @Html.TextBoxFor(m => m.LastName, new { @class = "textbox" })

    </div>

    <div class="col-sm-6">

        @Html.Label("Email:")<br />
        @Html.TextBoxFor(m => m.Email, new { @class = "textbox" })

        <br />

        @Html.Label("Phone number:")<br />
        @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "textbox" })

    </div>
</div>
<div class="row">
    <div class="col-sm-10">
        @Html.Label("Address:")<br />
        @Html.TextBoxFor(m => m.Address, new { @class = "textbox"})
    </div>

    <div class="col-sm-2">
        @Html.Label("Address number:")<br />
        @Html.TextBoxFor(m => m.AddressNr, new { @class = "textbox" })
    </div>

</div>

<br />

<div class="row">
    <div class="col-sm-12">
        <button class="button" type="submit">Submit</button>
    </div>
</div>
}
</div>

ResultController

public class ResultController : Controller
{
    // GET: Result
    public ActionResult Index()
    {
       UserModel um = Session["SelectedValues"] as UserModel;
        return View(um);
    }
}

Index View (ResultController)

<div>
<h4>UserModel</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.FirstName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FirstName)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.LastName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.LastName)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.PhoneNumber)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.PhoneNumber)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Email)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Email)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Address)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Address)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.AddressNr)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.AddressNr)
    </dd>

</dl>
</div>

I want to create the test before i add the validation, just too see the Unit test in action.. can some explain or help me what i should do?

My test class

namespace UnitTestApplication.Tests
{
[TestClass]
public class ValidationOfInputFieldTest
{

    [TestMethod]
    public void TestIfInputFieldsAreValidated()
    {

    }
}
}

The goal of unit tests for the MVC controllers is to verify that the controllers are behaving correctly. In the context of unit testing you never actually get to a view or interact with a view. You just validate that the model returned by the controller is correct, that the model is correctly updated after posting to a controller, etc.

What you want to achieve is called a UI test and it mocks user interaction. You cannot achieve that with a unit test .

What you can achieve with a unit test is you can test the post controller by providing sample models posted to the controller with variation of valid and invalid model states to verify that the controller correctly populates the Session only when the ModelState is valid, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM