Can we unit test View ('V') of MVC? - asp.net-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.

Related

Replace MVC Application Part and Razor File Providers in runtime

I have a WebAPI/MVC app which loads separate plugin assemblies with embedded controllers and views and puts them into the MVC PartManager. This works fine but now I'd like to be able to swap these parts out in runtime without restarting the server. I can use PartManager.Remove on the old part and Add with a new part, but this does not seem to be enough. When calling the controller I still get a response from the old controller in the old part.
Apart from adding parts in the PartManager I also add a EmbeddedFileProvider into the RazorViewEngineOptions on Configure because just adding an AssemblyPart does not seem to be enough for the Razor engine to find the view (not sure I'm doing this right). But I don't know if I can replace this Razor-fileprovider in runtime? I have not found a way.
Anyway, what is the preferred way of doing all this, I mean loading/replacing dlls with controllers, views and static resources. Am I on the right track even?
i think that Razor view engines are configured once only, so you cannot change them later.
What you can do is to
have multiple view engines running,
choose between them based on the URL (as in your case - you want to use a different engine for the one controller).
This is described in CUSTOMMOBILEVIEWENGINE section of Scott Hanselsman's blog post. What he was doing was to use different view engines for different devices, but you can adapt the technique for your own purposes.

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.

ASP.NET MVC View/Site Design

This stems from my question that i started at MVC Beginner Question
Im now confident in developing a MVC website from the MVC side of things.
My fatal floor now is the UI level designing. i.e. designing views and master pages including css styling.
Just wanting to get any advice on where i should start? Im currently about to look at the expression studio to see if that will help.
I need to get up and running in this quickly so that it doesnt hold back the development of the website.
Thanks in advance
I would look at a CSS grid-framework like 960 Grid System or Blueprint.
There is often controversy from CSS/Markup purists as to whether the use of such frameworks is valid, but they often overlook that not everyone is a CSS expert. I have found that the use of these frameworks has saved me hours of productivity since my CSS skills (as a programmer) are not that great.
As far as using Expression, I tend to use this as a "test-bed" with simple HTML files. The Designer and CSS Style support seem to be more interactive than that which Visual Studio supports, so for quick mock-ups -- Expression, then move that HTML/CSS over to VS as soon as possible.
One other priceless tool to use while designing is the add-on "Firebug" for Firefox ... it allows you to inspect single elements on your page, the CSS properties affecting that element - it even allows you to change CSS attributes "on-the-fly" to immediately see the ramificatiions. This tool has saved me HOURS!
Consider finding a suitable theme in a free open source template.
Consider something that suits at http://www.freecsstemplates.org/.

Grails: Debugging scaffolding template

is there a way to debug the scaffolding template (generated in /src/template/scaffolding). Not to debug the view gsp files, but the template itself. I guess I could put some comments.. other than that..
Normally you should not have to "debug" the template as the templates work rather well.
I am thinking you are looking for tracing the data flow to make your own modifications to the templates which will lead you to using print statements. Otherwise, you are looking at using the Page Renderer.
So, there are two options I would recommend:
Print statements. This is your easy, tried and true, brute force approach. I have used it a year ago in early 2011 for a project and it works well. The downside is that it is non reusable (unless you leave your code commented out but that's a bit awkward).
This allows you to step through the "guts" of the template system and see what happens. The framework cannot "hide" anything from you really, so you get a front-row seat at what happens.
Use the grails.gsp.PageRenderer
This allows you to debug things at runtime, you can create proper tests, asserts, reusable components, the works. You are a little removed from the actual templates (as opposed to using print statements inside the templates).
Here is a link to the GRAILS Doc: Page Rendering Doc
Here is a link to an example on how to use them: Grails Goodness: Render GSP Views and...

Asp.net MVC View Testing?

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.

Resources