Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do web testing for ASP.NET MVC views?

In ASP.NET WebForms, I've used Selenium to record and run my web and UI tests in a number of projects.

I know we can unit test Controllers and Models in ASP.NET MVC very easily.

What about Views?

Would you also test Views? If not how to make sure the Views, which are rendered by Controller Actions, contain the expected logics and behaviour?

I'd guess the same Web Testing Tools used in WebForms, such as WatiN and Selenium, could also be used here, not sure?

If so, what would be the difference between WebTesting in WebForms and WebTesting in MVC?

like image 485
The Light Avatar asked Jan 03 '13 21:01

The Light


People also ask

What is MVC in testing?

The Model-View-Controller (MVC) framework is an architectural pattern that separates an application into three main logical components Model, View, and Controller. Hence the abbreviation MVC.

How do I test a .NET project?

The dotnet new sln command creates a new solution in the unit-testing-using-dotnet-test directory. Change directory to the unit-testing-using-dotnet-test folder. The dotnet new classlib command creates a new class library project in the PrimeService folder. The new class library will contain the code to be tested.


2 Answers

What about Views?

Erm, Selenium and WatiN.

Really, what makes you think that ASP.NET MVC views are any different than classic ASP.NET WebForms as far as integration tests are concerned? I mean they all spit HTML at the end of the day. That's what browsers and stuff like Selenium understand. Who spit this HTML is not that much important. What you care about in an integration test is that when a user lambda clicks on a button alpha after entering beta in the gamma text field hi got this Welcome User Phi on the resulting HTML page, don't you? You really don't care about things like ASP.NET MVC or even less about things like ASP.NET MVC Views.

By the way you could use Selenium and WatiN with PHP. Or with Java Servlets if you are a fan of them. Doesn't really matter the server side technology you are using. What matters in an integration test is the scenarios that you have defined previously.

If so, what would be the difference between WebTesting in WebForms and WebTesting in MVC?

None.

like image 130
Darin Dimitrov Avatar answered Oct 04 '22 11:10

Darin Dimitrov


I think there are differences between ASP.NET MVC and WebForms when it comes to browser driven integration tests, in that the different Server-side aspects of these frameworks lead to different testing approaches.

Take a framework that assists with this, such as Seleno. It uses Selenium under the bonnet but abstracts all browser interaction into C# Page Objects, which you can then use very fluently in C# tests.

There are lots of extensions and conventions which assist with many common browser automation operations, such as clicking on buttons or extracting data from tables.

But why do I bring this up as an answer to this question? Seleno is particularly handy for .NET MVC as the Page Objects can be generic, typed to the ViewModel for a particular View (or Partial View) which creates a strongly typed association when writing a corresponding Page Object for that View.

It is the ViewModel aspect of MVC which opens up this sort of approach; I think it would be trickier to do this with WebForms.

like image 25
Holf Avatar answered Oct 04 '22 12:10

Holf