Simplify your test assertions

Came across this failing test, immediately felt a bit verbose… with testing in general the simpler and cleaner your tests can stay the better, otherwise test rot starts to creep up, developers procrastinate
about fixing them etc….

The test assertion was:


It Should_be_add_a_message_to_the_contractor_conversation = () =>
{
Assert.That(_contractor.Messages, Has.Some.InstanceOf().And.All.With.Property("Content").EqualTo(messageContent));
};

Changed to:


It Should_be_add_a_message_to_the_contractor_conversation = () =>
{
_contractor.Messages.ShouldContainAny(m => m.Content == messageContent);
};

The extension method:


public static void ShouldContainAny(this IEnumerable list, Func comparer, params T[] items)
{
if (items.Any(comparer))
{
throw new SpecificationException(string.Format(@"Should contain any matching item but contains none"));
}
}

Shared Transformable App/Web Config

Nice way to keep your config in one place for all types of projects is using web config transformations in addition to using shared (using Link) config files with DependentUpon back to the root config file, e.g. App.Config

Using the standard web config transforms, e.g:

In App.config

 <connectionStrings>
    <add name="Db" connectionString="Server=localhost;Port=3306;Database=myapp;
Uid=myappuser;Pwd=secret;/>
  </connectionStrings>

In App.Production.config

 <add name="Db" connectionString="Server=production;Port=3306;Database=myappprod;
UId=produser;Pwd=supersecret;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

This will transform your out so you have a production config file which overrides the standard config file. as documented here:
Web Config Transforms

But what if you need the config shared across projects, e.g. Unit Testing, Integration testing, Tasks project etc…

Well you can use the standard Linked file approach, where you Add an existing item but select the little drop down on the add button.
The project file items will look like below.


<ItemGroup>
	<Content Include="..\Shared\App.config">
	<Link>App.config</Link>
	<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

<Content Include="..\Shared\App.Local.config">
	<Link>App.Local.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>

<Content Include="..\Shared\App.Staging.config">
	<Link>App.Staging.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>

<Content Include="..\Shared\App.Production.config">
	<Link>App.Production.config</Link>
	<DependentUpon>App.config</DependentUpon>
</Content>
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />

<Target Name="PostTransformAppConfig" AfterTargets="TransformWebConfig">
	<Copy Condition="Exists('$(TransformWebConfigIntermediateLocation)\transformed\App.config')" 
                  SourceFiles="$(TransformWebConfigIntermediateLocation)\transformed\App.config" DestinationFiles="$(OutputPath)\App.config" />
	<Copy Condition="Exists('$(TransformWebConfigIntermediateLocation)\transformed\App.config')" 
                  SourceFiles="$(TransformWebConfigIntermediateLocation)\transformed\App.config" DestinationFiles="$(OutputPath)\App.config" />
</Target>

Then Just Call:

  msbuild myproject.proj /t:TransformWebConfig /p:Configuration=Production

And the output will be

 <add name="Db" connectionString="Server=production;Port=3306;Database=myappprod;
UId=produser;Pwd=supersecret;"/>

Win place at the TDD Masterclass!!

Roy Osherove is giving an hands-on TDD Masterclass in the UK, September 21-25. Roy is author of “The Art of Unit Testing” (http://www.artofunittesting.com/), a leading tdd & unit testing book; he maintains a blog at http://iserializable.com (which amoung other things has critiqued tests written by Microsoft for asp.net MVC – check out the testreviews category) and has recently been on the Scott Hanselman podcast (http://bit.ly/psgYO) where he educated Scott on best practices in Unit Testing techniques. For a further insight into Roy’s style, be sure to also check out Roy’s talk at the recent Norwegian Developer’s Conference (http://bit.ly/NuJVa).

Full Details here: http://bbits.co.uk/tddmasterclass

bbits are holding a raffle for a free ticket for the event. To be eligible to win the ticket (worth £2395!) you MUST paste this text, including all links, into your blog and email Ian@bbits.co.uk with the url to the blog entry.  The draw will be made on September 1st and the winner informed by email and on bbits.co.uk/blog

asp.net mvc – The virtual path maps to another application, which is not allowed

what a waste of time… i was getting this error:

Error 1
The virtual path ‘/Views/Shared/Site.Master’ maps to another application, which is not allowed. C:\temp\Views\Cart\New.aspx

this didn’t really help me, some searching said stuff about IIS mappings etc… anyways it turns out to be very simple… one of my views had a missing tilde ~

“/Views/Shared/Site.Master” i changed it to   “~/Views/Shared/Site.Master” and the problem was gone…

as i said what a waste of time !!!!


ApplicationPoolIdentity IIS 7, Asp.net Gotcha!!!!

 

What a waste of an hour…   I was running an asp.net mvc app  on my VMWare Fusion VM, with Windows 7 RC & IIS 7. When i kept getting this error:

System.Security.SecurityException: Request for the permission of type ‘System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.

The thing that threw me off was that it only seemed to happen doing certain stuff, like the mvc future strongly typed links and html helper submits..

<% Html.SubmitImage(…..

Also if you search for posts/forums on it you;’ll bump into the UNC path issue, which again threw me a little.. “was it because of my VM, was iis seeing my C drive as a share?” crazy talk but still that’s the kind of shit that goes through your head when debugging these kinds of issues…

 

Anyways in the end it was the identity that the Application pool for my site was running under, funnily enough  called “ApplicationPoolIdentity”.

I changed it to network service and all is fine..

check out this post which covers the issue…

Visual Studio CSS Formatting… nice!!

 

Now that I’m back doing alot of CSS i came across a post describing the CSS formatting feature in visual studio…

My preference is for single line css. I dont like to expand to one line per rule, thats just my personal preference. But it can be a pain to maintain.

Enter visual studio:

here’s some code i had….

 

body
  {
      background: #123;
      border-top: 5px solid #000;
      color: #333;
      font-size: 11px;
      padding: 20px 0 40px;
  }
  a
  {
      color: #fff;
      text-decoration: none;
  }
  a:hover
  {
      text-decoration: underline;
  }
  h1
  {
      font-family: Georgia, serif;
      font-weight: normal;
      text-align: center;
  }

and with a quick Format!!!

body { background: #123; border-top: 5px solid #000; color: #333; font-size: 11px; padding: 20px 0 40px; }
a { color: #fff; text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-family: Georgia, serif; font-weight: normal; text-align: center; }

nice….

 

now if only the formatter would make the rules alphabetical !!!!

Visual Studio CSS Formatting… nice!!

 

Now that I’m back doing alot of CSS i came across a post describing the CSS formatting feature in visual studio…

My preference is for single line css. I dont like to expand to one line per rule, thats just my personal preference. But it can be a pain to maintain.

Enter visual studio:

here’s some code i had….

 

body
  {
      background: #123;
      border-top: 5px solid #000;
      color: #333;
      font-size: 11px;
      padding: 20px 0 40px;
  }
  a
  {
      color: #fff;
      text-decoration: none;
  }
  a:hover
  {
      text-decoration: underline;
  }
  h1
  {
      font-family: Georgia, serif;
      font-weight: normal;
      text-align: center;
  }

and with a quick Format!!!

body { background: #123; border-top: 5px solid #000; color: #333; font-size: 11px; padding: 20px 0 40px; }
a { color: #fff; text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-family: Georgia, serif; font-weight: normal; text-align: center; }

nice….

 

now if only the formatter would make the rules alphabetical !!!!