How to test that the view compiles?

4 views
Skip to first unread message

Mihai

unread,
Aug 23, 2008, 9:14:06 AM8/23/08
to Spark View Engine Dev
Ok, I've sent this message to the recipient of the contact form at
dev.jardin.org. So at the risk of getting my ass kicked for not
posting here first I'll ask again.

I've just started using Sparke since I think is the View engine that
best fits the MVC Model. Sure NHaml is cool too but let's be honest
were writing web sites not playing around with '%' signs etc..

So the first wall I'm hitting is the fact that I'm writing certain
things wrong and keep getting runtime errors about the compiling
process. My question is: How to get a test running that checks for
those errors so I don't have to run the web site. I'm sure it's pretty
simple ( at least I'm imagining it should be) but until I start
learning the code behind the engine better I'm forced to rely on your
experience.

Thanks in advance.

Louis DeJardin

unread,
Aug 23, 2008, 2:16:20 PM8/23/08
to spar...@googlegroups.com
One thing that might help you would be the view precompilation that's being worked on... There's an active thread going on and that could be something which helps.

Two use cases so far are to precompile a large number of views on startup, and to precompile the views to an assembly on disk. The first would give you up-front notice of any errors, and the second would do that and also help with deploying to shared/hosted environments.

It sounds like this would be another good third use-case to keep in mind. Precompiling views from a unit test would give you a nice, universal pass/fail condition.

----

That said if you want to unit test view compilation without waiting for those features here's an example. The Castle case would be a bit more difficult because the engine needs more run-time information about registered view components.


[Test]
public void TestCompiledViewsWithJustEngine()
{
    // Make the same settings the web app provides.
    // (Assemblies are forced to be added because many aren't loaded
    // in the nunit appdomain.)
    var settings = new SparkSettings()
        .SetDebug(false)
        .SetPageBaseType("MvcContrib.SparkViewEngine.SparkView")
        .AddNamespace("System")
        .AddNamespace("System.Collections.Generic")
        .AddNamespace("System.Linq")
        .AddNamespace("System.Web.Mvc")
        .AddNamespace("NorthwindDemo.Models")
        .AddNamespace("NorthwindDemo.Views.Helpers")
        .AddAssembly("NorthwindDemo")
        .AddAssembly("System.Web.Mvc")
        .AddAssembly("MvcContrib")
        .AddAssembly("MvcContrib.SparkViewEngine")
        .AddAssembly("System.Web.Routing");
   
    // create an engine
    var engine = new SparkViewEngine(settings)
                     {
                         ViewFolder = new FileSystemViewFolder(@"..\..\..\NorthwindDemo\Views")
                     };

    // Generate and compile a bunch of views
    engine.CreateEntry(new SparkViewDescriptor()
                           .SetTargetNamespace("NorthwindDemo.Controllers")
                           .AddTemplate("Products/Edit.spark")
                           .AddTemplate("Shared/Application.spark"));

    engine.CreateEntry(new SparkViewDescriptor()
                           .SetTargetNamespace("NorthwindDemo.Controllers")
                           .AddTemplate("Products/Categories.spark")
                           .AddTemplate("Shared/Application.spark"));

    engine.CreateEntry(new SparkViewDescriptor()
                           .SetTargetNamespace("NorthwindDemo.Controllers")
                           .AddTemplate("Products/Detail.spark")
                           .AddTemplate("Shared/Application.spark"));

    engine.CreateEntry(new SparkViewDescriptor()
                           .SetTargetNamespace("NorthwindDemo.Controllers")
                           .AddTemplate("Products/ListingByCategory.spark")
                           .AddTemplate("Shared/Application.spark"));

Mihai

unread,
Aug 23, 2008, 3:45:12 PM8/23/08
to Spark View Engine Dev
Thank you very much, I'll try it tomorrow.

BTW: I'm very impressed with Spark - it just feels right.
I've spent the better half of the day going "Oh! It does this too? :-
o"
These moments are rare, but when they actually happen it' just
great :D

:D Thanks again

Louis DeJardin

unread,
Aug 23, 2008, 4:01:31 PM8/23/08
to Spark View Engine Dev
Thanks! I'm glad you like it. It was a lot of fun to build a view
engine the way I would expect one to work, and it's been especially
rewarding to see other people adopting it. The requests and
suggestions have been great.
Reply all
Reply to author
Forward
0 new messages