简体   繁体   English

ASP.NET中输入字段的单元测试验证

[英]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) 索引视图(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) 索引视图(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. MVC控制器的单元测试的目的是验证控制器的行为是否正确。 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. 您想要实现的目标称为UI test ,它可以模拟用户交互。 You cannot achieve that with a unit test . 您不能通过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. 单元测试可以实现的是,您可以通过向发布到控制器的样本模型提供有效和无效模型状态的变化来测试后控制器,以仅在ModelState有效时验证控制器正确地填充Session等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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