Tags » ASP .NET MVC

Unit Testing on Visual Studio 2013 with NUnit framework

Unit Testing on Visual Studio 2013

Visual Studio 2013 provides us a test framework library named MsTest, proprietary of Microsoft.

We can write and run unit tests using… 196 more words

Programming

Meet Mr. ASP.NET MVC4

Me: Hello!! Welcome to everyone. I am so excited to introduce and welcome our guest Mr. ASP.NET MVC4. Welcome Mr.MVC4

ASP.NET MVC4: Thank you so much for that. 898 more words

ASP.NET MVC

Testing the view

The view part of ASP.NET MVC is difficult to test in isolation. It is more commonly integration tested together with the rest of the system. 314 more words

ASP.NET

Testing the model

The model part of ASP.NET MVC is best tested in conjunction with the database.

public class ShipRepository : IShipRepository
{
    private string ConnectionString;

    public ShipRepository()
    {
        ConnectionString = ConfigurationManager
            .ConnectionStrings["Database"].ToString();
    }

    public ShipRepository(string connectionString)
    {
        ConnectionString = connectionString;
    }

    public IEnumerable<Ship> GetList()
    {
        const string query = "SELECT Name FROM Ships";
        var ships = new List<Ship>();
        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.Open();
            using (var command = new SqlCommand(query, connection))
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    ships.Add(new Ship { Name = reader.GetString(0) });
                }
            }
        }
        return ships;
    }

    public void Insert(Ship ship)
    {
        var ships = new List<Ship>();
        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.Open();
            using (var command = new SqlCommand("spInsertShip", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@Name", ship.Name);
                command.ExecuteNonQuery();
            }
        }
    }
}
…
224 more words

ASP.NET

Testing the controller

The controller is the most straightforward part of ASP.NET MVC to test.

public class ShipController : Controller
{
    private readonly IShipRepository Repository;

    public ShipController() : this(new ShipRepository()) { }

    public ShipController(IShipRepository repository)
    {
        Repository = repository;
    }

    public ViewResult Index()
    {
        IEnumerable<Ship> ships = Repository.GetList();
        return View(ships);
    }

    public RedirectResult Create(Ship ship)
    {
        if (ModelState.IsValid)
        {
            Repository.Insert(ship);
        }
        return Redirect("/");
    }
}
…
165 more words

ASP.NET

Validating a token from the client against the server to prevent CSRF attacks

Web security should always be No.1 priority which needs to be taken into consideration by a web developer, but surprisingly most of the web developers prioritize how to make it attractive and pay less attention to web security. 399 more words

ASP.NET