Asp.net MVC View Testing? - asp.net-mvc

With more and more code pushed to the Views in Asp.Net MVC (i.e. AJAX, JQuery, etc...), how do you maintain the 'testability'?
How do you test your Views?
How do you test your views with client-side jscript code?
How do you test your Views with Async behavior?
It seems that most examples on the testability of MVC deal with controllers. What about Views?

Selenium is a great tool for testing the front end of any web app. It is written in the browser's native language, JavaScript. Having the browser run the test framework code gives your tests the ability to expose browser incompatibility issues. It is free and open source.

Also see other free browser automation tools like ArtOfTest and WatiN. The Selenium stack can be a little complicated to set up.

Related

Can use visual studio 2017 with angular4 whitout index view? without mvc?

All the examples I've seen, even the "official examples" (spa) all use an MVC project always having a view (index.cshtml) to run a project using angular (2/4).
Angular is a front-end framework that does not need a view (index.cshtml) to fulfill its function, the concept of front-end is to develop everything next to the client without depending on the backend.
Then, because you have to use a server-side view to work the client side.
What would be the best way to work with an application using angular (2/4)? Of course it would be better to use a single solution for all projects, but i see that can't separate the front end with back end.

Design Surface in ASP.Net MVC

Are there any recommendations for a design surface (or design tool) that could be used by a designer (the dude/dudette with the black turtleneck) in the process of building an ASP.Net MVC application?
Such that once there is agreement on the structure of the Model, and the interactions required by the app, then the designer goes away and builds out the UI (V in MVC) using this design tool, while the developer goes away and builds out the code (M&C in MVC) using Visual Studio.
As I understand it now, this designer person would also need to use Visual Studio and build the Views using Razor (or other view engine) syntax, instead of having the ability to build the Views using a design surface with drag-drop layout and property settings and the like.
I think you could ask them to give you a harcoded HTML for each view... then, you replace the harcoded data once you place it in Razor.
That is the beauty of Razor... it is very easy to pass from fixed HTML to a razor view.
Razor requires programming, and creating views is not an arbitrary task. I'd let the designer work in whatever program they like where they can excel about design and let the programmers worry about how best to create the views and programming in Razor (which is really just C#/VB with some extra syntax).
It depends.. what kind of skills does your designer have? Are they a true designer (no JS/jQuery/etc knowledge.. they live & breath PSDs) or are they a designer/front end engineer (they know js/jQuery, the diff. between IE7 and IE9 from a rendering pov).
If you've got a true designer then the best thing to get would be HTML. A PSD would be okay as well but if they can convert their design into HTML they've done a lot of the hard work for you (unless you love figuring out floats and margins and all that jazz). That said if you're using something like Twitter Bootstrap or Blueprint CSS then they would obviously have to know how to use that as well.
If you're lucky enough to have a designer/front end engineer then it's well worth your time to teach them some basic razor synax like #Url.Action and #Html.BeginForm. They can tell you the actions they need and you can work together by giving them a fake data/response version first which they can use while you create a real version. And in this case they can either use Visual Studio OR you can set it up so that they have your site hosted via IIS on their machine and they just use your source control to get latest which automatically gets placed in the right directory. Then they can just continue working in whatever editor they prefer as they should only be working in html which will get updated live. That said if you're using ASP.Net MVC 4 bundling you'll have to decide how to maintain bundles.

Unit Testing rich Views for ASP.NET MVC

If I have a rich AJAX-driven GUI in ASP.NET MVC, how would I unit test that effectively using a framework like NUnit?
If you're looking to test the functionality of the UI, use browser automation tools like Selenium.
jQuery has a unit testing project called QUnit which you could use to test your ajax code from a client. It isn't going to integrate with NUnit, but it is another option.
With NUnit, you would primarily be testing your controller actions -- i.e., given a set of parameters and a configuration, does the method return the correct result and view model. If you need to test your client-side javascript, you should look at either a javascript unit testing framework or a UI testing tool, such as Selenium or WatiN.
You can use Selenium IDE to record tests and the generate NUnit testcases to verify behaviour and output of your views.
Steve Sanderson also blogged earlier on today about HtmlUnit on .NET as 'headless browser'. (Testing using the Selenium server can be slow, because it requires creating an instance of FireFox, IE etc);
You would have to unit test your ajax calls and not your UI per se., For example if you are getting some json data through a ajax call, this would translate to a controller method which can be unit tested.
I you want to unit test something which happens after the data reaches the browser then its a whole different ball game and not related to MVC.

ASP.NET MVC for Ruby on Rails developers?

Long time lurker, first time poster. I'm a self-taught hacker that learned Ruby on Rails to start. At work I've been allowed to work on a web app--the only catch is I have to use ASP.NET. This technology choice is mandated, as much as I'd prefer to use Rails.
There's dozens of "Rails for .NET/PHP/Java Developers" books and blog posts but I haven't found any going the opposite direction, from Rails to .NET.
Could someone please give me an overview of how a typical Rails app would translate over to ASP.NET MVC? I'll research the details of the IDE, C#/VBscript, etc. But what are the possible equivalents to:
Generators
Gems/Plugins
Databases
Migrations
Routes
Models (ORMs)
Controllers (InheritedResources)
Views (layouts, templates, partials)
Rails Console
Test Units/Specs
etc. anything else I'm forgetting
I assume a lot of the Rails niceties I take for granted like route-based helper methods, and simple macro association declarations will not be possible. :(
Thank you so much!
I think what you'll find in the .Net world is that you have a lot of choices to make. Rails is nice because it provides all of that stuff in one place, but developing for .Net you'll have to piece together a solution of your own.
Generators - There are various code generation facilities, but each one is for a different piece. Eg, you can get MyGeneration that will generate code based on a database.
Gems/Plugins - No uniting system for this; Components can be found on the web and you would download either the source or the .dll, then you would add a reference in your project to the assembly (.dll).
Databases - you can connect to pretty much anything; You'll probably find the most guidance for an MS SQL Server.
Migrations - I don't know of a direct method for this in the .net world; I usually write SQL code in SQL and run scripts on the server manually as part of deployment.
Routes - ASP.Net MVC includes routes, look in the global.asax.cs file that gets generated when you create a project for example.
Models (ORMs) - ORMs for .Net are all over the place. Included as part of .Net are things like Linq-to-sql and the Entity Framework. Outside of MS you can find many, but I'd probably recommend NHibernate.
Controllers - Built in to .Net MVC; You get to write the code.
Views - Built in to .Net MVC; Once again you get to write them. MasterPages allow you to get the same general layout on all your pages(including common header/footer, etc), Web Controls (.ascx files) allow you to do a partial view.
Rails Console - I don't know exactly what this provides (I'm a .net developer interested in learning Rails, but haven't spent much time yet); Visual Studio lets you debug applications, step through code, etc. I don't think there are any consoles available to test code outside of just writing the code, compiling, and running it.
Test Units/Specs - There are a few test frameworks for .Net (MS has a framework included, NUnit is one alternative). For specs and such, probably google around for Behavior Driven Design and see what exists.
There are a couple of .NET ports of RoR migrations. I have used migratordotnet and FluentMigrator. Both work as expected but I prefer FluentMigrator. It is more full-featured (e.g. can create indexes) and I like the fluent style.
LINQPad is your equivalent to Rails Console.. see here:
https://stackoverflow.com/a/9403457/1029644
You should download Visual Studio 2008 Express, and download ASP.NET MVC 1.0 (I wouldn't download ASP.NET MVC 2.0 yet because it's only in RC. Wait until it hits 2.0 final).
You can also check out the Nerddinner walkthrough. It's very helpful when learning ASP.NET MVC.
Generators
Do you mean code generators? Ew.
Gems/Plugins
If you want functionality, you can either build it or see if a JQuery plugin exists for it.
Databases
The Database is accessed through your model.
Migrations
?
Routes
Routing is handled by the framework, and you can add routes in the Global.asax.cs file.
Models (ORMs)
Models are indeed still called 'Models', and in ASP.NET MVC, if you use LINQ-To-SQL, the model is generated for you when you drag your database tables in. You can use the Repository pattern to access the database model.
Controllers (InheritedResources)
Controllers are still called controllers.
Views (layouts, templates, partials)
There are different types of View Engines, but the one provided with ASP.NET MVC should do well at first.
Rails Console
I'm guessing you mean the IDE/Debugger? You can build and debug an ASP.NET MVC app inside of Visual Studio.
Test Units/Specs
You can use NUnit, or you can use MSUnit. MSUnit is already integrated with Visual Studio, but NUnit can be.

Can we unit test View ('V') of MVC?

Duplicate: Unit Testing the Views?
Is there any way to unit test View?
I am sure that we can test Model & Controller but don't know how to unit test View?
Is that worth testing View?
You can enable compilation of MVC views. That helps a lot. Otherwise, I don't think it is worth it. After all, the there are only two things that you are interested in. Does view compile and do you get any exceptions (null, out of bounds exceptions, or similar)?
There are some folks who claim that you should not include any logic in view. Write helpers for anything. In that case, compilation is pretty much everything you'll want.
We decided to invest into WatiN testing. It tests views and it tests the whole app at the same time. Has some nice helpers, but requires constant maintainance.
Haven't views abandoned code behind now? So what are you going to test? If you are testing the controller, then you just need a succesful view result to show that the view works. Rather than going to the trouble of pre compiling views or whatever, this will start to drag any sizeable project down in terms of continuous integration, and build.
From what I have read (in Pro ASP.NET MVC Framework by Steven Sanderson), views are not considered worth testing. ASP.NET MVC viewes can be generated using various engines, e.g. the default lightweight ASPX, or for example http://www.stringtemplate.org/. For ASPX output you might run some HTML syntax checker tool, and for other view engines the fact that the views compile successfully should be a good enough test ;)
I do not see the point of unit testing the views, since they don't contain much logic. You can however to some integration testing/UI testing using a tool like WatiN.
Example of a test written in WatiN:
[Test]
public void SearchForWatiNOnGoogle()
{
using (IE ie = new IE("http://www.google.com"))
{
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("WatiN"));
}
}
You should not try to test everything using tool like this. Select some key functionality of the application, and write test for them.
For those who do not see the value in testing views.... How can you be sure that the view has the right attributes on elements, or that it is bound correctly?
Many answer "at a higher level" (such as running the site and using tools such as selenium or equivalent).
However these techniques make it virtually impossible to prove that the source of the error is in the view itself and also require massive changes to the server side code so that the views can be rendered in a targeted manner.

Resources