Scott Gu announced yesterday that the new MVC 3 Preview has been released which includes the new Razor View engine…
Download it from here..
I thought I might take the opportunity to talk about a technique that we use to isolate our code from ConfigurationManager and also other system classes such as the system DateTime, using the language constructs built within the .Net Framework version 3.5 and above. I’m not saying that this technique is better than the Castle Dictionary Adapter method but it just highlights an alternative approach that is possible without using third party assemblies.
Ok so using Ben’s example, because I’m too lazy to think up my own, we have an application configuration value for our EnableNewsLetterSignup.
In our code we pull out the value using the static method on the ConfigurationManager class like so.
Ok so nothing new here but when it comes to testing this code it means that we have to have a configuration file for our unit test project (which is bad) and we can only ever test one path through the code without changing the value of the configuration file. Which means we some untestable code without doing some configuration Kung Fu on our config file which is more work than we should have to do.
The technique we use is to wrap our class up in a static wrapper class which we will call Config. For our corresponding application setting we add a public static field of the same name for clarity, of type Func<string>. Now the important thing is we give it it’s default behaviour which in this case is to go off and use our old friend ConfigurationManager to determine whether or not our newsletter is enabled or not.
Now going back to our code where we use the application setting it will now look something like this.
So now when we want to unit test this piece of code we simply change the default behaviour of our Config class to the behaviour we want to test. See line 5 below, where we force it to return false for the purpose of our unit test. We now have total control over what our supposedly configuration value is without ever having to touch the file system.
As I said earlier this technique can also be quite useful for isolating other system dependant classes such as code that uses the DateTime.Now property. Sometimes if you want to logic concerning dates and times it can be pretty hard to control the flow of the code because the DateTime is generated by the system. How for example do you test something if you need to be some date in the future or some date in the past. The code below can be used in the same way to isolate our tests from the underlying call to the DateTime.Now which can be overriden as required to give the behaviour we want.
This approach offers an alternative solution to using the Castle Dictionary Adapter, and although it requires writing slightly more code it does mean that it is one less assembly that you have to reference in your project which can be a good thing sometimes.
Ok this is a bit of a follow up from my earlier blog about the newly announced MVC view engine Razor from Scot Guthries blog a couple of weeks ago. Below is a quote that Scott has made in the comments to the original post.
“The Razor parser and view engine can be instantiated and used outside of the ASP.NET application domain. This means you can directly instantiate and use it within a unit test project without any dependencies on running ASP.NET.
From a unit testing perspective you can indicate the view template you want to run, supply it with any dependencies, and then pass your own test models/viewmodels to it and have it run and render back a string to you. You could then verify that the correct content came back. This would isolate the views from your controllers and any data access, and allow you to also isolate them from the runtime environment. View engines in ASP.NET MVC VNext (both Razor and the .ASPX one) will also support and integrate with dependency injection as well.”
Let’s start with talking about the current testing story with the current MVC framework. Now clearly you want to put as much logic into your controller as possible which is currently testable with the current MVC framework. But on the modern MVC websites that I’ve been working on there ends up being a considerable amount of JQuery in most pages.
This is where I hear you shout “but what about QUnit you can test your JQuery with that” Yes you can but do I do it..No I don’t. I never really got QUnit it always seemed great in theory but in reality it seemed like too much work to implement.
So typically I find myself writing JQuery that does fairly trivial stuff. Hiding and showing stuff and manipulating items in the Dom. Now when I say trivial it is still important. If you hide your submit button when it should be displayed you have broken your page.
To counter this John Teague over at Los Techies outlines a technique that you can use to generate your markup automatically. To be honest I haven’t used this approach as it still seems like a lot of extra hard work, but it’s worth considering.
Therefore going back to Scott Guthrie’s comment that we will now be able to isolate the view from the controller pass in some data and render back a screen as HTML. This is just fantastic, and for me the last piece in the puzzle with regards to testing. With this functionality it will should be relatively trivial to produce the markup that we can run our QUnit tests against and it means that we are actually testing our view rather than a mock up of it.
Well done Microsoft I think this is a positive step, and I can’t wait to get my hands on the framework. Keep up the good work.
So you want to add a scrollable view to your iPhone/iPad application and you want to use the power of Interface Builder to make it easier. This is how you do it.
In this example we are going add a vertically scrolling window to an iPhone screen in portrait mode.
Start by adding your UIScrollView to your view using Interface Builder. It is not possible to actually scroll the view itself within Interface Builder, so select the UIScrollView and move it up so half the view is off the screen. Drag the edge of the screen to make it longer and then add your extra controls to the bottom. Once you are finished drag the view back to its original position so that you can see the top of the screen again.
Now in Interface Builder select your UIScrollView and look at its size in the Attribute Inspector window. Make a note of the views height, in this example assume it is 756 pixels. Because we don’t want horizontal scrolling in this example the width should be the same as the standard iPhone screen (320 pixels) .
Finally in Interface Builder make sure you add an outlet between your ViewController and your UIScrollView.
Go back to your .NET code and open up your view controller. In your ViewDidLoad method override add the following code to set the initial frame and the content size of you scroll view. Notice that the content size has the full dimension of the entire scrollable view eg. 320 pixels wide and 756 pixels high.
Now fire up your application in the iPhone Simulator and you should now have a working scrollable view.